Edit stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.9 KiB

  1. // In production, we register a service worker to serve assets from local cache.
  2. // This lets the app load faster on subsequent visits in production, and gives
  3. // it offline capabilities. However, it also means that developers (and users)
  4. // will only see deployed updates on the "N+1" visit to a page, since previously
  5. // cached resources are updated in the background.
  6. // To learn more about the benefits of this model, read https://goo.gl/KwvDNy.
  7. // This link also includes instructions on opting out of this behavior.
  8. const isLocalhost = Boolean(
  9. window.location.hostname === 'localhost' ||
  10. // [::1] is the IPv6 localhost address.
  11. window.location.hostname === '[::1]' ||
  12. // 127.0.0.1/8 is considered localhost for IPv4.
  13. window.location.hostname.match(
  14. /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
  15. )
  16. );
  17. export default function register() {
  18. if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
  19. // The URL constructor is available in all browsers that support SW.
  20. const publicUrl = new URL(process.env.PUBLIC_URL, window.location);
  21. if (publicUrl.origin !== window.location.origin) {
  22. // Our service worker won't work if PUBLIC_URL is on a different origin
  23. // from what our page is served on. This might happen if a CDN is used to
  24. // serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374
  25. return;
  26. }
  27. window.addEventListener('load', () => {
  28. const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
  29. if (isLocalhost) {
  30. // This is running on localhost. Lets check if a service worker still exists or not.
  31. checkValidServiceWorker(swUrl);
  32. } else {
  33. // Is not local host. Just register service worker
  34. registerValidSW(swUrl);
  35. }
  36. });
  37. }
  38. }
  39. function registerValidSW(swUrl) {
  40. navigator.serviceWorker
  41. .register(swUrl)
  42. .then(registration => {
  43. registration.onupdatefound = () => {
  44. const installingWorker = registration.installing;
  45. installingWorker.onstatechange = () => {
  46. if (installingWorker.state === 'installed') {
  47. if (navigator.serviceWorker.controller) {
  48. // At this point, the old content will have been purged and
  49. // the fresh content will have been added to the cache.
  50. // It's the perfect time to display a "New content is
  51. // available; please refresh." message in your web app.
  52. console.log('New content is available; please refresh.');
  53. } else {
  54. // At this point, everything has been precached.
  55. // It's the perfect time to display a
  56. // "Content is cached for offline use." message.
  57. console.log('Content is cached for offline use.');
  58. }
  59. }
  60. };
  61. };
  62. })
  63. .catch(error => {
  64. console.error('Error during service worker registration:', error);
  65. });
  66. }
  67. function checkValidServiceWorker(swUrl) {
  68. // Check if the service worker can be found. If it can't reload the page.
  69. fetch(swUrl)
  70. .then(response => {
  71. // Ensure service worker exists, and that we really are getting a JS file.
  72. if (
  73. response.status === 404 ||
  74. response.headers.get('content-type').indexOf('javascript') === -1
  75. ) {
  76. // No service worker found. Probably a different app. Reload the page.
  77. navigator.serviceWorker.ready.then(registration => {
  78. registration.unregister().then(() => {
  79. window.location.reload();
  80. });
  81. });
  82. } else {
  83. // Service worker found. Proceed as normal.
  84. registerValidSW(swUrl);
  85. }
  86. })
  87. .catch(() => {
  88. console.log(
  89. 'No internet connection found. App is running in offline mode.'
  90. );
  91. });
  92. }
  93. export function unregister() {
  94. if ('serviceWorker' in navigator) {
  95. navigator.serviceWorker.ready.then(registration => {
  96. registration.unregister();
  97. });
  98. }
  99. }