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