Skip to content

fix: should return 400 in image optimization when Next passes errorMessage #830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
19 changes: 12 additions & 7 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,15 @@ 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) {
return buildFailureResponse(
new Error(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 Down Expand Up @@ -124,9 +133,6 @@ function validateImageParams(
false,
);
debug("image params", imageParams);
if ("errorMessage" in imageParams) {
throw new Error(imageParams.errorMessage);
}
return imageParams;
}

Expand Down Expand Up @@ -184,6 +190,7 @@ function buildSuccessResponse(
function buildFailureResponse(
e: any,
streamCreator?: StreamCreator,
statusCode = 500,
): InternalResult {
debug(e);
if (streamCreator) {
Expand All @@ -192,22 +199,20 @@ function buildFailureResponse(
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");
}
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"),
};
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