Skip to content

Commit 44e8216

Browse files
committed
Fix redirects returned from loader using data
1 parent 91a7fdc commit 44e8216

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

.changeset/purple-poems-laugh.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
Fix redirects returned from loaders/actions using `data()`

integration/defer-loader-test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ test.describe("deferred loaders", () => {
4040
}
4141
);
4242
}
43-
export default function Redirect() {return null;}
43+
export default function Redirect() {
44+
return null;
45+
}
4446
`,
4547

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

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

85-
test.skip("deferred response can redirect on document request", async ({
87+
test("deferred response can redirect on document request", async ({
8688
page,
8789
}) => {
8890
let app = new PlaywrightFixture(appFixture, page);
8991
await app.goto("/redirect");
9092
await page.waitForURL(/\?redirected/);
9193
});
9294

93-
test.skip("deferred response can redirect on transition", async ({
94-
page,
95-
}) => {
95+
test("deferred response can redirect on transition", async ({ page }) => {
9696
let app = new PlaywrightFixture(appFixture, page);
9797
await app.goto("/");
9898
await app.clickLink("/redirect");

packages/react-router/lib/server-runtime/data.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isDataWithResponseInit } from "../router/router";
2+
import { isRedirectStatusCode } from "./responses";
13
import type {
24
ActionFunction,
35
ActionFunctionArgs,
@@ -32,6 +34,16 @@ export async function callRouteHandler(
3234
context: args.context,
3335
});
3436

37+
// If they returned a redirect via data(), re-throw it as a Response
38+
if (
39+
isDataWithResponseInit(result) &&
40+
result.init &&
41+
result.init.status &&
42+
isRedirectStatusCode(result.init.status)
43+
) {
44+
throw new Response(null, result.init);
45+
}
46+
3547
return result;
3648
}
3749

0 commit comments

Comments
 (0)