@serwist/next 9.0.5 → 9.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2018 Google LLC, 2019 ShadowWalker w@weiw.io https://weiw.io, 2023 Serwist
3
+ Copyright (c) 2018-2023 Google LLC, 2019-2023 ShadowWalker w@weiw.io https://weiw.io, 2020-2023 Anthony Fu <https://github.com/antfu>, 2023-PRESENT Serwist
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/dist/index.js CHANGED
@@ -45,8 +45,7 @@ const loadTSConfig = (baseDir, relativeTSConfigPath)=>{
45
45
  if (!tsConfigPath) {
46
46
  return undefined;
47
47
  }
48
- const tsConfigFile = JSON.parse(fs.readFileSync(tsConfigPath, "utf-8"));
49
- return tsConfigFile;
48
+ return JSON.parse(fs.readFileSync(tsConfigPath, "utf-8"));
50
49
  } catch {
51
50
  return undefined;
52
51
  }
@@ -1,4 +1,14 @@
1
- import { PAGES_CACHE_NAME } from "./worker/constants.js";
2
- import { defaultCache } from "./worker/defaultCache.js";
3
- export { defaultCache, PAGES_CACHE_NAME };
1
+ import type { RuntimeCaching } from "serwist";
2
+ export declare const PAGES_CACHE_NAME: {
3
+ readonly rscPrefetch: "pages-rsc-prefetch";
4
+ readonly rsc: "pages-rsc";
5
+ readonly html: "pages";
6
+ };
7
+ /**
8
+ * The default, recommended list of caching strategies for applications
9
+ * built with Next.js.
10
+ *
11
+ * @see https://serwist.pages.dev/docs/next/worker-exports#default-cache
12
+ */
13
+ export declare const defaultCache: RuntimeCaching[];
4
14
  //# sourceMappingURL=index.worker.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.worker.d.ts","sourceRoot":"","sources":["../src/index.worker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAExD,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,CAAC"}
