Skip to content

Commit 9c5d5f9

Browse files
committed
refractor service worker
1 parent c2878ff commit 9c5d5f9

File tree

4 files changed

+39
-37
lines changed

4 files changed

+39
-37
lines changed

src/sw.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { precacheAndRoute } from 'workbox-precaching/precacheAndRoute';
2+
import { registerRoute } from 'workbox-routing/registerRoute';
3+
import { CacheFirst } from 'workbox-strategies';
4+
import { ExpirationPlugin } from 'workbox-expiration/ExpirationPlugin';
5+
6+
// Precache assets built with webpack
7+
precacheAndRoute(self.__WB_MANIFEST);
8+
9+
// Cache Google Fonts
10+
registerRoute(
11+
/https:\/\/fonts\.gstatic\.com/,
12+
new CacheFirst({
13+
cacheName: 'google-fonts-cache',
14+
plugins: [
15+
new ExpirationPlugin({
16+
// Cache for one year
17+
maxAgeSeconds: 60 * 60 * 24 * 365,
18+
maxEntries: 30,
19+
}),
20+
],
21+
})
22+
);
23+
24+
// TODO Cache /app-shell/index.html
25+
26+
// TODO Cache /index.html, /concepts/index.html, etc.

webpack.common.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,17 @@ module.exports = (env = {}) => ({
136136
loader: 'file-loader',
137137
options: {
138138
outputPath: 'font',
139-
esModule: false
139+
esModule: false,
140+
name: '[name].[md4:hash:hex:20].[ext]'
140141
}
141142
}
142143
},
143144
{
144145
test: /\.(jpg|jpeg|png|svg|ico)$/i,
145-
type: 'asset/resource'
146+
type: 'asset/resource',
147+
generator: {
148+
filename: '[name].[hash][ext][query]'
149+
}
146150
}
147151
]
148152
},

webpack.prod.js

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Import External Dependencies
22
const { merge } = require('webpack-merge');
33
const OptimizeCSSAssetsPlugin = require('css-minimizer-webpack-plugin');
4-
const { GenerateSW } = require('workbox-webpack-plugin');
4+
const { InjectManifest } = require('workbox-webpack-plugin');
5+
const path = require('path');
56

67
// Load Common Configuration
78
const common = require('./webpack.common.js');
@@ -43,40 +44,11 @@ module.exports = env => merge(common(env), {
4344
]
4445
},
4546
plugins: [
46-
new GenerateSW({
47-
skipWaiting: true,
48-
clientsClaim: true,
47+
new InjectManifest({
48+
swSrc: path.join(__dirname, 'src/sw.js'),
4949
swDest: 'sw.js',
50-
exclude: [/icon_.*\.png/, /printable/, '/robots.txt', ...hashedAssetsBySSGRun],
51-
additionalManifestEntries: [
52-
{
53-
url: '/app-shell/index.html',
54-
revision: new Date().getTime().toString() // dirty hack
55-
},
56-
{
57-
url: '/manifest.json',
58-
revision: '1'
59-
},
60-
...hashedAssetsBySSGRun.map(url => ({
61-
url: '/' + url, // prepend the publicPath
62-
revision: null
63-
}))
64-
],
65-
navigateFallback: '/app-shell/index.html',
66-
navigateFallbackDenylist: [/printable/],
67-
runtimeCaching: [
68-
{
69-
urlPattern: /https:\/\/fonts\.gstatic\.com/, // cache google fonts for one year
70-
handler: 'CacheFirst',
71-
options: {
72-
cacheName: 'google-fonts',
73-
expiration: {
74-
maxAgeSeconds: 60 * 60 * 24 * 365,
75-
maxEntries: 30
76-
}
77-
}
78-
}
79-
],
50+
// match [name].[contenthash].[ext]
51+
dontCacheBustURLsMatching: /\.[0-9a-f]{20}\./
8052
})
8153
]
8254
});

webpack.ssg.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = env => merge(common(env), {
3434
index: './server.jsx'
3535
},
3636
output: {
37-
filename: '.server/[name].js',
37+
filename: '.server/[name].[contenthash].js',
3838
libraryTarget: 'umd'
3939
},
4040
optimization: {

0 commit comments

Comments
 (0)