Skip to content

Commit

Permalink
WIP: upgrade to Workbox v6 and TypeScript service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
caleb531 committed Dec 25, 2021
1 parent a153204 commit 815fed4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
File renamed without changes.
6 changes: 6 additions & 0 deletions src/scripts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../generic-tsconfig.json",
"references": [
{"path": "../service-worker"}
]
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');
import { CacheableResponsePlugin } from 'workbox-cacheable-response';
import { ExpirationPlugin } from 'workbox-expiration';
import { cleanupOutdatedCaches, precacheAndRoute } from 'workbox-precaching';
import { registerRoute } from 'workbox-routing';
import { CacheFirst } from 'workbox-strategies';

// The following call will be populated automatically with the precached file
// data during the build step
workbox.precaching.precacheAndRoute(self.__WB_MANIFEST);
cleanupOutdatedCaches();
precacheAndRoute(self.__WB_MANIFEST);

// Cache Google fonts
workbox.routing.registerRoute(new RegExp('^https://fonts.(?:googleapis|gstatic).com/(.*)'), new workbox.strategies.CacheFirst({
registerRoute(new RegExp('^https://fonts.(?:googleapis|gstatic).com/(.*)'), new CacheFirst({
cacheName: 'google-fonts',
plugins: [
// When the cap is reached, the oldest entries are purged
new workbox.expiration.ExpirationPlugin({
new ExpirationPlugin({
maxEntries: 30
}),
// The Google Fonts CSS response is an opaque (non-CORS) response with a
// status code of 0, so we need to enable caching for that type of response
new workbox.cacheableResponse.CacheableResponsePlugin({
new CacheableResponsePlugin({
statuses: [0, 200]
})
]
Expand Down
7 changes: 7 additions & 0 deletions src/service-worker/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../generic-tsconfig.json",
"compilerOptions": {
"lib": ["esnext", "webworker"],
"moduleResolution": "node"
}
}

0 comments on commit 815fed4

Please sign in to comment.