Skip to content
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

Fix issue with 304 response sin single fetch #9941

Merged
merged 1 commit into from
Sep 5, 2024
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/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