1
/// <reference lib="webworker" />
2
/* eslint-disable no-restricted-globals */
4
// This service worker can be customized!
5
// See https://developers.google.com/web/tools/workbox/modules
6
// for the list of available Workbox modules, or add any other
8
// You can also remove this file if you'd prefer not to use a
9
// service worker, and the Workbox build step will be skipped.
11
import { clientsClaim } from 'workbox-core'
12
import { ExpirationPlugin } from 'workbox-expiration'
13
import { precacheAndRoute, createHandlerBoundToURL } from 'workbox-precaching'
14
import { registerRoute } from 'workbox-routing'
15
import { StaleWhileRevalidate } from 'workbox-strategies'
17
declare const self: ServiceWorkerGlobalScope
21
// Precache all of the assets generated by your build process.
22
// Their URLs are injected into the manifest variable below.
23
// This variable must be present somewhere in your service worker file,
24
// even if you decide not to use precaching. See https://cra.link/PWA
25
precacheAndRoute(self.__WB_MANIFEST)
27
// Set up App Shell-style routing, so that all navigation requests
28
// are fulfilled with your index.html shell. Learn more at
29
// https://developers.google.com/web/fundamentals/architecture/app-shell
30
const fileExtensionRegexp = new RegExp('/[^/?]+\\.[^/]+$')
32
// Return false to exempt requests from being fulfilled by index.html.
33
({ request, url }: { request: Request; url: URL }) => {
34
// If this isn't a navigation, skip.
35
if (request.mode !== 'navigate') {
39
// If this is a URL that starts with /_, skip.
40
if (url.pathname.startsWith('/_')) {
44
// If this looks like a URL for a resource, because it contains
45
// a file extension, skip.
46
if (url.pathname.match(fileExtensionRegexp)) {
50
// Return true to signal that we want to use the handler.
53
createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html')
56
// An example runtime caching route for requests that aren't handled by the
57
// precache, in this case same-origin .png requests like those from in public/
59
// Add in any other file extensions or routing criteria as needed.
60
({ url }) => url.origin === self.location.origin && url.pathname.endsWith('.png'),
61
// Customize this strategy as needed, e.g., by changing to CacheFirst.
62
new StaleWhileRevalidate({
65
// Ensure that once this runtime cache reaches a maximum size the
66
// least-recently used images are removed.
67
new ExpirationPlugin({ maxEntries: 50 }),
72
// This allows the web app to trigger skipWaiting via
73
// registration.waiting.postMessage({type: 'SKIP_WAITING'})
74
self.addEventListener('message', (event) => {
75
if (event.data && event.data.type === 'SKIP_WAITING') {
80
// Any other custom service worker logic can go here.