Path: blob/main/src/serviceWorker.js
270 views
// This optional code is used to register a service worker.1// register() is not called by default.23// This lets the app load faster on subsequent visits in production, and gives4// it offline capabilities. However, it also means that developers (and users)5// will only see deployed updates on subsequent visits to a page, after all the6// existing tabs open on the page have been closed, since previously cached7// resources are updated in the background.89// To learn more about the benefits of this model and instructions on how to10// opt-in, read https://cra.link/PWA1112const isLocalhost = Boolean(13window.location.hostname === "localhost" ||14// [::1] is the IPv6 localhost address.15window.location.hostname === "[::1]" ||16// 127.0.0.0/8 are considered localhost for IPv4.17window.location.hostname.match(18/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/19)20);2122export function register(config) {23if (process.env.NODE_ENV === "production" && "serviceWorker" in navigator) {24// The URL constructor is available in all browsers that support SW.25const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);26if (publicUrl.origin !== window.location.origin) {27// Our service worker won't work if PUBLIC_URL is on a different origin28// from what our page is served on. This might happen if a CDN is used to29// serve assets; see https://github.com/facebook/create-react-app/issues/237430return;31}3233window.addEventListener("load", () => {34const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;3536if (isLocalhost) {37// This is running on localhost. Let's check if a service worker still exists or not.38checkValidServiceWorker(swUrl, config);3940// Add some additional logging to localhost, pointing developers to the41// service worker/PWA documentation.42navigator.serviceWorker.ready.then(() => {43console.log(44"This web app is being served cache-first by a service " +45"worker. To learn more, visit https://cra.link/PWA"46);47});48} else {49// Is not localhost. Just register service worker50registerValidSW(swUrl, config);51}52});53}54}5556function registerValidSW(swUrl, config) {57navigator.serviceWorker58.register(swUrl)59.then((registration) => {60registration.onupdatefound = () => {61const installingWorker = registration.installing;62if (installingWorker == null) {63return;64}65installingWorker.onstatechange = () => {66if (installingWorker.state === "installed") {67if (navigator.serviceWorker.controller) {68// At this point, the updated precached content has been fetched,69// but the previous service worker will still serve the older70// content until all client tabs are closed.71console.log(72"New content is available and will be used when all " +73"tabs for this page are closed. See https://cra.link/PWA."74);7576// Execute callback77if (config && config.onUpdate) {78config.onUpdate(registration);79}80} else {81// At this point, everything has been precached.82// It's the perfect time to display a83// "Content is cached for offline use." message.84console.log("Content is cached for offline use.");8586// Execute callback87if (config && config.onSuccess) {88config.onSuccess(registration);89}90}91}92};93};94})95.catch((error) => {96console.error("Error during service worker registration:", error);97});98}99100function checkValidServiceWorker(swUrl, config) {101// Check if the service worker can be found. If it can't reload the page.102fetch(swUrl, {103headers: { "Service-Worker": "script" },104})105.then((response) => {106// Ensure service worker exists, and that we really are getting a JS file.107const contentType = response.headers.get("content-type");108if (109response.status === 404 ||110(contentType != null && contentType.indexOf("javascript") === -1)111) {112// No service worker found. Probably a different app. Reload the page.113navigator.serviceWorker.ready.then((registration) => {114registration.unregister().then(() => {115window.location.reload();116});117});118} else {119// Service worker found. Proceed as normal.120registerValidSW(swUrl, config);121}122})123.catch(() => {124console.log(125"No internet connection found. App is running in offline mode."126);127});128}129130export function unregister() {131if ("serviceWorker" in navigator) {132navigator.serviceWorker.ready133.then((registration) => {134registration.unregister();135})136.catch((error) => {137console.error(error.message);138});139}140}141142143