@serwist/next 8.4.3 → 9.0.0-preview.0

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.
Files changed (67) hide show
  1. package/dist/index.d.ts +5 -4
  2. package/dist/index.d.ts.map +1 -0
  3. package/dist/index.js +51 -43
  4. package/dist/index.worker.d.ts +5 -0
  5. package/dist/index.worker.d.ts.map +1 -0
  6. package/dist/{index.browser.cjs → index.worker.js} +57 -22
  7. package/dist/internal-types.d.ts +6 -0
  8. package/dist/internal-types.d.ts.map +1 -0
  9. package/dist/sw-entry-worker.d.ts +1 -0
  10. package/dist/sw-entry-worker.d.ts.map +1 -0
  11. package/dist/sw-entry.d.ts +1 -0
  12. package/dist/sw-entry.d.ts.map +1 -0
  13. package/dist/utils/find-first-truthy.d.ts +1 -0
  14. package/dist/utils/find-first-truthy.d.ts.map +1 -0
  15. package/dist/utils/get-content-hash.d.ts +1 -0
  16. package/dist/utils/get-content-hash.d.ts.map +1 -0
  17. package/dist/utils/get-file-hash.d.ts +1 -0
  18. package/dist/utils/get-file-hash.d.ts.map +1 -0
  19. package/dist/utils/get-package-version.d.ts +1 -0
  20. package/dist/utils/get-package-version.d.ts.map +1 -0
  21. package/dist/utils/index.d.ts +1 -0
  22. package/dist/utils/index.d.ts.map +1 -0
  23. package/dist/utils/load-tsconfig.d.ts +1 -0
  24. package/dist/utils/load-tsconfig.d.ts.map +1 -0
  25. package/dist/utils/logger.d.ts +1 -0
  26. package/dist/utils/logger.d.ts.map +1 -0
  27. package/dist/utils.d.ts +1 -0
  28. package/dist/utils.d.ts.map +1 -0
  29. package/dist/worker/defaultCache.d.ts +3 -0
  30. package/dist/worker/defaultCache.d.ts.map +1 -0
  31. package/dist/worker/definePageRuntimeCaching.d.ts +16 -0
  32. package/dist/worker/definePageRuntimeCaching.d.ts.map +1 -0
  33. package/package.json +47 -49
  34. package/src/index.ts +241 -0
  35. package/src/index.worker.ts +5 -0
  36. package/src/internal-types.ts +17 -0
  37. package/src/sw-entry-worker.ts +46 -0
  38. package/src/sw-entry.ts +64 -0
  39. package/src/utils/find-first-truthy.ts +15 -0
  40. package/src/utils/get-content-hash.ts +10 -0
  41. package/src/utils/get-file-hash.ts +4 -0
  42. package/src/utils/get-package-version.ts +16 -0
  43. package/src/utils/load-tsconfig.ts +27 -0
  44. package/src/utils/logger.ts +57 -0
  45. package/src/utils.ts +11 -0
  46. package/src/worker/defaultCache.ts +223 -0
  47. package/src/worker/definePageRuntimeCaching.ts +36 -0
  48. package/dist/index.browser.d.cts +0 -2
  49. package/dist/index.browser.d.ts +0 -2
  50. package/dist/index.browser.js +0 -208
  51. package/dist/index.cjs +0 -933
  52. package/dist/index.d.cts +0 -5
  53. package/dist/internal-types.d.cts +0 -9
  54. package/dist/sw-entry-worker.cjs +0 -35
  55. package/dist/sw-entry-worker.d.cts +0 -7
  56. package/dist/sw-entry.cjs +0 -43
  57. package/dist/sw-entry.d.cts +0 -6
  58. package/dist/types.d.cts +0 -81
  59. package/dist/types.d.ts +0 -81
  60. package/dist/utils/find-first-truthy.d.cts +0 -7
  61. package/dist/utils/get-content-hash.d.cts +0 -3
  62. package/dist/utils/get-file-hash.d.cts +0 -3
  63. package/dist/utils/get-package-version.d.cts +0 -6
  64. package/dist/utils/load-tsconfig.d.cts +0 -2
  65. package/dist/utils/logger.d.cts +0 -5
  66. package/dist/utils.d.cts +0 -0
  67. /package/{dist/utils/index.d.cts → src/utils/index.ts} +0 -0
