@netlify/plugin-nextjs 5.8.1 → 5.9.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 (46) hide show
  1. package/README.md +3 -2
  2. package/dist/build/advanced-api-routes.js +4 -136
  3. package/dist/build/cache.js +4 -25
  4. package/dist/build/content/prerendered.js +11 -290
  5. package/dist/build/content/server.js +11 -219
  6. package/dist/build/content/static.js +14 -103
  7. package/dist/build/functions/edge.js +7 -534
  8. package/dist/build/functions/server.js +11 -130
  9. package/dist/build/image-cdn.js +3 -1599
  10. package/dist/build/plugin-context.js +6 -292
  11. package/dist/build/templates/handler-monorepo.tmpl.js +1 -1
  12. package/dist/build/templates/handler.tmpl.js +1 -1
  13. package/dist/build/verification.js +9 -104
  14. package/dist/esm-chunks/chunk-72ZI2IVI.js +36 -0
  15. package/dist/esm-chunks/chunk-AMY4NOT5.js +1610 -0
  16. package/dist/esm-chunks/chunk-BEIUVQZK.js +212 -0
  17. package/dist/esm-chunks/chunk-BFYMHE3E.js +548 -0
  18. package/dist/esm-chunks/chunk-BVYZSEV6.js +306 -0
  19. package/dist/esm-chunks/chunk-DLVROEVU.js +144 -0
  20. package/dist/esm-chunks/chunk-GFYWJNQR.js +305 -0
  21. package/dist/esm-chunks/chunk-HWMLYAVP.js +122 -0
  22. package/dist/esm-chunks/chunk-IJZEDP6B.js +235 -0
  23. package/dist/esm-chunks/chunk-K4RDUZYO.js +609 -0
  24. package/dist/esm-chunks/chunk-KBX7SJLC.js +73 -0
  25. package/dist/esm-chunks/chunk-NDSDIXRD.js +122 -0
  26. package/dist/esm-chunks/chunk-TYCYFZ22.js +25 -0
  27. package/dist/esm-chunks/chunk-UYKENJEU.js +19 -0
  28. package/dist/esm-chunks/chunk-XS27YRA5.js +34 -0
  29. package/dist/esm-chunks/chunk-ZENB67PD.js +148 -0
  30. package/dist/esm-chunks/chunk-ZSVHJNNY.js +120 -0
  31. package/dist/esm-chunks/next-4L47PQSM.js +569 -0
  32. package/dist/esm-chunks/{package-AOKLDA5E.js → package-LCNINN36.js} +5 -5
  33. package/dist/index.js +39 -18
  34. package/dist/run/config.js +4 -1
  35. package/dist/run/constants.js +5 -7
  36. package/dist/run/handlers/cache.cjs +1618 -31
  37. package/dist/run/handlers/request-context.cjs +6 -2
  38. package/dist/run/handlers/server.js +40 -20
  39. package/dist/run/handlers/tracer.cjs +83 -2
  40. package/dist/run/handlers/tracing.js +4 -2
  41. package/dist/run/handlers/wait-until.cjs +122 -0
  42. package/dist/run/headers.js +9 -194
  43. package/dist/run/next.cjs +1597 -15
  44. package/dist/run/revalidate.js +3 -24
  45. package/dist/shared/blobkey.js +3 -15
  46. package/package.json +4 -4
