Skip to content

Commit

Permalink
Fix redirects returned from loader using data
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Oct 2, 2024
1 parent 17dca50 commit ba63e12
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-poems-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

Fix redirects returned from loaders/actions using `data()`
10 changes: 5 additions & 5 deletions integration/defer-loader-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ test.describe("deferred loaders", () => {
}
);
}
export default function Redirect() {return null;}
export default function Redirect() {
return null;
}
`,

"app/routes/direct-promise-access.tsx": js`
Expand Down Expand Up @@ -82,17 +84,15 @@ test.describe("deferred loaders", () => {

test.afterAll(async () => appFixture.close());

test.skip("deferred response can redirect on document request", async ({
test("deferred response can redirect on document request", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/redirect");
await page.waitForURL(/\?redirected/);
});

test.skip("deferred response can redirect on transition", async ({
page,
}) => {
test("deferred response can redirect on transition", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickLink("/redirect");
Expand Down
12 changes: 12 additions & 0 deletions packages/react-router/lib/server-runtime/data.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isDataWithResponseInit } from "../router/router";
import { isRedirectStatusCode } from "./responses";
import type {
ActionFunction,
ActionFunctionArgs,
Expand Down Expand Up @@ -32,6 +34,16 @@ export async function callRouteHandler(
context: args.context,
});

// If they returned a redirect via data(), re-throw it as a Response
if (
isDataWithResponseInit(result) &&
result.init &&
result.init.status &&
isRedirectStatusCode(result.init.status)
) {
throw new Response(null, result.init);
}

return result;
}

Expand Down

0 comments on commit ba63e12

Please sign in to comment.