Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/unlucky-shrimps-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

fix: return 400 when validateImageParams from Next passes an errorMessage
38 changes: 25 additions & 13 deletions packages/open-next/src/adapters/image-optimization-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ export async function defaultHandler(
headers,
queryString === null ? undefined : queryString,
);
// We return a 400 here if imageParams returns an errorMessage
// https://github.com/vercel/next.js/blob/512d8283054407ab92b2583ecce3b253c3be7b85/packages/next/src/server/next-server.ts#L937-L941
if ("errorMessage" in imageParams) {
error(
"Error during validation of image params",
imageParams.errorMessage,
);
return buildFailureResponse(
imageParams.errorMessage,
options?.streamCreator,
400,
);
}
let etag: string | undefined;
// We don't cache any images, so in order to be able to return 304 responses, we compute an ETag from what is assumed to be static
if (process.env.OPENNEXT_STATIC_ETAG) {
Expand All @@ -99,10 +112,13 @@ export async function defaultHandler(
nextConfig,
downloadHandler,
);

return buildSuccessResponse(result, options?.streamCreator, etag);
} catch (e: any) {
return buildFailureResponse(e, options?.streamCreator);
error("Failed to optimize image", e);
return buildFailureResponse(
"Internal server error",
options?.streamCreator,
);
}
}

Expand All @@ -124,9 +140,6 @@ function validateImageParams(
false,
);
debug("image params", imageParams);
if ("errorMessage" in imageParams) {
throw new Error(imageParams.errorMessage);
}
return imageParams;
}

Expand Down Expand Up @@ -182,34 +195,33 @@ function buildSuccessResponse(
}

function buildFailureResponse(
e: any,
errorMessage: string,
streamCreator?: StreamCreator,
statusCode = 500,
): InternalResult {
debug(e);
debug(errorMessage, statusCode);
if (streamCreator) {
const response = new OpenNextNodeResponse(
() => void 0,
async () => void 0,
streamCreator,
);
response.writeHead(500, {
response.writeHead(statusCode, {
Vary: "Accept",
"Cache-Control": "public,max-age=60,immutable",
"Content-Type": "application/json",
});
response.end(e?.message || e?.toString() || "An error occurred");
response.end(errorMessage);
}
return {
type: "core",
isBase64Encoded: false,
statusCode: 500,
statusCode: statusCode,
headers: {
Vary: "Accept",
// For failed images, allow client to retry after 1 minute.
"Cache-Control": "public,max-age=60,immutable",
"Content-Type": "application/json",
},
body: toReadableStream(e?.message || e?.toString() || "An error occurred"),
body: toReadableStream(errorMessage),
};
}

Expand Down
9 changes: 9 additions & 0 deletions packages/tests-e2e/tests/appRouter/image-optimization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,12 @@ test("Image Optimization", async ({ page }) => {
await expect(el).toHaveJSProperty("complete", true);
await expect(el).not.toHaveJSProperty("naturalWidth", 0);
});

test("should return 400 when validateParams returns an errorMessage", async ({
request,
}) => {
const res = await request.get("/_next/image");
expect(res.status()).toBe(400);
expect(res.headers()["cache-control"]).toBe("public,max-age=60,immutable");
expect(await res.text()).toBe(`"url" parameter is required`);
});
Loading