Skip to content

Commit

Permalink
Fix issue with 304 response sin single fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Sep 5, 2024
1 parent 5cfd3c3 commit 539ad76
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-rabbits-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/server-runtime": patch
---

Single Fetch: Do not try to encode a `turbo-stream` body into 304 responses
5 changes: 3 additions & 2 deletions integration/helpers/create-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,13 @@ export async function createFixture(init: FixtureInit, mode?: ServerMode) {
let url = new URL(href, "test://test");
let request = new Request(url.toString(), init);
let response = await handler(request);
let decoded = await decodeViaTurboStream(response.body!, global);
return {
status: response.status,
statusText: response.statusText,
headers: response.headers,
data: decoded.value,
data: response.body
? (await decodeViaTurboStream(response.body!, global)).value
: null,
};
};

Expand Down
50 changes: 50 additions & 0 deletions integration/single-fetch-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1924,6 +1924,56 @@ test.describe("single-fetch", () => {
]);
});

test("does not try to encode a turbo-stream body into 304 responses", async () => {
let fixture = await createFixture({
config: {
future: {
unstable_singleFetch: true,
},
},
files: {
...files,
"app/routes/_index.tsx": js`
import { json } from "@remix-run/node";
import { useLoaderData } from "@remix-run/react";
const eTag = "1234";
export function loader({ request }) {
if (request.headers.get("If-None-Match") === eTag) {
throw new Response(null, { status: 304 });
}
return { message: "Hello from the loader!" };
};
export default function Index() {
const { message } = useLoaderData<typeof loader>();
return <h1>{message}</h1>
}
`,
},
});
let res = await fixture.requestSingleFetchData("/_root.data");
expect(res.data).toEqual({
root: {
data: {
message: "ROOT",
},
},
"routes/_index": {
data: {
message: "Hello from the loader!",
},
},
});
res = await fixture.requestSingleFetchData("/_root.data", {
headers: {
"If-None-Match": "1234",
},
});
expect(res.status).toBe(304);
expect(res.data).toBeNull();
});

test.describe("revalidations/_routes param", () => {
test("does not make a server call if no loaders need to run", async ({
page,
Expand Down
5 changes: 5 additions & 0 deletions packages/remix-server-runtime/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,11 @@ async function handleSingleFetchRequest(
let resultHeaders = new Headers(headers);
resultHeaders.set("X-Remix-Response", "yes");

// 304 responses should not have a body
if (status === 304) {
return new Response(null, { status: 304, headers: resultHeaders });
}

// We use a less-descriptive `text/x-script` here instead of something like
// `text/x-turbo` to enable compression when deployed via Cloudflare. See:
// - https://github.com/remix-run/remix/issues/9884
Expand Down

0 comments on commit 539ad76

Please sign in to comment.