通用评论

registerServiceWorker.js.map 7.2KB

1
  1. {"version":3,"sources":["../src/registerServiceWorker.js"],"names":["register","unregister","isLocalhost","Boolean","window","location","hostname","match","process","env","NODE_ENV","navigator","publicUrl","URL","PUBLIC_URL","origin","addEventListener","swUrl","checkValidServiceWorker","serviceWorker","ready","then","console","log","registerValidSW","registration","onupdatefound","installingWorker","installing","onstatechange","state","controller","catch","error","fetch","response","status","headers","get","indexOf","reload"],"mappings":";;;;;kBAoBwBA,Q;QA0FRC,U,GAAAA,U;AA9GhB;;AAEA;AACA;AACA;AACA;;AAEA;AACA;;AAEA,IAAMC,cAAcC,QAClBC,OAAOC,QAAP,CAAgBC,QAAhB,KAA6B,WAA7B;AACE;AACAF,OAAOC,QAAP,CAAgBC,QAAhB,KAA6B,OAF/B;AAGE;AACAF,OAAOC,QAAP,CAAgBC,QAAhB,CAAyBC,KAAzB,CACE,wDADF,CALgB,CAApB;;AAUe,SAASP,QAAT,GAAoB;AACjC,MAAIQ,QAAQC,GAAR,CAAYC,QAAZ,KAAyB,YAAzB,IAAyC,mBAAmBC,SAAhE,EAA2E;AACzE;AACA,QAAMC,YAAY,IAAIC,GAAJ,CAAQL,QAAQC,GAAR,CAAYK,UAApB,EAAgCV,OAAOC,QAAvC,CAAlB;AACA,QAAIO,UAAUG,MAAV,KAAqBX,OAAOC,QAAP,CAAgBU,MAAzC,EAAiD;AAC/C;AACA;AACA;AACA;AACD;;AAEDX,WAAOY,gBAAP,CAAwB,MAAxB,EAAgC,YAAM;AACpC,UAAMC,QAAWT,QAAQC,GAAR,CAAYK,UAAvB,uBAAN;;AAEA,UAAIZ,WAAJ,EAAiB;AACf;AACAgB,gCAAwBD,KAAxB;;AAEA;AACA;AACAN,kBAAUQ,aAAV,CAAwBC,KAAxB,CAA8BC,IAA9B,CAAmC,YAAM;AACvCC,kBAAQC,GAAR,CACE,2DACE,oDAFJ;AAID,SALD;AAMD,OAZD,MAYO;AACL;AACAC,wBAAgBP,KAAhB;AACD;AACF,KA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BU,SAAvB,EAAkC;AAChCA,cAAUQ,aAAV,CAAwBC,KAAxB,CAA8BC,IAA9B,CAAmC,wBAAgB;AACjDI,mBAAaxB,UAAb;AACD,KAFD;AAGD;AACF","file":"registerServiceWorker.js","sourcesContent":["// In production, we register a service worker to serve assets from local cache.\r\n\r\n// This lets the app load faster on subsequent visits in production, and gives\r\n// it offline capabilities. However, it also means that developers (and users)\r\n// will only see deployed updates on the \"N+1\" visit to a page, since previously\r\n// cached resources are updated in the background.\r\n\r\n// To learn more about the benefits of this model, read https://goo.gl/KwvDNy.\r\n// This link also includes instructions on opting out of this behavior.\r\n\r\nconst isLocalhost = Boolean(\r\n window.location.hostname === \"localhost\" ||\r\n // [::1] is the IPv6 localhost address.\r\n window.location.hostname === \"[::1]\" ||\r\n // 127.0.0.1/8 is considered localhost for IPv4.\r\n window.location.hostname.match(\r\n /^127(?:\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/\r\n )\r\n);\r\n\r\nexport default function register() {\r\n if (process.env.NODE_ENV === \"production\" && \"serviceWorker\" in navigator) {\r\n // The URL constructor is available in all browsers that support SW.\r\n const publicUrl = new URL(process.env.PUBLIC_URL, window.location);\r\n if (publicUrl.origin !== window.location.origin) {\r\n // Our service worker won't work if PUBLIC_URL is on a different origin\r\n // from what our page is served on. This might happen if a CDN is used to\r\n // serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374\r\n return;\r\n }\r\n\r\n window.addEventListener(\"load\", () => {\r\n const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;\r\n\r\n if (isLocalhost) {\r\n // This is running on localhost. Lets check if a service worker still exists or not.\r\n checkValidServiceWorker(swUrl);\r\n\r\n // Add some additional logging to localhost, pointing developers to the\r\n // service worker/PWA documentation.\r\n navigator.serviceWorker.ready.then(() => {\r\n console.log(\r\n \"This web app is being served cache-first by a service \" +\r\n \"worker. To learn more, visit https://goo.gl/SC7cgQ\"\r\n );\r\n });\r\n } else {\r\n // Is not local host. Just register service worker\r\n registerValidSW(swUrl);\r\n }\r\n });\r\n }\r\n}\r\n\r\nfunction registerValidSW(swUrl) {\r\n navigator.serviceWorker\r\n .register(swUrl)\r\n .then(registration => {\r\n registration.onupdatefound = () => {\r\n const installingWorker = registration.installing;\r\n installingWorker.onstatechange = () => {\r\n if (installingWorker.state === \"installed\") {\r\n if (navigator.serviceWorker.controller) {\r\n // At this point, the old content will have been purged and\r\n // the fresh content will have been added to the cache.\r\n // It's the perfect time to display a \"New content is\r\n // available; please refresh.\" message in your web app.\r\n console.log(\"New content is available; please refresh.\");\r\n } else {\r\n // At this point, everything has been precached.\r\n // It's the perfect time to display a\r\n // \"Content is cached for offline use.\" message.\r\n console.log(\"Content is cached for offline use.\");\r\n }\r\n }\r\n };\r\n };\r\n })\r\n .catch(error => {\r\n console.error(\"Error during service worker registration:\", error);\r\n });\r\n}\r\n\r\nfunction checkValidServiceWorker(swUrl) {\r\n // Check if the service worker can be found. If it can't reload the page.\r\n fetch(swUrl)\r\n .then(response => {\r\n // Ensure service worker exists, and that we really are getting a JS file.\r\n if (\r\n response.status === 404 ||\r\n response.headers.get(\"content-type\").indexOf(\"javascript\") === -1\r\n ) {\r\n // No service worker found. Probably a different app. Reload the page.\r\n navigator.serviceWorker.ready.then(registration => {\r\n registration.unregister().then(() => {\r\n window.location.reload();\r\n });\r\n });\r\n } else {\r\n // Service worker found. Proceed as normal.\r\n registerValidSW(swUrl);\r\n }\r\n })\r\n .catch(() => {\r\n console.log(\r\n \"No internet connection found. App is running in offline mode.\"\r\n );\r\n });\r\n}\r\n\r\nexport function unregister() {\r\n if (\"serviceWorker\" in navigator) {\r\n navigator.serviceWorker.ready.then(registration => {\r\n registration.unregister();\r\n });\r\n }\r\n}\r\n"]}