-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
next.config.js
134 lines (123 loc) · 4.6 KB
/
next.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const WorkboxPlugin = require('workbox-webpack-plugin');
const generateSitemap = require('./scripts/sitemap');
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const baseUrl = 'https://pwa-boilerplate.com';
const sitemapDest = path.resolve('.next/static');
const skipIndex = ['/profile'];
const serviceWorkerPath = 'static/sw.js';
const serviceWorkerUrl = `/_next/${serviceWorkerPath}`;
const serviceWorkerDest = `.next/${serviceWorkerPath}`;
module.exports = {
webpack5: true,
reactStrictMode: true,
env: {
serviceWorkerUrl
},
pageExtensions: ['ts', 'tsx'],
excludeFile: (str) => /\/src\/sw\/.*/.test(str),
webpack: (config, { isServer, dev, webpack, buildId }) => {
config.module.rules.push(
{
test: /\.svg$/,
loader: '@svgr/webpack'
},
{
test: /\.html$/,
loader: 'raw-loader'
}
);
if (isServer && !dev) {
generateSitemap({
baseUrl,
skipIndex
}, sitemapDest);
}
if (!isServer) {
const additionalManifestEntries = fs
.readdirSync('public', { withFileTypes: true })
/*
* Add the public files to precache-manifest entries.
*
* We're creating an MD5 hash from file contents
* to know if they've changed, so that the service worker
* would know to recache them if they have.
*/
.reduce((manifest, file) => {
const { name } = file;
// Filter out directories and hidden files.
if (!file.isFile() || name.startsWith('.')) {
return manifest;
}
return [
...manifest,
{
url: `/${name}`,
revision: crypto.createHash('md5').update(
Buffer.from(
fs.readFileSync(`public/${name}`)
)
).digest('hex')
}
];
}, []);
/*
* In development mode pre-cache files up-to 5MB
*/
const maximumFileSizeToCacheInBytes = dev
? 5000000 : undefined;
config.plugins.push(
new WorkboxPlugin.InjectManifest({
swSrc: path.resolve('src', 'sw', 'index.ts'),
swDest: path.resolve(serviceWorkerDest),
dontCacheBustURLsMatching: /^\/_next\/static\//,
maximumFileSizeToCacheInBytes,
additionalManifestEntries,
webpackCompilationPlugins: [
new webpack.DefinePlugin({
'self.__BUILD_ID': JSON.stringify(buildId)
})
],
exclude: [
/*
* Filter out our API route,
* we need this here because our api is a NextJS page,
* and is treated as a static endpoint.
*/
/\/api\//i,
/^build-manifest\.json$/i,
/^react-loadable-manifest\.json$/i,
/\/react-refresh\.js$/i,
/\/_error\.js$/i,
/\.js\.map$/i,
/*
* Since we're using variable fonts
* we don't want to pre-cache any,
* otherwise we're downloading both
* variable & regular fonts.
*/
/\.(ttf|woff)/i
],
modifyURLPrefix: {
'static/': '/_next/static/'
}
})
);
}
return config;
},
headers: () => [{
/*
* Since we're outputing service worker
* with static files in /_next/static directory
* we have to return the service worker file with an additional header
* so that the browser would know that it's safe to run it on the root scope.
*/
source: serviceWorkerUrl,
headers: [{
key: 'service-worker-allowed',
value: '/'
}]
}]
}