1
+ {"version":3,"file":"index.worker.d.ts","sourceRoot":"","sources":["../src/index.worker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAG9C,eAAO,MAAM,gBAAgB;;;;CAInB,CAAC;AAEX;;;;;GAKG;AACH,eAAO,MAAM,YAAY,EAAE,cAAc,EAsQlC,CAAC"}
@@ -5,7 +5,6 @@ const PAGES_CACHE_NAME = {
5
5
  rsc: "pages-rsc",
6
6
  html: "pages"
7
7
  };
8
-
9
8
  const defaultCache = process.env.NODE_ENV !== "production" ? [
10
9
  {
11
10
  matcher: /.*/i,
@@ -1 +1 @@
1
- {"version":3,"file":"load-tsconfig.d.ts","sourceRoot":"","sources":["../../src/lib/load-tsconfig.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,IAAI,YAAY,EAAE,MAAM,WAAW,CAAC;AAI9D,eAAO,MAAM,YAAY,YAAa,MAAM,wBAAwB,MAAM,GAAG,SAAS,KAAG,YAAY,GAAG,SAmBvG,CAAC"}
1
+ {"version":3,"file":"load-tsconfig.d.ts","sourceRoot":"","sources":["../../src/lib/load-tsconfig.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,IAAI,YAAY,EAAE,MAAM,WAAW,CAAC;AAI9D,eAAO,MAAM,YAAY,YAAa,MAAM,wBAAwB,MAAM,GAAG,SAAS,KAAG,YAAY,GAAG,SAevG,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serwist/next",
3
- "version": "9.0.5",
3
+ "version": "9.0.7",
4
4
  "type": "module",
5
5
  "description": "A module that integrates Serwist into your Next.js application.",
6
6
  "files": [
@@ -23,7 +23,7 @@
23
23
  "engines": {
24
24
  "node": ">=18.0.0"
25
25
  },
26
- "author": "Serwist's Team",
26
+ "author": "Serwist <ducanh2912.rusty@gmail.com> (https://serwist.pages.dev/)",
27
27
  "license": "MIT",
28
28
  "repository": "https://github.com/serwist/serwist",
29
29
  "bugs": "https://github.com/serwist/serwist/issues",
@@ -65,22 +65,22 @@
65
65
  "chalk": "5.3.0",
66
66
  "glob": "10.4.5",
67
67
  "zod": "3.23.8",
68
- "@serwist/build": "9.0.5",
69
- "@serwist/webpack-plugin": "9.0.5",
70
- "@serwist/window": "9.0.5",
71
- "serwist": "9.0.5"
68
+ "@serwist/build": "9.0.7",
69
+ "@serwist/webpack-plugin": "9.0.7",
70
+ "@serwist/window": "9.0.7",
71
+ "serwist": "9.0.7"
72
72
  },
73
73
  "devDependencies": {
74
- "@types/node": "20.14.10",
75
- "next": "14.2.5",
74
+ "@types/node": "22.5.0",
75
+ "next": "14.2.6",
76
76
  "react": "18.3.1",
77
77
  "react-dom": "18.3.1",
78
- "rollup": "4.18.1",
79
- "type-fest": "4.21.0",
80
- "typescript": "5.5.3",
78
+ "rollup": "4.21.0",
79
+ "type-fest": "4.25.0",
80
+ "typescript": "5.5.4",
81
81
  "webpack": "5.93.0",
82
- "@serwist/configs": "9.0.5",
83
- "@serwist/utils": "9.0.5"
82
+ "@serwist/configs": "9.0.7",
83
+ "@serwist/utils": "9.0.7"
84
84
  },
85
85
  "peerDependencies": {
86
86
  "next": ">=14.0.0",
@@ -1,4 +1,278 @@
1
- import { PAGES_CACHE_NAME } from "./worker/constants.js";
2
- import { defaultCache } from "./worker/defaultCache.js";
1
+ import type { RuntimeCaching } from "serwist";
2
+ import { CacheFirst, ExpirationPlugin, NetworkFirst, NetworkOnly, RangeRequestsPlugin, StaleWhileRevalidate } from "serwist";
3
3
 
4
- export { defaultCache, PAGES_CACHE_NAME };
4
+ export const PAGES_CACHE_NAME = {
5
+ rscPrefetch: "pages-rsc-prefetch",
6
+ rsc: "pages-rsc",
7
+ html: "pages",
8
+ } as const;
9
+
10
+ /**
11
+ * The default, recommended list of caching strategies for applications
12
+ * built with Next.js.
13
+ *
14
+ * @see https://serwist.pages.dev/docs/next/worker-exports#default-cache
15
+ */
16
+ export const defaultCache: RuntimeCaching[] =
17
+ process.env.NODE_ENV !== "production"
18
+ ? [
19
+ {
20
+ matcher: /.*/i,
21
+ handler: new NetworkOnly(),
22
+ },
23
+ ]
24
+ : [
25
+ {
26
+ matcher: /^https:\/\/fonts\.(?:gstatic)\.com\/.*/i,
27
+ handler: new CacheFirst({
28
+ cacheName: "google-fonts-webfonts",
29
+ plugins: [
30
+ new ExpirationPlugin({
31
+ maxEntries: 4,
32
+ maxAgeSeconds: 365 * 24 * 60 * 60, // 365 days
33
+ maxAgeFrom: "last-used",
34
+ }),
35
+ ],
36
+ }),
37
+ },
38
+ {
39
+ matcher: /^https:\/\/fonts\.(?:googleapis)\.com\/.*/i,
40
+ handler: new StaleWhileRevalidate({
41
+ cacheName: "google-fonts-stylesheets",
42
+ plugins: [
43
+ new ExpirationPlugin({
44
+ maxEntries: 4,
45
+ maxAgeSeconds: 7 * 24 * 60 * 60, // 7 days
46
+ maxAgeFrom: "last-used",
47
+ }),
48
+ ],
49
+ }),
50
+ },
51
+ {
52
+ matcher: /\.(?:eot|otf|ttc|ttf|woff|woff2|font.css)$/i,
53
+ handler: new StaleWhileRevalidate({
54
+ cacheName: "static-font-assets",
55
+ plugins: [
56
+ new ExpirationPlugin({
57
+ maxEntries: 4,
58
+ maxAgeSeconds: 7 * 24 * 60 * 60, // 7 days
59
+ maxAgeFrom: "last-used",
60
+ }),
61
+ ],
62
+ }),
63
+ },
64
+ {
65
+ matcher: /\.(?:jpg|jpeg|gif|png|svg|ico|webp)$/i,
66
+ handler: new StaleWhileRevalidate({
67
+ cacheName: "static-image-assets",
68
+ plugins: [
69
+ new ExpirationPlugin({
70
+ maxEntries: 64,
71
+ maxAgeSeconds: 30 * 24 * 60 * 60, // 30 days
72
+ maxAgeFrom: "last-used",
73
+ }),
74
+ ],
75
+ }),
76
+ },
77
+ {
78
+ matcher: /\/_next\/static.+\.js$/i,
79
+ handler: new CacheFirst({
80
+ cacheName: "next-static-js-assets",
81
+ plugins: [
82
+ new ExpirationPlugin({
83
+ maxEntries: 64,
84
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
85
+ maxAgeFrom: "last-used",
86
+ }),
87
+ ],
88
+ }),
89
+ },
90
+ {
91
+ matcher: /\/_next\/image\?url=.+$/i,
92
+ handler: new StaleWhileRevalidate({
93
+ cacheName: "next-image",
94
+ plugins: [
95
+ new ExpirationPlugin({
96
+ maxEntries: 64,
97
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
98
+ maxAgeFrom: "last-used",
99
+ }),
100
+ ],
101
+ }),
102
+ },
103
+ {
104
+ matcher: /\.(?:mp3|wav|ogg)$/i,
105
+ handler: new CacheFirst({
106
+ cacheName: "static-audio-assets",
107
+ plugins: [
108
+ new ExpirationPlugin({
109
+ maxEntries: 32,
110
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
111
+ maxAgeFrom: "last-used",
112
+ }),
113
+ new RangeRequestsPlugin(),
114
+ ],
115
+ }),
116
+ },
117
+ {
118
+ matcher: /\.(?:mp4|webm)$/i,
119
+ handler: new CacheFirst({
120
+ cacheName: "static-video-assets",
121
+ plugins: [
122
+ new ExpirationPlugin({
123
+ maxEntries: 32,
124
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
125
+ maxAgeFrom: "last-used",
126
+ }),
127
+ new RangeRequestsPlugin(),
128
+ ],
129
+ }),
130
+ },
131
+ {
132
+ matcher: /\.(?:js)$/i,
133
+ handler: new StaleWhileRevalidate({
134
+ cacheName: "static-js-assets",
135
+ plugins: [
136
+ new ExpirationPlugin({
137
+ maxEntries: 48,
138
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
139
+ maxAgeFrom: "last-used",
140
+ }),
141
+ ],
142
+ }),
143
+ },
144
+ {
145
+ matcher: /\.(?:css|less)$/i,
146
+ handler: new StaleWhileRevalidate({
147
+ cacheName: "static-style-assets",
148
+ plugins: [
149
+ new ExpirationPlugin({
150
+ maxEntries: 32,
151
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
152
+ maxAgeFrom: "last-used",
153
+ }),
154
+ ],
155
+ }),
156
+ },
157
+ {
158
+ matcher: /\/_next\/data\/.+\/.+\.json$/i,
159
+ handler: new NetworkFirst({
160
+ cacheName: "next-data",
161
+ plugins: [
162
+ new ExpirationPlugin({
163
+ maxEntries: 32,
164
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
165
+ maxAgeFrom: "last-used",
166
+ }),
167
+ ],
168
+ }),
169
+ },
170
+ {
171
+ matcher: /\.(?:json|xml|csv)$/i,
172
+ handler: new NetworkFirst({
173
+ cacheName: "static-data-assets",
174
+ plugins: [
175
+ new ExpirationPlugin({
176
+ maxEntries: 32,
177
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
178
+ maxAgeFrom: "last-used",
179
+ }),
180
+ ],
181
+ }),
182
+ },
183
+ {
184
+ matcher: ({ sameOrigin, url: { pathname } }) => {
185
+ // Exclude /api/auth/callback/* to fix OAuth workflow in Safari without having
186
+ // an impact on other environments
187
+ // The above route is the default for next-auth, you may need to change it if
188
+ // your OAuth workflow has a different callback route.
189
+ // Issue: https://github.com/shadowwalker/next-pwa/issues/131#issuecomment-821894809
190
+ // TODO(ducanhgh): Investigate Auth.js's "/api/auth/*" failing when we allow them
191
+ // to be cached (the current behaviour).
192
+ if (!sameOrigin || pathname.startsWith("/api/auth/callback")) {
193
+ return false;
194
+ }
195
+
196
+ if (pathname.startsWith("/api/")) {
197
+ return true;
198
+ }
199
+
200
+ return false;
201
+ },
202
+ method: "GET",
203
+ handler: new NetworkFirst({
204
+ cacheName: "apis",
205
+ plugins: [
206
+ new ExpirationPlugin({
207
+ maxEntries: 16,
208
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
209
+ maxAgeFrom: "last-used",
210
+ }),
211
+ ],
212
+ networkTimeoutSeconds: 10, // fallback to cache if API does not response within 10 seconds
213
+ }),
214
+ },
215
+ {
216
+ matcher: ({ request, url: { pathname }, sameOrigin }) =>
217
+ request.headers.get("RSC") === "1" && request.headers.get("Next-Router-Prefetch") === "1" && sameOrigin && !pathname.startsWith("/api/"),
218
+ handler: new NetworkFirst({
219
+ cacheName: PAGES_CACHE_NAME.rscPrefetch,
220
+ plugins: [
221
+ new ExpirationPlugin({
222
+ maxEntries: 32,
223
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
224
+ }),
225
+ ],
226
+ }),
227
+ },
228
+ {
229
+ matcher: ({ request, url: { pathname }, sameOrigin }) => request.headers.get("RSC") === "1" && sameOrigin && !pathname.startsWith("/api/"),
230
+ handler: new NetworkFirst({
231
+ cacheName: PAGES_CACHE_NAME.rsc,
232
+ plugins: [
233
+ new ExpirationPlugin({
234
+ maxEntries: 32,
235
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
236
+ }),
237
+ ],
238
+ }),
239
+ },
240
+ {
241
+ matcher: ({ request, url: { pathname }, sameOrigin }) =>
242
+ request.headers.get("Content-Type")?.includes("text/html") && sameOrigin && !pathname.startsWith("/api/"),
243
+ handler: new NetworkFirst({
244
+ cacheName: PAGES_CACHE_NAME.html,
245
+ plugins: [
246
+ new ExpirationPlugin({
247
+ maxEntries: 32,
248
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
249
+ }),
250
+ ],
251
+ }),
252
+ },
253
+ {
254
+ matcher: ({ url: { pathname }, sameOrigin }) => sameOrigin && !pathname.startsWith("/api/"),
255
+ handler: new NetworkFirst({
256
+ cacheName: "others",
257
+ plugins: [
258
+ new ExpirationPlugin({
259
+ maxEntries: 32,
260
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
261
+ }),
262
+ ],
263
+ }),
264
+ },
265
+ {
266
+ matcher: ({ sameOrigin }) => !sameOrigin,
267
+ handler: new NetworkFirst({
268
+ cacheName: "cross-origin",
269
+ plugins: [
270
+ new ExpirationPlugin({
271
+ maxEntries: 32,
272
+ maxAgeSeconds: 60 * 60, // 1 hour
273
+ }),
274
+ ],
275
+ networkTimeoutSeconds: 10,
276
+ }),
277
+ },
278
+ ];
@@ -12,15 +12,11 @@ export const loadTSConfig = (baseDir: string, relativeTSConfigPath: string | und
12
12
  const resolvedPath = path.join(baseDir, filePath);
13
13
  return fs.existsSync(resolvedPath) ? resolvedPath : undefined;
14
14
  });
15
-
16
15
  if (!tsConfigPath) {
17
16
  return undefined;
18
17
  }
19
-
20
18
  // Read tsconfig.json file
21
- const tsConfigFile = JSON.parse(fs.readFileSync(tsConfigPath, "utf-8"));
22
-
23
- return tsConfigFile;
19
+ return JSON.parse(fs.readFileSync(tsConfigPath, "utf-8"));
24
20
  } catch {
25
21
  return undefined;
26
22
  }
@@ -1,6 +0,0 @@
1
- export declare const PAGES_CACHE_NAME: {
2
- readonly rscPrefetch: "pages-rsc-prefetch";
3
- readonly rsc: "pages-rsc";
4
- readonly html: "pages";
5
- };
6
- //# sourceMappingURL=constants.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/worker/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB;;;;CAInB,CAAC"}
@@ -1,9 +0,0 @@
1
- import type { RuntimeCaching } from "serwist";
2
- /**
3
- * The default, recommended list of caching strategies for applications
4
- * built with Next.js.
5
- *
6
- * @see https://serwist.pages.dev/docs/next/worker-exports#default-cache
7
- */
8
- export declare const defaultCache: RuntimeCaching[];
9
- //# sourceMappingURL=defaultCache.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"defaultCache.d.ts","sourceRoot":"","sources":["../../src/worker/defaultCache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAK9C;;;;;GAKG;AACH,eAAO,MAAM,YAAY,EAAE,cAAc,EAsQlC,CAAC"}
@@ -1,5 +0,0 @@
1
- export const PAGES_CACHE_NAME = {
2
- rscPrefetch: "pages-rsc-prefetch",
3
- rsc: "pages-rsc",
4
- html: "pages",
5
- } as const;
@@ -1,274 +0,0 @@
1
- import type { RuntimeCaching } from "serwist";
2
- import { CacheFirst, ExpirationPlugin, NetworkFirst, NetworkOnly, RangeRequestsPlugin, StaleWhileRevalidate } from "serwist";
3
-
4
- import { PAGES_CACHE_NAME } from "./constants.js";
5
-
6
- /**
7
- * The default, recommended list of caching strategies for applications
8
- * built with Next.js.
9
- *
10
- * @see https://serwist.pages.dev/docs/next/worker-exports#default-cache
11
- */
12
- export const defaultCache: RuntimeCaching[] =
13
- process.env.NODE_ENV !== "production"
14
- ? [
15
- {
16
- matcher: /.*/i,
17
- handler: new NetworkOnly(),
18
- },
19
- ]
20
- : [
21
- {
22
- matcher: /^https:\/\/fonts\.(?:gstatic)\.com\/.*/i,
23
- handler: new CacheFirst({
24
- cacheName: "google-fonts-webfonts",
25
- plugins: [
26
- new ExpirationPlugin({
27
- maxEntries: 4,
28
- maxAgeSeconds: 365 * 24 * 60 * 60, // 365 days
29
- maxAgeFrom: "last-used",
30
- }),
31
- ],
32
- }),
33
- },
34
- {
35
- matcher: /^https:\/\/fonts\.(?:googleapis)\.com\/.*/i,
36
- handler: new StaleWhileRevalidate({
37
- cacheName: "google-fonts-stylesheets",
38
- plugins: [
39
- new ExpirationPlugin({
40
- maxEntries: 4,
41
- maxAgeSeconds: 7 * 24 * 60 * 60, // 7 days
42
- maxAgeFrom: "last-used",
43
- }),
44
- ],
45
- }),
46
- },
47
- {
48
- matcher: /\.(?:eot|otf|ttc|ttf|woff|woff2|font.css)$/i,
49
- handler: new StaleWhileRevalidate({
50
- cacheName: "static-font-assets",
51
- plugins: [
52
- new ExpirationPlugin({
53
- maxEntries: 4,
54
- maxAgeSeconds: 7 * 24 * 60 * 60, // 7 days
55
- maxAgeFrom: "last-used",
56
- }),
57
- ],
58
- }),
59
- },
60
- {
61
- matcher: /\.(?:jpg|jpeg|gif|png|svg|ico|webp)$/i,
62
- handler: new StaleWhileRevalidate({
63
- cacheName: "static-image-assets",
64
- plugins: [
65
- new ExpirationPlugin({
66
- maxEntries: 64,
67
- maxAgeSeconds: 30 * 24 * 60 * 60, // 30 days
68
- maxAgeFrom: "last-used",
69
- }),
70
- ],
71
- }),
72
- },
73
- {
74
- matcher: /\/_next\/static.+\.js$/i,
75
- handler: new CacheFirst({
76
- cacheName: "next-static-js-assets",
77
- plugins: [
78
- new ExpirationPlugin({
79
- maxEntries: 64,
80
- maxAgeSeconds: 24 * 60 * 60, // 24 hours
81
- maxAgeFrom: "last-used",
82
- }),
83
- ],
84
- }),
85
- },
86
- {
87
- matcher: /\/_next\/image\?url=.+$/i,
88
- handler: new StaleWhileRevalidate({
89
- cacheName: "next-image",
90
- plugins: [
91
- new ExpirationPlugin({
92
- maxEntries: 64,
93
- maxAgeSeconds: 24 * 60 * 60, // 24 hours
94
- maxAgeFrom: "last-used",
95
- }),
96
- ],
97
- }),
98
- },
99
- {
100
- matcher: /\.(?:mp3|wav|ogg)$/i,
101
- handler: new CacheFirst({
102
- cacheName: "static-audio-assets",
103
- plugins: [
104
- new ExpirationPlugin({
105
- maxEntries: 32,
106
- maxAgeSeconds: 24 * 60 * 60, // 24 hours
107
- maxAgeFrom: "last-used",
108
- }),
109
- new RangeRequestsPlugin(),
110
- ],
111
- }),
112
- },
113
- {
114
- matcher: /\.(?:mp4|webm)$/i,
115
- handler: new CacheFirst({
116
- cacheName: "static-video-assets",
117
- plugins: [
118
- new ExpirationPlugin({
119
- maxEntries: 32,
120
- maxAgeSeconds: 24 * 60 * 60, // 24 hours
121
- maxAgeFrom: "last-used",
122
- }),
123
- new RangeRequestsPlugin(),
124
- ],
125
- }),
126
- },
127
- {
128
- matcher: /\.(?:js)$/i,
129
- handler: new StaleWhileRevalidate({
130
- cacheName: "static-js-assets",
131
- plugins: [
132
- new ExpirationPlugin({
133
- maxEntries: 48,
134
- maxAgeSeconds: 24 * 60 * 60, // 24 hours
135
- maxAgeFrom: "last-used",
136
- }),
137
- ],
138
- }),
139
- },
140
- {
141
- matcher: /\.(?:css|less)$/i,
142
- handler: new StaleWhileRevalidate({
143
- cacheName: "static-style-assets",
144
- plugins: [
145
- new ExpirationPlugin({
146
- maxEntries: 32,
147
- maxAgeSeconds: 24 * 60 * 60, // 24 hours
148
- maxAgeFrom: "last-used",
149
- }),
150
- ],
151
- }),
152
- },
153
- {
154
- matcher: /\/_next\/data\/.+\/.+\.json$/i,
155
- handler: new NetworkFirst({
156
- cacheName: "next-data",
157
- plugins: [
158
- new ExpirationPlugin({
159
- maxEntries: 32,
160
- maxAgeSeconds: 24 * 60 * 60, // 24 hours
161
- maxAgeFrom: "last-used",
162
- }),
163
- ],
164
- }),
165
- },
166
- {
167
- matcher: /\.(?:json|xml|csv)$/i,
168
- handler: new NetworkFirst({
169
- cacheName: "static-data-assets",
170
- plugins: [
171
- new ExpirationPlugin({
172
- maxEntries: 32,
173
- maxAgeSeconds: 24 * 60 * 60, // 24 hours
174
- maxAgeFrom: "last-used",
175
- }),
176
- ],
177
- }),
178
- },
179
- {
180
- matcher: ({ sameOrigin, url: { pathname } }) => {
181
- // Exclude /api/auth/callback/* to fix OAuth workflow in Safari without having
182
- // an impact on other environments
183
- // The above route is the default for next-auth, you may need to change it if
184
- // your OAuth workflow has a different callback route.
185
- // Issue: https://github.com/shadowwalker/next-pwa/issues/131#issuecomment-821894809
186
- // TODO(ducanhgh): Investigate Auth.js's "/api/auth/*" failing when we allow them
187
- // to be cached (the current behaviour).
188
- if (!sameOrigin || pathname.startsWith("/api/auth/callback")) {
189
- return false;
190
- }
191
-
192
- if (pathname.startsWith("/api/")) {
193
- return true;
194
- }
195
-
196
- return false;
197
- },
198
- method: "GET",
199
- handler: new NetworkFirst({
200
- cacheName: "apis",
201
- plugins: [
202
- new ExpirationPlugin({
203
- maxEntries: 16,
204
- maxAgeSeconds: 24 * 60 * 60, // 24 hours
205
- maxAgeFrom: "last-used",
206
- }),
207
- ],
208
- networkTimeoutSeconds: 10, // fallback to cache if API does not response within 10 seconds
209
- }),
210
- },
211
- {
212
- matcher: ({ request, url: { pathname }, sameOrigin }) =>
213
- request.headers.get("RSC") === "1" && request.headers.get("Next-Router-Prefetch") === "1" && sameOrigin && !pathname.startsWith("/api/"),
214
- handler: new NetworkFirst({
215
- cacheName: PAGES_CACHE_NAME.rscPrefetch,
216
- plugins: [
217
- new ExpirationPlugin({
218
- maxEntries: 32,
219
- maxAgeSeconds: 24 * 60 * 60, // 24 hours
220
- }),
221
- ],
222
- }),
223
- },
224
- {
225
- matcher: ({ request, url: { pathname }, sameOrigin }) => request.headers.get("RSC") === "1" && sameOrigin && !pathname.startsWith("/api/"),
226
- handler: new NetworkFirst({
227
- cacheName: PAGES_CACHE_NAME.rsc,
228
- plugins: [
229
- new ExpirationPlugin({
230
- maxEntries: 32,
231
- maxAgeSeconds: 24 * 60 * 60, // 24 hours
232
- }),
233
- ],
234
- }),
235
- },
236
- {
237
- matcher: ({ request, url: { pathname }, sameOrigin }) =>
238
- request.headers.get("Content-Type")?.includes("text/html") && sameOrigin && !pathname.startsWith("/api/"),
239
- handler: new NetworkFirst({
240
- cacheName: PAGES_CACHE_NAME.html,
241
- plugins: [
242
- new ExpirationPlugin({
243
- maxEntries: 32,
244
- maxAgeSeconds: 24 * 60 * 60, // 24 hours
245
- }),
246
- ],
247
- }),
248
- },
249
- {
250
- matcher: ({ url: { pathname }, sameOrigin }) => sameOrigin && !pathname.startsWith("/api/"),
251
- handler: new NetworkFirst({
252
- cacheName: "others",
253
- plugins: [
254
- new ExpirationPlugin({
255
- maxEntries: 32,
256
- maxAgeSeconds: 24 * 60 * 60, // 24 hours
257
- }),
258
- ],
259
- }),
260
- },
261
- {
262
- matcher: ({ sameOrigin }) => !sameOrigin,
263
- handler: new NetworkFirst({
264
- cacheName: "cross-origin",
265
- plugins: [
266
- new ExpirationPlugin({
267
- maxEntries: 32,
268
- maxAgeSeconds: 60 * 60, // 1 hour
269
- }),
270
- ],
271
- networkTimeoutSeconds: 10,
272
- }),
273
- },
274
- ];