@opennextjs/aws 4.1.2 → 4.1.4

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.
@@ -28,15 +28,15 @@ const defaultHandler = async (internalEvent, options) => {
28
28
  });
29
29
  const responseHeaders = {};
30
30
  response.headers.forEach((value, key) => {
31
- if (key.toLowerCase() === "set-cookie") {
32
- responseHeaders[key] = responseHeaders[key]
33
- ? [...responseHeaders[key], value]
34
- : [value];
35
- }
36
- else {
37
- responseHeaders[key] = value;
38
- }
31
+ // Headers.forEach folds same-name headers; use getSetCookie() below instead.
32
+ if (key.toLowerCase() === "set-cookie")
33
+ return;
34
+ responseHeaders[key] = value;
39
35
  });
36
+ const setCookies = response.headers.getSetCookie();
37
+ if (setCookies.length > 0) {
38
+ responseHeaders["set-cookie"] = setCookies;
39
+ }
40
40
  const body = response.body ?? emptyReadableStream();
41
41
  return {
42
42
  type: "core",
@@ -74,7 +74,7 @@ export async function createImageOptimizationBundle(options) {
74
74
  // Sharp provides pre-build binaries for all platforms. https://github.com/lovell/sharp/blob/main/docs/install.md#cross-platform
75
75
  // Target should be same as used by Lambda, see https://github.com/sst/sst/blob/ca6f763fdfddd099ce2260202d0ce48c72e211ea/packages/sst/src/constructs/NextjsSite.ts#L114
76
76
  // For SHARP_IGNORE_GLOBAL_LIBVIPS see: https://github.com/lovell/sharp/blob/main/docs/install.md#aws-lambda
77
- const sharpVersion = process.env.SHARP_VERSION ?? "0.35.3";
77
+ const sharpVersion = process.env.SHARP_VERSION ?? "0.35.4";
78
78
  // In development, we want to use the local machine settings
79
79
  const isDev = config.imageOptimization?.loader === "fs-dev";
80
80
  installDependencies(outputPath, config.imageOptimization?.install ?? {
@@ -169,12 +169,7 @@ async function generateResult(event, localizedPath, cachedValue, lastModified, i
169
169
  return undefined;
170
170
  }
171
171
  const cacheControl = await computeCacheControl(localizedPath, body, event.headers.host, cachedValue.revalidate, lastModified, isStaleFromTagCache);
172
- // Sometimes other status codes can be cached, like 404. For these cases, we should return the correct status code
173
- // Also set the status code to the rewriteStatusCode if defined
174
- // This can happen in handleMiddleware in routingHandler.
175
- // `NextResponse.rewrite(url, { status: xxx})
176
- // The rewrite status code should take precedence over the cached one
177
- const statusCode = event.rewriteStatusCode ?? cachedValue.meta?.status ?? 200;
172
+ const statusCode = computeStatusCode(event.rewriteStatusCode, cachedValue.meta?.status);
178
173
  const headers = {
179
174
  ...cacheControl,
180
175
  "content-type": type,
@@ -195,6 +190,26 @@ async function generateResult(event, localizedPath, cachedValue, lastModified, i
195
190
  headers,
196
191
  };
197
192
  }
193
+ /**
194
+ * Computes the status code to return for a cache hit.
195
+ *
196
+ * Sometimes other status codes can be cached, like 404. For these cases, we should return the correct status code.
197
+ * The rewrite status code can be set in handleMiddleware in routingHandler with
198
+ * `NextResponse.rewrite(url, { status: xxx })`.
199
+ *
200
+ * A meaningful cached status code (i.e. anything but the implicit 200) wins over the rewrite
201
+ * status code, otherwise a rewrite would turn a cached error page into a successful response.
202
+ *
203
+ * @param rewriteStatusCode The status code from the middleware rewrite, if any.
204
+ * @param cachedStatusCode The status code stored in the cache entry meta, if any.
205
+ * @returns The status code to return.
206
+ */
207
+ function computeStatusCode(rewriteStatusCode, cachedStatusCode) {
208
+ if (cachedStatusCode !== undefined && cachedStatusCode !== 200) {
209
+ return cachedStatusCode;
210
+ }
211
+ return rewriteStatusCode ?? cachedStatusCode ?? 200;
212
+ }
198
213
  /**
199
214
  *
200
215
  * https://github.com/vercel/next.js/blob/34039551d2e5f611c0abde31a197d9985918adaf/packages/next/src/shared/lib/router/utils/escape-path-delimiters.ts#L2-L10
@@ -298,7 +313,7 @@ export async function cacheInterceptor(event) {
298
313
  case "route": {
299
314
  const cacheControl = await computeCacheControl(localizedPath, cachedData.value.body, host, cachedData.value.revalidate, cachedData.lastModified, _isStale);
300
315
  const isBinary = isBinaryContentType(String(cachedData.value.meta?.headers?.["content-type"]));
301
- const statusCode = event.rewriteStatusCode ?? cachedData.value.meta?.status ?? 200;
316
+ const statusCode = computeStatusCode(event.rewriteStatusCode, cachedData.value.meta?.status);
302
317
  const headers = {
303
318
  ...cacheControl,
304
319
  ...cachedData.value.meta?.headers,
@@ -101,13 +101,10 @@ export async function handleMiddleware(internalEvent, initialSearch, middlewareL
101
101
  else {
102
102
  if (filteredHeaders.includes(key.toLowerCase()))
103
103
  return;
104
- if (key.toLowerCase() === "set-cookie") {
105
- resHeaders[key] = resHeaders[key]
106
- ? [...resHeaders[key], value]
107
- : [value];
108
- }
109
- else if (REDIRECTS.has(statusCode) &&
110
- key.toLowerCase() === "location") {
104
+ // Headers.forEach folds same-name headers; use getSetCookie() below instead.
105
+ if (key.toLowerCase() === "set-cookie")
106
+ return;
107
+ if (REDIRECTS.has(statusCode) && key.toLowerCase() === "location") {
111
108
  resHeaders[key] = normalizeLocationHeader(value, internalEvent.url);
112
109
  }
113
110
  else {
@@ -115,6 +112,10 @@ export async function handleMiddleware(internalEvent, initialSearch, middlewareL
115
112
  }
116
113
  }
117
114
  });
115
+ const setCookies = responseHeaders.getSetCookie();
116
+ if (setCookies.length > 0) {
117
+ resHeaders["set-cookie"] = setCookies;
118
+ }
118
119
  // If the middleware returned a Rewrite, set the `url` to the pathname of the rewrite
119
120
  // NOTE: the header was added to `req` from above
120
121
  const rewriteUrl = responseHeaders.get("x-middleware-rewrite");
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "access": "public"
4
4
  },
5
5
  "name": "@opennextjs/aws",
6
- "version": "4.1.2",
6
+ "version": "4.1.4",
7
7
  "bin": {
8
8
  "open-next": "./dist/index.js"
9
9
  },
@@ -56,7 +56,7 @@
56
56
  "@types/express": "5.0.0",
57
57
  "@types/node": "20.17.6",
58
58
  "concurrently": "^9.2.1",
59
- "next": "16.3.3",
59
+ "next": "16.3.4",
60
60
  "tsc-alias": "^1.8.16",
61
61
  "typescript": "5.9.3"
62
62
  },