Skip to content

Fix redirects returned from loader using data #12021

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

Merged
merged 1 commit into from
Oct 3, 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/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