Path: blob/main/src/service-worker.js
270 views
/* eslint-disable no-restricted-globals */12// This service worker can be customized!3// See https://developers.google.com/web/tools/workbox/modules4// for the list of available Workbox modules, or add any other5// code you'd like.6// You can also remove this file if you'd prefer not to use a7// service worker, and the Workbox build step will be skipped.89import { clientsClaim } from "workbox-core";10import { ExpirationPlugin } from "workbox-expiration";11import { precacheAndRoute, createHandlerBoundToURL } from "workbox-precaching";12import { registerRoute } from "workbox-routing";13import {14StaleWhileRevalidate,15NetworkFirst,16CacheFirst,17} from "workbox-strategies";18// import { googleFontsCache } from "workbox-recipes";1920// googleFontsCache();2122clientsClaim();2324// Precache all of the assets generated by your build process.25// Their URLs are injected into the manifest variable below.26// This variable must be present somewhere in your service worker file,27// even if you decide not to use precaching. See https://cra.link/PWA28precacheAndRoute(self.__WB_MANIFEST);2930// Set up App Shell-style routing, so that all navigation requests31// are fulfilled with your index.html shell. Learn more at32// https://developers.google.com/web/fundamentals/architecture/app-shell33const fileExtensionRegexp = new RegExp("/[^/?]+\\.[^/]+$");34registerRoute(35// Return false to exempt requests from being fulfilled by index.html.36({ request, url }) => {37// If this isn't a navigation, skip.38if (request.mode !== "navigate") {39return false;40} // If this is a URL that starts with /_, skip.4142if (url.pathname.startsWith("/_")) {43return false;44} // If this looks like a URL for a resource, because it contains // a file extension, skip.4546if (url.pathname.match(fileExtensionRegexp)) {47return false;48} // Return true to signal that we want to use the handler.4950return true;51},52createHandlerBoundToURL(process.env.PUBLIC_URL + "/index.html")53);5455// An example runtime caching route for requests that aren't handled by the56// precache, in this case same-origin .png requests like those from in public/57registerRoute(58// Add in any other file extensions or routing criteria as needed.59({ url }) =>60url.origin === self.location.origin &&61(url.pathname.endsWith(".webp") || url.pathname.endsWith(".png")), // Customize this strategy as needed, e.g., by changing to CacheFirst.62new CacheFirst({63cacheName: "images",64plugins: [65// Ensure that once this runtime cache reaches a maximum size the66// least-recently used images are removed.67new ExpirationPlugin({68maxAgeSeconds: 24 * 60 * 60,69}),70],71})72);7374registerRoute(75// Add in any other file extensions or routing criteria as needed.76({ url }) =>77url.origin === self.location.origin && url.pathname.endsWith(".mp3"), // Customize this strategy as needed, e.g., by changing to CacheFirst.78new CacheFirst({79cacheName: "audios",80plugins: [81// Ensure that once this runtime cache reaches a maximum size the82// least-recently used images are removed.83new ExpirationPlugin({84maxAgeSeconds: 24 * 60 * 60,85}),86],87})88);8990registerRoute(91({ request }) => request.destination === "style",92new StaleWhileRevalidate({93cacheName: "static",94plugins: [new ExpirationPlugin({ maxEntries: 50 })],95})96);9798registerRoute(99({ request }) =>100request.destination === "script" || request.destination === "worker",101new NetworkFirst({102cacheName: "static-script",103plugins: [new ExpirationPlugin({ maxEntries: 50 })],104})105);106107// This allows the web app to trigger skipWaiting via108// registration.waiting.postMessage({type: 'SKIP_WAITING'})109self.addEventListener("message", (event) => {110if (event.data && event.data.type === "SKIP_WAITING") {111self.skipWaiting();112}113});114115// Any other custom service worker logic can go here.116117118