Skip to content

Commit

Permalink
Permit 204/205 status codes on document requests as well
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Jan 17, 2025
1 parent d08cbb6 commit 0ef9c64
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
10 changes: 9 additions & 1 deletion integration/single-fetch-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1700,7 +1700,7 @@ test.describe("single-fetch", () => {
]);
});

test("does not try to encode a turbo-stream body into 204 responses", async ({
test.only("does not try to encode a turbo-stream body into 204 responses", async ({
page,
}) => {
let fixture = await createFixture({
Expand Down Expand Up @@ -1743,6 +1743,14 @@ test.describe("single-fetch", () => {
}
});

// Document requests
let documentRes = await fixture.requestDocument("/?index", {
method: "post",
});
expect(documentRes.status).toBe(204);
expect(await documentRes.text()).toBe("");

// Data requests
await app.goto("/");
(await page.$("[data-submit]"))?.click();
await page.waitForSelector("[data-active]");
Expand Down
14 changes: 11 additions & 3 deletions packages/react-router/lib/server-runtime/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,17 @@ async function handleDocumentRequest(

let headers = getDocumentHeaders(build, context);

// 304 responses should not have a body or a content-type
if (context.statusCode === 304) {
return new Response(null, { status: 304, headers });
// Do not include a response body if the status code is one of these,
// otherwise `undici` will throw an error when constructing the Response:
// /~https://github.com/nodejs/undici/blob/bd98a6303e45d5e0d44192a93731b1defdb415f3/lib/web/fetch/response.js#L522-L528
//
// Specs:
// https://datatracker.ietf.org/doc/html/rfc9110#name-204-no-content
// https://datatracker.ietf.org/doc/html/rfc9110#name-205-reset-content
// https://datatracker.ietf.org/doc/html/rfc9110#name-304-not-modified
let NO_BODY_STATUS_CODES = new Set([204, 205, 304]);
if (NO_BODY_STATUS_CODES.has(context.statusCode)) {
return new Response(null, { status: context.statusCode, headers });
}

// Sanitize errors outside of development environments
Expand Down

0 comments on commit 0ef9c64

Please sign in to comment.