Skip to content

Properly handle interrupted manifest requests #12915

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
Jan 30, 2025
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/young-stingrays-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

Properly handle interrupted manifest requests in lazy route discovery
55 changes: 55 additions & 0 deletions integration/fog-of-war-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1273,4 +1273,59 @@ test.describe("Fog of War", () => {
),
]);
});

test("handles interruptions from back to back navigations", async ({
page,
}) => {
let fixture = await createFixture({
files: {
...getFiles(),
"app/routes/a.tsx": js`
import { Link, Outlet, useLoaderData, useNavigate } from "react-router";
export function loader({ request }) {
return { message: "A LOADER" };
}
export default function Index() {
let data = useLoaderData();
let navigate = useNavigate();
return (
<>
<h1 id="a">A: {data.message}</h1>
<button data-link onClick={async () => {
navigate('/a/b');
setTimeout(() => navigate('/a/b'), 0)
}}>
/a/b
</button>
<Outlet/>
</>
)
}
`,
},
});
let appFixture = await createAppFixture(fixture);
let app = new PlaywrightFixture(appFixture, page);

await app.goto("/a", true);
expect(
await page.evaluate(() =>
Object.keys((window as any).__reactRouterManifest.routes)
)
).toEqual(["root", "routes/a", "routes/_index"]);

// /a/b gets discovered on click
await app.clickElement("[data-link]");
await new Promise((resolve) => setTimeout(resolve, 1000));
expect(await (await page.$("body"))?.textContent()).not.toContain(
"Not Found"
);
await page.waitForSelector("#b");

expect(
await page.evaluate(() =>
Object.keys((window as any).__reactRouterManifest.routes)
)
).toEqual(["root", "routes/a", "routes/_index", "routes/a.b"]);
});
});
28 changes: 18 additions & 10 deletions packages/react-router/lib/dom/ssr/fog-of-war.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function getPatchRoutesOnNavigationFunction(
return undefined;
}

return async ({ path, patch }) => {
return async ({ path, patch, signal }) => {
if (discoveredPaths.has(path)) {
return;
}
Expand All @@ -87,7 +87,8 @@ export function getPatchRoutesOnNavigationFunction(
routeModules,
isSpaMode,
basename,
patch
patch,
signal
);
};
}
Expand Down Expand Up @@ -185,7 +186,8 @@ export async function fetchAndApplyManifestPatches(
routeModules: RouteModules,
isSpaMode: boolean,
basename: string | undefined,
patchRoutes: DataRouter["patchRoutes"]
patchRoutes: DataRouter["patchRoutes"],
signal?: AbortSignal
): Promise<void> {
let manifestPath = `${basename != null ? basename : "/"}/__manifest`.replace(
/\/+/g,
Expand All @@ -203,15 +205,21 @@ export async function fetchAndApplyManifestPatches(
return;
}

let res = await fetch(url);
let serverPatches: AssetsManifest["routes"];
try {
let res = await fetch(url, { signal });

if (!res.ok) {
throw new Error(`${res.status} ${res.statusText}`);
} else if (res.status >= 400) {
throw new Error(await res.text());
}
if (!res.ok) {
throw new Error(`${res.status} ${res.statusText}`);
} else if (res.status >= 400) {
throw new Error(await res.text());
}

let serverPatches = (await res.json()) as AssetsManifest["routes"];
serverPatches = (await res.json()) as AssetsManifest["routes"];
} catch (e) {
if (signal?.aborted) return;
throw e;
}

// Patch routes we don't know about yet into the manifest
let knownRoutes = new Set(Object.keys(manifest.routes));
Expand Down
1 change: 1 addition & 0 deletions packages/react-router/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3180,6 +3180,7 @@ export function createRouter(init: RouterInit): Router {
let localManifest = manifest;
try {
await patchRoutesOnNavigationImpl({
signal,
path: pathname,
matches: partialMatches,
patch: (routeId, children) => {
Expand Down
1 change: 1 addition & 0 deletions packages/react-router/lib/router/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ export type AgnosticPatchRoutesOnNavigationFunctionArgs<
O extends AgnosticRouteObject = AgnosticRouteObject,
M extends AgnosticRouteMatch = AgnosticRouteMatch
> = {
signal: AbortSignal;
path: string;
matches: M[];
patch: (routeId: string | null, children: O[]) => void;
Expand Down