@@ -0,0 +1,212 @@
1
+
2
+ var require = await (async () => {
3
+ var { createRequire } = await import("node:module");
4
+ return createRequire(import.meta.url);
5
+ })();
6
+
7
+ import {
8
+ getRegionalBlobStore
9
+ } from "./chunk-K4RDUZYO.js";
10
+ import {
11
+ encodeBlobKey
12
+ } from "./chunk-TYCYFZ22.js";
13
+
14
+ // src/run/headers.ts
15
+ var ALL_VARIATIONS = Symbol.for("ALL_VARIATIONS");
16
+ var NetlifyVaryKeys = /* @__PURE__ */ new Set(["header", "language", "cookie", "query", "country"]);
17
+ var isNetlifyVaryKey = (key) => NetlifyVaryKeys.has(key);
18
+ var generateNetlifyVaryValues = ({
19
+ header,
20
+ language,
21
+ cookie,
22
+ query,
23
+ country
24
+ }) => {
25
+ const values = [];
26
+ if (query.length !== 0) {
27
+ if (query.includes(ALL_VARIATIONS)) {
28
+ values.push(`query`);
29
+ } else {
30
+ values.push(`query=${query.join(`|`)}`);
31
+ }
32
+ }
33
+ if (header.length !== 0) {
34
+ values.push(`header=${header.join(`|`)}`);
35
+ }
36
+ if (language.length !== 0) {
37
+ values.push(`language=${language.join(`|`)}`);
38
+ }
39
+ if (cookie.length !== 0) {
40
+ values.push(`cookie=${cookie.join(`|`)}`);
41
+ }
42
+ if (country.length !== 0) {
43
+ values.push(`country=${country.join(`|`)}`);
44
+ }
45
+ return values.join(",");
46
+ };
47
+ var getHeaderValueArray = (header) => {
48
+ return header.split(",").map((value) => value.trim()).filter(Boolean);
49
+ };
50
+ var omitHeaderValues = (header, values) => {
51
+ const headerValues = getHeaderValueArray(header);
52
+ const filteredValues = headerValues.filter(
53
+ (value) => !values.some((val) => value.startsWith(val))
54
+ );
55
+ return filteredValues.join(", ");
56
+ };
57
+ var setVaryHeaders = (headers, request, { basePath, i18n }) => {
58
+ const netlifyVaryValues = {
59
+ header: ["x-nextjs-data", "x-next-debug-logging"],
60
+ language: [],
61
+ cookie: ["__prerender_bypass", "__next_preview_data"],
62
+ query: ["__nextDataReq"],
63
+ country: []
64
+ };
65
+ const vary = headers.get("vary");
66
+ if (vary !== null) {
67
+ netlifyVaryValues.header.push(...getHeaderValueArray(vary));
68
+ }
69
+ const path = new URL(request.url).pathname;
70
+ const locales = i18n && i18n.localeDetection !== false ? i18n.locales : [];
71
+ if (locales.length > 1 && (path === "/" || path === basePath)) {
72
+ netlifyVaryValues.language.push(...locales);
73
+ netlifyVaryValues.cookie.push(`NEXT_LOCALE`);
74
+ }
75
+ const userNetlifyVary = headers.get("netlify-vary");
76
+ if (userNetlifyVary) {
77
+ const directives = getHeaderValueArray(userNetlifyVary);
78
+ for (const directive of directives) {
79
+ const [key, value] = directive.split("=");
80
+ if (key === "query" && !value) {
81
+ netlifyVaryValues.query.push(ALL_VARIATIONS);
82
+ } else if (value && isNetlifyVaryKey(key)) {
83
+ netlifyVaryValues[key].push(...value.split("|"));
84
+ }
85
+ }
86
+ }
87
+ headers.set(`netlify-vary`, generateNetlifyVaryValues(netlifyVaryValues));
88
+ };
89
+ var adjustDateHeader = async ({
90
+ headers,
91
+ request,
92
+ span,
93
+ tracer,
94
+ requestContext
95
+ }) => {
96
+ const cacheState = headers.get("x-nextjs-cache");
97
+ const isServedFromCache = cacheState === "HIT" || cacheState === "STALE";
98
+ span.setAttributes({
99
+ "x-nextjs-cache": cacheState ?? void 0,
100
+ isServedFromCache
101
+ });
102
+ if (!isServedFromCache) {
103
+ return;
104
+ }
105
+ const key = new URL(request.url).pathname;
106
+ let lastModified;
107
+ if (requestContext.responseCacheGetLastModified) {
108
+ lastModified = requestContext.responseCacheGetLastModified;
109
+ } else {
110
+ span.recordException(
111
+ new Error("lastModified not found in requestContext, falling back to trying blobs")
112
+ );
113
+ span.setAttributes({
114
+ severity: "alert",
115
+ warning: true
116
+ });
117
+ const blobStore = getRegionalBlobStore({ consistency: "strong" });
118
+ const blobKey = await encodeBlobKey(key);
119
+ lastModified = await tracer.withActiveSpan(
120
+ "get cache to calculate date header",
121
+ async (getBlobForDateSpan) => {
122
+ getBlobForDateSpan.setAttributes({
123
+ key,
124
+ blobKey
125
+ });
126
+ const blob = await blobStore.get(blobKey, { type: "json" }) ?? {};
127
+ getBlobForDateSpan.addEvent(blob ? "Cache hit" : "Cache miss");
128
+ return blob.lastModified;
129
+ }
130
+ );
131
+ }
132
+ if (!lastModified) {
133
+ span.recordException(
134
+ new Error(
135
+ "lastModified not found in either requestContext or blobs, date header for cached response is not set"
136
+ )
137
+ );
138
+ span.setAttributes({
139
+ severity: "alert",
140
+ warning: true
141
+ });
142
+ return;
143
+ }
144
+ const lastModifiedDate = new Date(lastModified);
145
+ headers.set("x-nextjs-date", headers.get("date") ?? lastModifiedDate.toUTCString());
146
+ headers.set("date", lastModifiedDate.toUTCString());
147
+ };
148
+ var setCacheControlHeaders = ({ headers, status }, request, requestContext) => {
149
+ if (typeof requestContext.routeHandlerRevalidate !== "undefined" && ["GET", "HEAD"].includes(request.method) && !headers.has("cdn-cache-control") && !headers.has("netlify-cdn-cache-control")) {
150
+ const cdnCacheControl = (
151
+ // if we are serving already stale response, instruct edge to not attempt to cache that response
152
+ headers.get("x-nextjs-cache") === "STALE" ? "public, max-age=0, must-revalidate, durable" : `s-maxage=${requestContext.routeHandlerRevalidate === false ? 31536e3 : requestContext.routeHandlerRevalidate}, stale-while-revalidate=31536000, durable`
153
+ );
154
+ headers.set("netlify-cdn-cache-control", cdnCacheControl);
155
+ return;
156
+ }
157
+ if (status === 404 && request.url.endsWith(".php")) {
158
+ headers.set("cache-control", "public, max-age=0, must-revalidate");
159
+ headers.set("netlify-cdn-cache-control", `max-age=31536000, durable`);
160
+ }
161
+ const cacheControl = headers.get("cache-control");
162
+ if (cacheControl !== null && ["GET", "HEAD"].includes(request.method) && !headers.has("cdn-cache-control") && !headers.has("netlify-cdn-cache-control")) {
163
+ const browserCacheControl = omitHeaderValues(cacheControl, [
164
+ "s-maxage",
165
+ "stale-while-revalidate"
166
+ ]);
167
+ const cdnCacheControl = (
168
+ // if we are serving already stale response, instruct edge to not attempt to cache that response
169
+ headers.get("x-nextjs-cache") === "STALE" ? "public, max-age=0, must-revalidate, durable" : [
170
+ ...getHeaderValueArray(cacheControl).map(
171
+ (value) => value === "stale-while-revalidate" ? "stale-while-revalidate=31536000" : value
172
+ ),
173
+ "durable"
174
+ ].join(", ")
175
+ );
176
+ headers.set("cache-control", browserCacheControl || "public, max-age=0, must-revalidate");
177
+ headers.set("netlify-cdn-cache-control", cdnCacheControl);
178
+ return;
179
+ }
180
+ if (cacheControl === null && !headers.has("cdn-cache-control") && !headers.has("netlify-cdn-cache-control") && requestContext.usedFsReadForNonFallback) {
181
+ headers.set("cache-control", "public, max-age=0, must-revalidate");
182
+ headers.set("netlify-cdn-cache-control", `max-age=31536000, durable`);
183
+ }
184
+ };
185
+ var setCacheTagsHeaders = (headers, requestContext) => {
186
+ if (requestContext.responseCacheTags && (headers.has("cache-control") || headers.has("netlify-cdn-cache-control"))) {
187
+ headers.set("netlify-cache-tag", requestContext.responseCacheTags.join(","));
188
+ }
189
+ };
190
+ var NEXT_CACHE_TO_CACHE_STATUS = {
191
+ HIT: `hit`,
192
+ MISS: `fwd=miss`,
193
+ STALE: `hit; fwd=stale`
194
+ };
195
+ var setCacheStatusHeader = (headers) => {
196
+ const nextCache = headers.get("x-nextjs-cache");
197
+ if (typeof nextCache === "string") {
198
+ if (nextCache in NEXT_CACHE_TO_CACHE_STATUS) {
199
+ const cacheStatus = NEXT_CACHE_TO_CACHE_STATUS[nextCache];
200
+ headers.set("cache-status", `"Next.js"; ${cacheStatus}`);
201
+ }
202
+ headers.delete("x-nextjs-cache");
203
+ }
204
+ };
205
+
206
+ export {
207
+ setVaryHeaders,
208
+ adjustDateHeader,
209
+ setCacheControlHeaders,
210
+ setCacheTagsHeaders,
211
+ setCacheStatusHeader
212
+ };