Guest User

Untitled

a guest
Jan 22nd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. // In production, we register a service worker to serve assets from local cache.
  2.  
  3. // This lets the app load faster on subsequent visits in production, and gives
  4. // it offline capabilities. However, it also means that developers (and users)
  5. // will only see deployed updates on the "N+1" visit to a page, since previously
  6. // cached resources are updated in the background.
  7.  
  8. // To learn more about the benefits of this model, read
  9. // This link also includes instructions on opting out of this behavior.
  10.  
  11. const isLocalhost = Boolean(
  12. window.location.hostname === 'localhost' ||
  13. // [::1] is the IPv6 localhost address.
  14. window.location.hostname === '[::1]' ||
  15. // 127.0.0.1/8 is considered localhost for IPv4.
  16. window.location.hostname.match(
  17. /^127(?:.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
  18. )
  19. );
  20.  
  21. export default function register() {
  22. if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
  23. // The URL constructor is available in all browsers that support SW.
  24. const publicUrl = new URL(process.env.PUBLIC_URL, window.location);
  25. if (publicUrl.origin !== window.location.origin) {
  26. // Our service worker won't work if PUBLIC_URL is on a different origin
  27. // from what our page is served on. This might happen if a CDN is used to
  28. // serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374
  29. return;
  30. }
  31.  
  32. window.addEventListener('load', () => {
  33. const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
  34.  
  35. if (!isLocalhost) {
  36. // Is not local host. Just register service worker
  37. registerValidSW(swUrl);
  38. } else {
  39. // This is running on localhost. Lets check if a service worker still exists or not.
  40. checkValidServiceWorker(swUrl);
  41. }
  42. });
  43.  
  44. window.addEventListener('message', messageEvent => {
  45. const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
  46.  
  47. if (messageEvent.data === 'skipWaiting') {
  48. console.log('skipWaiting');
  49. return navigator.serviceWorker.getRegistration(swUrl)
  50. .then(registration => registration.skipWaiting());
  51. }
  52.  
  53. console.log(`message=${messageEvent.data}`);
  54. });
  55.  
  56. let refreshingPage;
  57. navigator.serviceWorker.addEventListener('controllerchange', () => {
  58. console.log('refreshing page now');
  59. if (refreshingPage) return;
  60. refreshingPage = true;
  61. window.location.reload();
  62. });
  63. }
  64. }
  65.  
  66. function registerValidSW(swUrl) {
  67. navigator.serviceWorker
  68. .register(swUrl)
  69. .then(registration => {
  70. registration.onupdatefound = () => {
  71. const installingWorker = registration.installing;
  72. installingWorker.onstatechange = () => {
  73. if (installingWorker.state === 'installed') {
  74. if (navigator.serviceWorker.controller) {
  75. // At this point, the old content will have been purged and
  76. // the fresh content will have been added to the cache.
  77. // It's the perfect time to display a "New content is
  78. // available; please refresh." message in your web app.
  79. console.log('>> New content is available; please refresh.');
  80. navigator.serviceWorker.controller.postMessage('skipWaiting');
  81. } else {
  82. // At this point, everything has been precached.
  83. // It's the perfect time to display a
  84. // "Content is cached for offline use." message.
  85. console.log('Content is cached for offline use.');
  86. }
  87. }
  88. };
  89. };
  90. })
  91. .catch(error => {
  92. console.error('Error during service worker registration:', error);
  93. });
  94. }
  95.  
  96. function checkValidServiceWorker(swUrl) {
  97. // Check if the service worker can be found. If it can't reload the page.
  98. fetch(swUrl)
  99. .then(response => {
  100. // Ensure service worker exists, and that we really are getting a JS file.
  101. if (
  102. response.status === 404 ||
  103. response.headers.get('content-type').indexOf('javascript') === -1
  104. ) {
  105. // No service worker found. Probably a different app. Reload the page.
  106. navigator.serviceWorker.ready.then(registration => {
  107. registration.unregister().then(() => {
  108. window.location.reload();
  109. });
  110. });
  111. } else {
  112. // Service worker found. Proceed as normal.
  113. registerValidSW(swUrl);
  114. }
  115. })
  116. .catch(() => {
  117. console.log(
  118. 'No internet connection found. App is running in offline mode.'
  119. );
  120. });
  121. }
  122.  
  123. export function unregister() {
  124. if ('serviceWorker' in navigator) {
  125. navigator.serviceWorker.ready.then(registration => {
  126. registration.unregister();
  127. });
  128. }
  129. }
  130.  
  131. console.log('>> New content is available; please refresh.');
  132. navigator.serviceWorker.controller.postMessage('skipWaiting');
  133.  
  134. window.addEventListener('message', messageEvent => {
  135. const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
  136.  
  137. if (messageEvent.data === 'skipWaiting') {
  138. console.log('skipWaiting');
  139. return navigator.serviceWorker.getRegistration(swUrl)
  140. .then(registration => registration.skipWaiting());
  141. }
  142.  
  143. console.log(`message=${messageEvent.data}`);
  144. });
  145.  
  146. message=
  147. registerServiceWorker.js:53 message=!_{"h":"I0_1548194465894"}
  148. registerServiceWorker.js:53 message=!_{"s":"/I0_1548194465894::_g_rpcReady","f":"I0_1548194465894","r":"I0_1548194465894","t":"32296067","c":1,"a":[null],"g":false}
  149. registerServiceWorker.js:53 message=!_{"s":"__cb","f":"I0_1548194465894","r":"I0_1548194465894","t":"32296067","c":null,"a":[1,[null]],"g":false}
  150. registerServiceWorker.js:53 message=!_{"s":"__cb","f":"I0_1548194465894","r":"I0_1548194465894","t":"32296067","c":null,"a":[2,[null]],"g":false}
  151. registerServiceWorker.js:53 message=!_{"s":"/I0_1548194465894::_g_restyleMe","f":"I0_1548194465894","r":"I0_1548194465894","t":"32296067","c":2,"a":[{"setHideOnLeave":false}],"g":false}
  152. registerServiceWorker.js:53 message=!_{"s":"__cb","f":"I0_1548194465894","r":"I0_1548194465894","t":"32296067","c":null,"a":[4,[null]],"g":false}
  153. registerServiceWorker.js:53 message=!_{"s":"/I0_1548194465894::authEvent","f":"I0_1548194465894","r":"I0_1548194465894","t":"32296067","c":3,"a":[{"type":"authEvent","authEvent":{"type":"unknown","eventId":null,"urlResponse":null,"sessionId":null,"postBody":null,"tenantId":null,"error":{"code":"auth/no-auth-event","message":"An internal error has occurred."}}}],"g":false}
  154. registerServiceWorker.js:53 message=!_{"s":"__cb","f":"I0_1548194465894","r":"I0_1548194465894","t":"32296067","c":null,"a":[3,[true]],"g":false}
Add Comment
Please, Sign In to add comment