Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wellsousaaa
GitHub Repository: wellsousaaa/Five-Nights-at-Freddys-Web
Path: blob/main/src/service-worker.js
270 views
1
/* eslint-disable no-restricted-globals */
2
3
// This service worker can be customized!
4
// See https://developers.google.com/web/tools/workbox/modules
5
// for the list of available Workbox modules, or add any other
6
// code you'd like.
7
// You can also remove this file if you'd prefer not to use a
8
// service worker, and the Workbox build step will be skipped.
9
10
import { clientsClaim } from "workbox-core";
11
import { ExpirationPlugin } from "workbox-expiration";
12
import { precacheAndRoute, createHandlerBoundToURL } from "workbox-precaching";
13
import { registerRoute } from "workbox-routing";
14
import {
15
StaleWhileRevalidate,
16
NetworkFirst,
17
CacheFirst,
18
} from "workbox-strategies";
19
// import { googleFontsCache } from "workbox-recipes";
20
21
// googleFontsCache();
22
23
clientsClaim();
24
25
// Precache all of the assets generated by your build process.
26
// Their URLs are injected into the manifest variable below.
27
// This variable must be present somewhere in your service worker file,
28
// even if you decide not to use precaching. See https://cra.link/PWA
29
precacheAndRoute(self.__WB_MANIFEST);
30
31
// Set up App Shell-style routing, so that all navigation requests
32
// are fulfilled with your index.html shell. Learn more at
33
// https://developers.google.com/web/fundamentals/architecture/app-shell
34
const fileExtensionRegexp = new RegExp("/[^/?]+\\.[^/]+$");
35
registerRoute(
36
// Return false to exempt requests from being fulfilled by index.html.
37
({ request, url }) => {
38
// If this isn't a navigation, skip.
39
if (request.mode !== "navigate") {
40
return false;
41
} // If this is a URL that starts with /_, skip.
42
43
if (url.pathname.startsWith("/_")) {
44
return false;
45
} // If this looks like a URL for a resource, because it contains // a file extension, skip.
46
47
if (url.pathname.match(fileExtensionRegexp)) {
48
return false;
49
} // Return true to signal that we want to use the handler.
50
51
return true;
52
},
53
createHandlerBoundToURL(process.env.PUBLIC_URL + "/index.html")
54
);
55
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/
58
registerRoute(
59
// Add in any other file extensions or routing criteria as needed.
60
({ url }) =>
61
url.origin === self.location.origin &&
62
(url.pathname.endsWith(".webp") || url.pathname.endsWith(".png")), // Customize this strategy as needed, e.g., by changing to CacheFirst.
63
new CacheFirst({
64
cacheName: "images",
65
plugins: [
66
// Ensure that once this runtime cache reaches a maximum size the
67
// least-recently used images are removed.
68
new ExpirationPlugin({
69
maxAgeSeconds: 24 * 60 * 60,
70
}),
71
],
72
})
73
);
74
75
registerRoute(
76
// Add in any other file extensions or routing criteria as needed.
77
({ url }) =>
78
url.origin === self.location.origin && url.pathname.endsWith(".mp3"), // Customize this strategy as needed, e.g., by changing to CacheFirst.
79
new CacheFirst({
80
cacheName: "audios",
81
plugins: [
82
// Ensure that once this runtime cache reaches a maximum size the
83
// least-recently used images are removed.
84
new ExpirationPlugin({
85
maxAgeSeconds: 24 * 60 * 60,
86
}),
87
],
88
})
89
);
90
91
registerRoute(
92
({ request }) => request.destination === "style",
93
new StaleWhileRevalidate({
94
cacheName: "static",
95
plugins: [new ExpirationPlugin({ maxEntries: 50 })],
96
})
97
);
98
99
registerRoute(
100
({ request }) =>
101
request.destination === "script" || request.destination === "worker",
102
new NetworkFirst({
103
cacheName: "static-script",
104
plugins: [new ExpirationPlugin({ maxEntries: 50 })],
105
})
106
);
107
108
// This allows the web app to trigger skipWaiting via
109
// registration.waiting.postMessage({type: 'SKIP_WAITING'})
110
self.addEventListener("message", (event) => {
111
if (event.data && event.data.type === "SKIP_WAITING") {
112
self.skipWaiting();
113
}
114
});
115
116
// Any other custom service worker logic can go here.
117
118