@@ -0,0 +1,223 @@
1
+ import type { RuntimeCaching } from "@serwist/sw";
2
+ import { definePageRuntimeCaching } from "./definePageRuntimeCaching.js";
3
+
4
+ // Serwist RuntimeCaching config: https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-build#.RuntimeCachingEntry
5
+ export const defaultCache = [
6
+ {
7
+ urlPattern: /^https:\/\/fonts\.(?:gstatic)\.com\/.*/i,
8
+ handler: "CacheFirst",
9
+ options: {
10
+ cacheName: "google-fonts-webfonts",
11
+ expiration: {
12
+ maxEntries: 4,
13
+ maxAgeSeconds: 365 * 24 * 60 * 60, // 365 days
14
+ },
15
+ },
16
+ },
17
+ {
18
+ urlPattern: /^https:\/\/fonts\.(?:googleapis)\.com\/.*/i,
19
+ handler: "StaleWhileRevalidate",
20
+ options: {
21
+ cacheName: "google-fonts-stylesheets",
22
+ expiration: {
23
+ maxEntries: 4,
24
+ maxAgeSeconds: 7 * 24 * 60 * 60, // 7 days
25
+ },
26
+ },
27
+ },
28
+ {
29
+ urlPattern: /\.(?:eot|otf|ttc|ttf|woff|woff2|font.css)$/i,
30
+ handler: "StaleWhileRevalidate",
31
+ options: {
32
+ cacheName: "static-font-assets",
33
+ expiration: {
34
+ maxEntries: 4,
35
+ maxAgeSeconds: 7 * 24 * 60 * 60, // 7 days
36
+ },
37
+ },
38
+ },
39
+ {
40
+ urlPattern: /\.(?:jpg|jpeg|gif|png|svg|ico|webp)$/i,
41
+ handler: "StaleWhileRevalidate",
42
+ options: {
43
+ cacheName: "static-image-assets",
44
+ expiration: {
45
+ maxEntries: 64,
46
+ maxAgeSeconds: 30 * 24 * 60 * 60, // 30 days
47
+ },
48
+ },
49
+ },
50
+ {
51
+ urlPattern: /\/_next\/static.+\.js$/i,
52
+ handler: "CacheFirst",
53
+ options: {
54
+ cacheName: "next-static-js-assets",
55
+ expiration: {
56
+ maxEntries: 64,
57
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
58
+ },
59
+ },
60
+ },
61
+ {
62
+ urlPattern: /\/_next\/image\?url=.+$/i,
63
+ handler: "StaleWhileRevalidate",
64
+ options: {
65
+ cacheName: "next-image",
66
+ expiration: {
67
+ maxEntries: 64,
68
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
69
+ },
70
+ },
71
+ },
72
+ {
73
+ urlPattern: /\.(?:mp3|wav|ogg)$/i,
74
+ handler: "CacheFirst",
75
+ options: {
76
+ rangeRequests: true,
77
+ cacheName: "static-audio-assets",
78
+ expiration: {
79
+ maxEntries: 32,
80
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
81
+ },
82
+ },
83
+ },
84
+ {
85
+ urlPattern: /\.(?:mp4|webm)$/i,
86
+ handler: "CacheFirst",
87
+ options: {
88
+ rangeRequests: true,
89
+ cacheName: "static-video-assets",
90
+ expiration: {
91
+ maxEntries: 32,
92
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
93
+ },
94
+ },
95
+ },
96
+ {
97
+ urlPattern: /\.(?:js)$/i,
98
+ handler: "StaleWhileRevalidate",
99
+ options: {
100
+ cacheName: "static-js-assets",
101
+ expiration: {
102
+ maxEntries: 48,
103
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
104
+ },
105
+ },
106
+ },
107
+ {
108
+ urlPattern: /\.(?:css|less)$/i,
109
+ handler: "StaleWhileRevalidate",
110
+ options: {
111
+ cacheName: "static-style-assets",
112
+ expiration: {
113
+ maxEntries: 32,
114
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
115
+ },
116
+ },
117
+ },
118
+ {
119
+ urlPattern: /\/_next\/data\/.+\/.+\.json$/i,
120
+ handler: "StaleWhileRevalidate",
121
+ options: {
122
+ cacheName: "next-data",
123
+ expiration: {
124
+ maxEntries: 32,
125
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
126
+ },
127
+ },
128
+ },
129
+ {
130
+ urlPattern: /\.(?:json|xml|csv)$/i,
131
+ handler: "NetworkFirst",
132
+ options: {
133
+ cacheName: "static-data-assets",
134
+ expiration: {
135
+ maxEntries: 32,
136
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
137
+ },
138
+ },
139
+ },
140
+ {
141
+ urlPattern: ({ sameOrigin, url: { pathname } }) => {
142
+ // Exclude /api/auth/callback/* to fix OAuth workflow in Safari without having an impact on other environments
143
+ // The above route is the default for next-auth, you may need to change it if your OAuth workflow has a different callback route
144
+ // Issue: https://github.com/shadowwalker/next-pwa/issues/131#issuecomment-821894809
145
+ if (!sameOrigin || pathname.startsWith("/api/auth/callback")) {
146
+ return false;
147
+ }
148
+
149
+ if (pathname.startsWith("/api/")) {
150
+ return true;
151
+ }
152
+
153
+ return false;
154
+ },
155
+ handler: "NetworkFirst",
156
+ method: "GET",
157
+ options: {
158
+ cacheName: "apis",
159
+ expiration: {
160
+ maxEntries: 16,
161
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
162
+ },
163
+ networkTimeoutSeconds: 10, // fallback to cache if API does not response within 10 seconds
164
+ },
165
+ },
166
+ ...definePageRuntimeCaching({
167
+ rscPrefetch: {
168
+ urlPattern: ({ request, url: { pathname }, sameOrigin }) =>
169
+ request.headers.get("RSC") === "1" && request.headers.get("Next-Router-Prefetch") === "1" && sameOrigin && !pathname.startsWith("/api/"),
170
+ handler: "NetworkFirst",
171
+ options: {
172
+ expiration: {
173
+ maxEntries: 32,
174
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
175
+ },
176
+ },
177
+ },
178
+ rsc: {
179
+ urlPattern: ({ request, url: { pathname }, sameOrigin }) => request.headers.get("RSC") === "1" && sameOrigin && !pathname.startsWith("/api/"),
180
+ handler: "NetworkFirst",
181
+ options: {
182
+ expiration: {
183
+ maxEntries: 32,
184
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
185
+ },
186
+ },
187
+ },
188
+ html: {
189
+ urlPattern: ({ request, url: { pathname }, sameOrigin }) =>
190
+ request.headers.get("Content-Type")?.includes("text/html") && sameOrigin && !pathname.startsWith("/api/"),
191
+ handler: "NetworkFirst",
192
+ options: {
193
+ expiration: {
194
+ maxEntries: 32,
195
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
196
+ },
197
+ },
198
+ },
199
+ }),
200
+ {
201
+ urlPattern: ({ url: { pathname }, sameOrigin }) => sameOrigin && !pathname.startsWith("/api/"),
202
+ handler: "NetworkFirst",
203
+ options: {
204
+ cacheName: "others",
205
+ expiration: {
206
+ maxEntries: 32,
207
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
208
+ },
209
+ },
210
+ },
211
+ {
212
+ urlPattern: ({ sameOrigin }) => !sameOrigin,
213
+ handler: "NetworkFirst",
214
+ options: {
215
+ cacheName: "cross-origin",
216
+ expiration: {
217
+ maxEntries: 32,
218
+ maxAgeSeconds: 60 * 60, // 1 hour
219
+ },
220
+ networkTimeoutSeconds: 10,
221
+ },
222
+ },
223
+ ] satisfies RuntimeCaching[];
@@ -0,0 +1,36 @@
1
+ import type { RuntimeCaching } from "@serwist/sw";
2
+ import { nonNullable } from "@serwist/utils";
3
+
4
+ export interface PageRuntimeCaching extends Omit<RuntimeCaching, "options"> {
5
+ options?: Omit<NonNullable<RuntimeCaching["options"]>, "cacheName">;
6
+ }
7
+
8
+ export interface DefinePageRuntimeCachingOptions {
9
+ rscPrefetch?: PageRuntimeCaching;
10
+ rsc?: PageRuntimeCaching;
11
+ html?: PageRuntimeCaching;
12
+ }
13
+
14
+ /**
15
+ * Conveniently define three `runtimeCaching` entries for Next.js pages.
16
+ * @param options
17
+ * @returns
18
+ */
19
+ export const definePageRuntimeCaching = ({ rscPrefetch, rsc, html }: DefinePageRuntimeCachingOptions): RuntimeCaching[] => {
20
+ const pageRcs = [rscPrefetch, rsc, html] as RuntimeCaching[];
21
+
22
+ if (pageRcs[0]) {
23
+ if (!pageRcs[0].options) pageRcs[0].options = {};
24
+ pageRcs[0].options.cacheName = "pages-rsc-prefetch";
25
+ }
26
+ if (pageRcs[1]) {
27
+ if (!pageRcs[1].options) pageRcs[1].options = {};
28
+ pageRcs[1].options.cacheName = "pages-rsc";
29
+ }
30
+ if (pageRcs[2]) {
31
+ if (!pageRcs[2].options) pageRcs[2].options = {};
32
+ pageRcs[2].options.cacheName = "pages";
33
+ }
34
+
35
+ return pageRcs.filter(nonNullable);
36
+ };
@@ -1,2 +0,0 @@
1
- import type { RuntimeCaching } from "@serwist/build";
2
- export declare const defaultCache: RuntimeCaching[];
@@ -1,2 +0,0 @@
1
- import type { RuntimeCaching } from "@serwist/build";
2
- export declare const defaultCache: RuntimeCaching[];
@@ -1,208 +0,0 @@
1
- // Serwist RuntimeCaching config: https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-build#.RuntimeCachingEntry
2
- const defaultCache = [
3
- {
4
- urlPattern: /^https:\/\/fonts\.(?:gstatic)\.com\/.*/i,
5
- handler: "CacheFirst",
6
- options: {
7
- cacheName: "google-fonts-webfonts",
8
- expiration: {
9
- maxEntries: 4,
10
- maxAgeSeconds: 365 * 24 * 60 * 60
11
- }
12
- }
13
- },
14
- {
15
- urlPattern: /^https:\/\/fonts\.(?:googleapis)\.com\/.*/i,
16
- handler: "StaleWhileRevalidate",
17
- options: {
18
- cacheName: "google-fonts-stylesheets",
19
- expiration: {
20
- maxEntries: 4,
21
- maxAgeSeconds: 7 * 24 * 60 * 60
22
- }
23
- }
24
- },
25
- {
26
- urlPattern: /\.(?:eot|otf|ttc|ttf|woff|woff2|font.css)$/i,
27
- handler: "StaleWhileRevalidate",
28
- options: {
29
- cacheName: "static-font-assets",
30
- expiration: {
31
- maxEntries: 4,
32
- maxAgeSeconds: 7 * 24 * 60 * 60
33
- }
34
- }
35
- },
36
- {
37
- urlPattern: /\.(?:jpg|jpeg|gif|png|svg|ico|webp)$/i,
38
- handler: "StaleWhileRevalidate",
39
- options: {
40
- cacheName: "static-image-assets",
41
- expiration: {
42
- maxEntries: 64,
43
- maxAgeSeconds: 30 * 24 * 60 * 60
44
- }
45
- }
46
- },
47
- {
48
- urlPattern: /\/_next\/static.+\.js$/i,
49
- handler: "CacheFirst",
50
- options: {
51
- cacheName: "next-static-js-assets",
52
- expiration: {
53
- maxEntries: 64,
54
- maxAgeSeconds: 24 * 60 * 60
55
- }
56
- }
57
- },
58
- {
59
- urlPattern: /\/_next\/image\?url=.+$/i,
60
- handler: "StaleWhileRevalidate",
61
- options: {
62
- cacheName: "next-image",
63
- expiration: {
64
- maxEntries: 64,
65
- maxAgeSeconds: 24 * 60 * 60
66
- }
67
- }
68
- },
69
- {
70
- urlPattern: /\.(?:mp3|wav|ogg)$/i,
71
- handler: "CacheFirst",
72
- options: {
73
- rangeRequests: true,
74
- cacheName: "static-audio-assets",
75
- expiration: {
76
- maxEntries: 32,
77
- maxAgeSeconds: 24 * 60 * 60
78
- }
79
- }
80
- },
81
- {
82
- urlPattern: /\.(?:mp4|webm)$/i,
83
- handler: "CacheFirst",
84
- options: {
85
- rangeRequests: true,
86
- cacheName: "static-video-assets",
87
- expiration: {
88
- maxEntries: 32,
89
- maxAgeSeconds: 24 * 60 * 60
90
- }
91
- }
92
- },
93
- {
94
- urlPattern: /\.(?:js)$/i,
95
- handler: "StaleWhileRevalidate",
96
- options: {
97
- cacheName: "static-js-assets",
98
- expiration: {
99
- maxEntries: 48,
100
- maxAgeSeconds: 24 * 60 * 60
101
- }
102
- }
103
- },
104
- {
105
- urlPattern: /\.(?:css|less)$/i,
106
- handler: "StaleWhileRevalidate",
107
- options: {
108
- cacheName: "static-style-assets",
109
- expiration: {
110
- maxEntries: 32,
111
- maxAgeSeconds: 24 * 60 * 60
112
- }
113
- }
114
- },
115
- {
116
- urlPattern: /\/_next\/data\/.+\/.+\.json$/i,
117
- handler: "StaleWhileRevalidate",
118
- options: {
119
- cacheName: "next-data",
120
- expiration: {
121
- maxEntries: 32,
122
- maxAgeSeconds: 24 * 60 * 60
123
- }
124
- }
125
- },
126
- {
127
- urlPattern: /\.(?:json|xml|csv)$/i,
128
- handler: "NetworkFirst",
129
- options: {
130
- cacheName: "static-data-assets",
131
- expiration: {
132
- maxEntries: 32,
133
- maxAgeSeconds: 24 * 60 * 60
134
- }
135
- }
136
- },
137
- {
138
- urlPattern: ({ sameOrigin, url: { pathname } })=>{
139
- // Exclude /api/auth/callback/* to fix OAuth workflow in Safari without having an impact on other environments
140
- // The above route is the default for next-auth, you may need to change it if your OAuth workflow has a different callback route
141
- // Issue: https://github.com/shadowwalker/next-pwa/issues/131#issuecomment-821894809
142
- if (!sameOrigin || pathname.startsWith("/api/auth/callback")) {
143
- return false;
144
- }
145
- if (pathname.startsWith("/api/")) {
146
- return true;
147
- }
148
- return false;
149
- },
150
- handler: "NetworkFirst",
151
- method: "GET",
152
- options: {
153
- cacheName: "apis",
154
- expiration: {
155
- maxEntries: 16,
156
- maxAgeSeconds: 24 * 60 * 60
157
- },
158
- networkTimeoutSeconds: 10
159
- }
160
- },
161
- {
162
- urlPattern: ({ request, url: { pathname }, sameOrigin })=>request.headers.get("RSC") === "1" && request.headers.get("Next-Router-Prefetch") === "1" && sameOrigin && !pathname.startsWith("/api/"),
163
- handler: "NetworkFirst",
164
- options: {
165
- cacheName: "pages-rsc-prefetch",
166
- expiration: {
167
- maxEntries: 32,
168
- maxAgeSeconds: 24 * 60 * 60
169
- }
170
- }
171
- },
172
- {
173
- urlPattern: ({ request, url: { pathname }, sameOrigin })=>request.headers.get("RSC") === "1" && sameOrigin && !pathname.startsWith("/api/"),
174
- handler: "NetworkFirst",
175
- options: {
176
- cacheName: "pages-rsc",
177
- expiration: {
178
- maxEntries: 32,
179
- maxAgeSeconds: 24 * 60 * 60
180
- }
181
- }
182
- },
183
- {
184
- urlPattern: ({ url: { pathname }, sameOrigin })=>sameOrigin && !pathname.startsWith("/api/"),
185
- handler: "NetworkFirst",
186
- options: {
187
- cacheName: "pages",
188
- expiration: {
189
- maxEntries: 32,
190
- maxAgeSeconds: 24 * 60 * 60
191
- }
192
- }
193
- },
194
- {
195
- urlPattern: ({ sameOrigin })=>!sameOrigin,
196
- handler: "NetworkFirst",
197
- options: {
198
- cacheName: "cross-origin",
199
- expiration: {
200
- maxEntries: 32,
201
- maxAgeSeconds: 60 * 60
202
- },
203
- networkTimeoutSeconds: 10
204
- }
205
- }
206
- ];
207
-
208
- export { defaultCache };