Skip to content

Commit bfbb017

Browse files
authored
fix: avoid images being cached, fix #98 (#100)
1 parent 3fd5cc2 commit bfbb017

File tree

4 files changed

+404
-203
lines changed

4 files changed

+404
-203
lines changed

next.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
const withPWA = require('next-pwa')
2+
const cache = require('./scripts/cache')
23

34
module.exports = withPWA({
45
target: process.env.NETLIFY ? 'serverless' : 'server',
56

67
pwa: {
78
disable: process.env.NODE_ENV === 'development',
89
dest: 'public',
10+
runtimeCaching: cache
911
},
1012
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
"i18n-extract": "^0.6.7",
105105
"jest": "^27.1.0",
106106
"lint-staged": "^10.5.4",
107-
"next-pwa": "^5.2.21",
107+
"next-pwa": "^5.3.1",
108108
"nightwind": "^1.1.6",
109109
"postcss": "^8.2.6",
110110
"prettier": "^2.2.1",

scripts/cache.js

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
'use strict'
2+
3+
// Workbox RuntimeCaching config: https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-build#.RuntimeCachingEntry
4+
module.exports = [
5+
{
6+
urlPattern: /^https:\/\/fonts\.(?:gstatic)\.com\/.*/i,
7+
handler: 'CacheFirst',
8+
options: {
9+
cacheName: 'google-fonts-webfonts',
10+
expiration: {
11+
maxEntries: 4,
12+
maxAgeSeconds: 365 * 24 * 60 * 60, // 365 days
13+
},
14+
},
15+
},
16+
{
17+
urlPattern: /^https:\/\/fonts\.(?:googleapis)\.com\/.*/i,
18+
handler: 'StaleWhileRevalidate',
19+
options: {
20+
cacheName: 'google-fonts-stylesheets',
21+
expiration: {
22+
maxEntries: 4,
23+
maxAgeSeconds: 7 * 24 * 60 * 60, // 7 days
24+
},
25+
},
26+
},
27+
{
28+
urlPattern: /\.(?:eot|otf|ttc|ttf|woff|woff2|font.css)$/i,
29+
handler: 'StaleWhileRevalidate',
30+
options: {
31+
cacheName: 'static-font-assets',
32+
expiration: {
33+
maxEntries: 4,
34+
maxAgeSeconds: 7 * 24 * 60 * 60 // 7 days
35+
}
36+
}
37+
},
38+
{
39+
urlPattern: /\.(?:jpg|jpeg|gif|png|svg|ico|webp)$/i,
40+
handler: 'NetworkOnly'
41+
},
42+
{
43+
urlPattern: /\/_next\/image\?url=.+$/i,
44+
handler: "StaleWhileRevalidate",
45+
options: {
46+
cacheName: "next-image",
47+
expiration: {
48+
maxEntries: 64,
49+
maxAgeSeconds: 24 * 60 * 60, // 24 hours
50+
},
51+
},
52+
},
53+
{
54+
urlPattern: /\.(?:mp3|wav|ogg)$/i,
55+
handler: 'CacheFirst',
56+
options: {
57+
rangeRequests: true,
58+
cacheName: 'static-audio-assets',
59+
expiration: {
60+
maxEntries: 32,
61+
maxAgeSeconds: 24 * 60 * 60 // 24 hours
62+
}
63+
}
64+
},
65+
{
66+
urlPattern: /\.(?:mp4)$/i,
67+
handler: 'CacheFirst',
68+
options: {
69+
rangeRequests: true,
70+
cacheName: 'static-video-assets',
71+
expiration: {
72+
maxEntries: 32,
73+
maxAgeSeconds: 24 * 60 * 60 // 24 hours
74+
}
75+
}
76+
},
77+
{
78+
urlPattern: /\.(?:js)$/i,
79+
handler: 'StaleWhileRevalidate',
80+
options: {
81+
cacheName: 'static-js-assets',
82+
expiration: {
83+
maxEntries: 32,
84+
maxAgeSeconds: 24 * 60 * 60 // 24 hours
85+
}
86+
}
87+
},
88+
{
89+
urlPattern: /\.(?:css|less)$/i,
90+
handler: 'StaleWhileRevalidate',
91+
options: {
92+
cacheName: 'static-style-assets',
93+
expiration: {
94+
maxEntries: 32,
95+
maxAgeSeconds: 24 * 60 * 60 // 24 hours
96+
}
97+
}
98+
},
99+
{
100+
urlPattern: /\/_next\/data\/.+\/.+\.json$/i,
101+
handler: "StaleWhileRevalidate",
102+
options: {
103+
cacheName: "next-data",
104+
expiration: {
105+
maxEntries: 32,
106+
maxAgeSeconds: 24 * 60 * 60, // 24 hours
107+
}
108+
},
109+
},
110+
{
111+
urlPattern: /\.(?:json|xml|csv)$/i,
112+
handler: 'NetworkFirst',
113+
options: {
114+
cacheName: 'static-data-assets',
115+
expiration: {
116+
maxEntries: 32,
117+
maxAgeSeconds: 24 * 60 * 60 // 24 hours
118+
}
119+
}
120+
},
121+
{
122+
urlPattern: ({url}) => {
123+
const isSameOrigin = self.origin === url.origin
124+
if (!isSameOrigin) return false
125+
const pathname = url.pathname
126+
// Exclude /api/auth/callback/* to fix OAuth workflow in Safari without impact other environment
127+
// Above route is default for next-auth, you may need to change it if your OAuth workflow has a different callback route
128+
// Issue: https://github.com/shadowwalker/next-pwa/issues/131#issuecomment-821894809
129+
if (pathname.startsWith('/api/auth/')) return false
130+
if (pathname.startsWith('/api/')) return true
131+
return false
132+
},
133+
handler: 'NetworkFirst',
134+
method: 'GET',
135+
options: {
136+
cacheName: 'apis',
137+
expiration: {
138+
maxEntries: 16,
139+
maxAgeSeconds: 24 * 60 * 60 // 24 hours
140+
},
141+
networkTimeoutSeconds: 10 // fall back to cache if api does not response within 10 seconds
142+
}
143+
},
144+
{
145+
urlPattern: ({url}) => {
146+
const isSameOrigin = self.origin === url.origin
147+
if (!isSameOrigin) return false
148+
const pathname = url.pathname
149+
if (pathname.startsWith('/api/')) return false
150+
return true
151+
},
152+
handler: 'NetworkFirst',
153+
options: {
154+
cacheName: 'others',
155+
expiration: {
156+
maxEntries: 32,
157+
maxAgeSeconds: 24 * 60 * 60 // 24 hours
158+
},
159+
networkTimeoutSeconds: 10
160+
}
161+
},
162+
{
163+
urlPattern: ({url}) => {
164+
const isSameOrigin = self.origin === url.origin
165+
return !isSameOrigin
166+
},
167+
handler: 'NetworkFirst',
168+
options: {
169+
cacheName: 'cross-origin',
170+
expiration: {
171+
maxEntries: 32,
172+
maxAgeSeconds: 60 * 60 // 1 hour
173+
},
174+
networkTimeoutSeconds: 10
175+
}
176+
}
177+
]

0 commit comments

Comments
 (0)