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 single fetch redirects whena basename is present #9848

Merged
merged 2 commits into from
Aug 9, 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/rich-teachers-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/server-runtime": patch
---

Single Fetch: Fix redirects when a `basename` is presernt
65 changes: 65 additions & 0 deletions integration/single-fetch-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,71 @@ test.describe("single-fetch", () => {
expect(await app.getHtml("#target")).toContain("Target");
});

test("processes redirects when a basename is present", async ({ page }) => {
let fixture = await createFixture({
compiler: "vite",
files: {
...files,
"vite.config.ts": js`
import { defineConfig } from "vite";
import { vitePlugin as remix } from "@remix-run/dev";
export default defineConfig({
plugins: [
remix({
basename: '/base',
future: {
unstable_singleFetch: true,
}
}),
],
});
`,
"app/routes/data.tsx": js`
import { redirect } from '@remix-run/node';
export function loader() {
throw redirect('/target');
}
export default function Component() {
return null
}
`,
"app/routes/target.tsx": js`
export default function Component() {
return <h1 id="target">Target</h1>
}
`,
},
});

console.error = () => {};

let res = await fixture.requestDocument("/base/data");
expect(res.status).toBe(302);
expect(res.headers.get("Location")).toBe("/base/target");
expect(await res.text()).toBe("");

let { status, data } = await fixture.requestSingleFetchData(
"/base/data.data"
);
expect(data).toEqual({
[SingleFetchRedirectSymbol]: {
status: 302,
redirect: "/target",
reload: false,
replace: false,
revalidate: false,
},
});
expect(status).toBe(202);

let appFixture = await createAppFixture(fixture);
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/base/");
await app.clickLink("/base/data");
await page.waitForSelector("#target");
expect(await app.getHtml("#target")).toContain("Target");
});

test("processes thrown loader errors", async ({ page }) => {
let fixture = await createFixture({
config: {
Expand Down
6 changes: 5 additions & 1 deletion packages/remix-server-runtime/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,11 @@ export const createRequestHandler: CreateRequestHandlerFunction = (

if (isRedirectResponse(response)) {
let result: SingleFetchResult | SingleFetchResults =
getSingleFetchRedirect(response.status, response.headers);
getSingleFetchRedirect(
response.status,
response.headers,
_build.basename
);

if (request.method === "GET") {
result = {
Expand Down
30 changes: 24 additions & 6 deletions packages/remix-server-runtime/single-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
isRouteErrorResponse,
unstable_data as routerData,
UNSAFE_ErrorResponseImpl as ErrorResponseImpl,
stripBasename,
} from "@remix-run/router";
import { encode } from "turbo-stream";

Expand Down Expand Up @@ -111,7 +112,11 @@ export async function singleFetchAction(
// let non-Response return values through
if (isResponse(result)) {
return {
result: getSingleFetchRedirect(result.status, result.headers),
result: getSingleFetchRedirect(
result.status,
result.headers,
build.basename
),
headers: result.headers,
status: SINGLE_FETCH_REDIRECT_STATUS,
};
Expand All @@ -122,7 +127,11 @@ export async function singleFetchAction(

if (isRedirectStatusCode(context.statusCode) && headers.has("Location")) {
return {
result: getSingleFetchRedirect(context.statusCode, headers),
result: getSingleFetchRedirect(
context.statusCode,
headers,
build.basename
),
headers,
status: SINGLE_FETCH_REDIRECT_STATUS,
};
Expand Down Expand Up @@ -192,7 +201,8 @@ export async function singleFetchLoaders(
result: {
[SingleFetchRedirectSymbol]: getSingleFetchRedirect(
result.status,
result.headers
result.headers,
build.basename
),
},
headers: result.headers,
Expand All @@ -208,7 +218,8 @@ export async function singleFetchLoaders(
result: {
[SingleFetchRedirectSymbol]: getSingleFetchRedirect(
context.statusCode,
headers
headers,
build.basename
),
},
headers,
Expand Down Expand Up @@ -264,10 +275,17 @@ export async function singleFetchLoaders(

export function getSingleFetchRedirect(
status: number,
headers: Headers
headers: Headers,
basename: string | undefined
): SingleFetchRedirectResult {
let redirect = headers.get("Location")!;

if (basename) {
redirect = stripBasename(redirect, basename) || redirect;
}

return {
redirect: headers.get("Location")!,
redirect,
status,
revalidate:
// Technically X-Remix-Revalidate isn't needed here - that was an implementation
Expand Down