From c13841b21d75d832ea9d80a4c14d76624d25e290 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Thu, 23 May 2024 12:22:26 -0400 Subject: [PATCH 1/2] fix issues returning/throwing response stus from resource routes --- .changeset/smart-mirrors-flow.md | 5 ++ integration/single-fetch-test.ts | 111 ++++++++++++++++++++++++ packages/remix-server-runtime/server.ts | 16 +++- 3 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 .changeset/smart-mirrors-flow.md diff --git a/.changeset/smart-mirrors-flow.md b/.changeset/smart-mirrors-flow.md new file mode 100644 index 00000000000..45a6f4a6034 --- /dev/null +++ b/.changeset/smart-mirrors-flow.md @@ -0,0 +1,5 @@ +--- +"@remix-run/server-runtime": patch +--- + +Fix issues with returning or throwing response stubs from resource routes diff --git a/integration/single-fetch-test.ts b/integration/single-fetch-test.ts index 75807384b80..a3aa4f0d039 100644 --- a/integration/single-fetch-test.ts +++ b/integration/single-fetch-test.ts @@ -1539,6 +1539,33 @@ test.describe("single-fetch", () => { console.warn = oldConsoleWarn; }); + test("allows resource routes to return null using a response stub", async () => { + let fixture = await createFixture( + { + config: { + future: { + unstable_singleFetch: true, + }, + }, + files: { + ...files, + "app/routes/resource.tsx": js` + export function loader({ response }) { + response.status = 201; + response.headers.set('X-Created-Id', '1'); + return null; + } + `, + }, + }, + ServerMode.Development + ); + let res = await fixture.requestResource("/resource"); + expect(res.status).toBe(201); + expect(res.headers.get("X-Created-Id")).toBe("1"); + expect(await res.text()).toBe(""); + }); + test("processes response stub onto resource routes returning raw data", async () => { let fixture = await createFixture( { @@ -1618,6 +1645,90 @@ test.describe("single-fetch", () => { }); }); + test("processes returned response stub redirects", async () => { + let fixture = await createFixture( + { + config: { + future: { + unstable_singleFetch: true, + }, + }, + files: { + ...files, + "app/routes/resource.tsx": js` + import { json } from '@remix-run/node'; + + export function loader({ response }) { + response.status = 301; + response.headers.set('Location', '/whatever') + return response; + } + `, + }, + }, + ServerMode.Development + ); + let res = await fixture.requestResource("/resource"); + expect(res.status).toBe(301); + expect(res.headers.get("Location")).toBe("/whatever"); + }); + + test("processes thrown response stub redirects", async () => { + let fixture = await createFixture( + { + config: { + future: { + unstable_singleFetch: true, + }, + }, + files: { + ...files, + "app/routes/resource.tsx": js` + import { json } from '@remix-run/node'; + + export function loader({ response }) { + response.status = 301; + response.headers.set('Location', '/whatever') + throw response; + } + `, + }, + }, + ServerMode.Development + ); + let res = await fixture.requestResource("/resource"); + expect(res.status).toBe(301); + expect(res.headers.get("Location")).toBe("/whatever"); + }); + + test("processes response stub redirects when null is returned", async () => { + let fixture = await createFixture( + { + config: { + future: { + unstable_singleFetch: true, + }, + }, + files: { + ...files, + "app/routes/resource.tsx": js` + import { json } from '@remix-run/node'; + + export function loader({ response }) { + response.status = 301; + response.headers.set('Location', '/whatever') + return null; + } + `, + }, + }, + ServerMode.Development + ); + let res = await fixture.requestResource("/resource"); + expect(res.status).toBe(301); + expect(res.headers.get("Location")).toBe("/whatever"); + }); + test("allows fetcher to hit resource route and return via turbo stream", async ({ page, }) => { diff --git a/packages/remix-server-runtime/server.ts b/packages/remix-server-runtime/server.ts index 8c0a797a86a..566c515ecfc 100644 --- a/packages/remix-server-runtime/server.ts +++ b/packages/remix-server-runtime/server.ts @@ -591,7 +591,7 @@ async function handleResourceRequest( : null), }); - if (typeof response === "object") { + if (typeof response === "object" && response !== null) { invariant( !(DEFERRED_SYMBOL in response), `You cannot return a \`defer()\` response from a Resource Route. Did you ` + @@ -609,6 +609,13 @@ async function handleResourceRequest( // @ts-expect-error response.headers[op](...args); } + } else if (isResponseStub(response) || response == null) { + // If the stub or null was returned, then there is no body so we just + // proxy along the status/headers to a Response + response = new Response(null, { + status: stub.status, + headers: stub.headers, + }); } else { console.warn( resourceRouteJsonWarning( @@ -639,6 +646,13 @@ async function handleResourceRequest( return error; } + if (isResponseStub(error)) { + return new Response(null, { + status: error.status, + headers: error.headers, + }); + } + if (isRouteErrorResponse(error)) { if (error) { handleError(error); From a70bd6bf1dea8524adb23ffb844ca7ad2c8dd668 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Tue, 28 May 2024 10:54:28 -0400 Subject: [PATCH 2/2] Update .changeset/smart-mirrors-flow.md --- .changeset/smart-mirrors-flow.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.changeset/smart-mirrors-flow.md b/.changeset/smart-mirrors-flow.md index 45a6f4a6034..ffd88d253af 100644 --- a/.changeset/smart-mirrors-flow.md +++ b/.changeset/smart-mirrors-flow.md @@ -2,4 +2,5 @@ "@remix-run/server-runtime": patch --- -Fix issues with returning or throwing response stubs from resource routes +- Fix error when returning null from a resource route in single fetch +- Fix issues with returning or throwing a response stub from a resource route in single fetch