Skip to content

Commit d91e20d

Browse files
committed
Updates
1 parent 6f82ad8 commit d91e20d

File tree

6 files changed

+68
-69
lines changed

6 files changed

+68
-69
lines changed

integration/fog-of-war-test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,7 @@ test.describe("Fog of War", () => {
14141414
files: {
14151415
...getFiles(),
14161416
"react-router.config.ts": reactRouterConfig({
1417-
routeDiscovery: { manifestPath: "/custom-manifest" },
1417+
routeDiscovery: { mode: "lazy", manifestPath: "/custom-manifest" },
14181418
}),
14191419
},
14201420
});
@@ -1461,7 +1461,7 @@ test.describe("Fog of War", () => {
14611461
files: {
14621462
...getFiles(),
14631463
"react-router.config.ts": reactRouterConfig({
1464-
routeDiscovery: "initial",
1464+
routeDiscovery: { mode: "initial" },
14651465
}),
14661466
"app/entry.client.tsx": js`
14671467
import { HydratedRouter } from "react-router/dom";
@@ -1575,7 +1575,7 @@ test.describe("Fog of War", () => {
15751575
let res = await fixture.requestDocument("/");
15761576
let html = await res.text();
15771577

1578-
expect(html).toContain('"routeDiscovery":"initial"');
1578+
expect(html).toContain('"routeDiscovery":{"mode":"initial"}');
15791579

15801580
await app.goto("/", true);
15811581
await page.waitForSelector("#index");
@@ -1598,7 +1598,7 @@ test.describe("Fog of War", () => {
15981598
...getFiles(),
15991599
"react-router.config.ts": reactRouterConfig({
16001600
ssr: false,
1601-
routeDiscovery: "lazy",
1601+
routeDiscovery: { mode: "lazy" },
16021602
}),
16031603
},
16041604
});
@@ -1617,7 +1617,7 @@ test.describe("Fog of War", () => {
16171617

16181618
expect(err).toEqual(new Error("Build failed, check the output above"));
16191619
expect(buildOutput).toContain(
1620-
'Error: The `routeDiscovery` config cannot be set to "lazy" when setting `ssr:false`'
1620+
'Error: The `routeDiscovery.mode` config cannot be set to "lazy" when setting `ssr:false`'
16211621
);
16221622
console.error = ogConsole;
16231623
});

integration/helpers/vite.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,7 @@ export const reactRouterConfig = ({
4242
>["unstable_splitRouteModules"];
4343
viteEnvironmentApi?: boolean;
4444
middleware?: boolean;
45-
routeDiscovery?:
46-
| "initial"
47-
| "lazy"
48-
| {
49-
mode?: "lazy";
50-
manifestPath: string;
51-
}
52-
| { mode: "initial" };
45+
routeDiscovery?: Config["routeDiscovery"];
5346
}) => {
5447
let config: Config = {
5548
ssr,

packages/react-router-dev/config/config.ts

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,9 @@ export type ReactRouterConfig = {
172172
* directly on the `routeDiscovery` config.
173173
*/
174174
routeDiscovery?:
175-
| "lazy"
176-
| "initial"
177175
| {
178-
mode?: "lazy"; // Can only adjust the manifest path in `lazy` mode
179-
manifestPath: string;
176+
mode?: "lazy";
177+
manifestPath?: string;
180178
}
181179
| {
182180
mode: "initial";
@@ -234,10 +232,7 @@ export type ResolvedReactRouterConfig = Readonly<{
234232
* application. You can set this to `initial` to opt-out of this behavior and
235233
* load all routes with the initial HTML document load.
236234
*/
237-
routeDiscovery: {
238-
mode: "lazy" | "initial";
239-
manifestPath: string;
240-
};
235+
routeDiscovery: ReactRouterConfig["routeDiscovery"];
241236
/**
242237
* An object of all available routes, keyed by route id.
243238
*/
@@ -416,6 +411,10 @@ async function resolveConfig({
416411
let defaults = {
417412
basename: "/",
418413
buildDirectory: "build",
414+
routeDiscovery: {
415+
mode: "lazy",
416+
manifestPath: "/__manifest",
417+
},
419418
serverBuildFile: "index.js",
420419
serverModuleFormat: "esm",
421420
ssr: true,
@@ -432,7 +431,7 @@ async function resolveConfig({
432431
buildDirectory: userBuildDirectory,
433432
buildEnd,
434433
prerender,
435-
routeDiscovery: userRouteDiscovery,
434+
routeDiscovery,
436435
serverBuildFile,
437436
serverBundles,
438437
serverModuleFormat,
@@ -459,43 +458,24 @@ async function resolveConfig({
459458
);
460459
}
461460

462-
let routeDiscovery: ResolvedReactRouterConfig["routeDiscovery"] = {
463-
mode: "lazy",
464-
manifestPath: "/__manifest",
465-
};
466-
467-
if (userRouteDiscovery == null) {
461+
if (userAndPresetConfigs.routeDiscovery == null) {
462+
// Disable FOW when SSR is disabled, otherwise use defaults
468463
if (!ssr) {
469-
// No FOW when SSR is disabled
470-
routeDiscovery.mode = "initial";
471-
} else {
472-
// no-op - use defaults
464+
routeDiscovery = { mode: "initial" };
473465
}
474-
} else if (
475-
userRouteDiscovery === "initial" ||
476-
(typeof userRouteDiscovery === "object" &&
477-
userRouteDiscovery.mode === "initial")
478-
) {
479-
routeDiscovery.mode = "initial";
480-
} else {
466+
} else if (userAndPresetConfigs.routeDiscovery.mode === "lazy") {
481467
if (!ssr) {
482468
return err(
483-
'The `routeDiscovery` config must be left unset or set to "initial" ' +
484-
"when setting `ssr:false`"
469+
'The `routeDiscovery.mode` config cannot be set to "lazy" when setting `ssr:false`'
485470
);
486471
}
487-
if (typeof userRouteDiscovery === "object") {
488-
if (
489-
userRouteDiscovery.manifestPath != null &&
490-
!userRouteDiscovery.manifestPath.startsWith("/")
491-
) {
492-
return err(
493-
"The `routeDiscovery.manifestPath` config must be a root-relative " +
494-
'pathname beginning with a slash (i.e., "/__manifest")'
495-
);
496-
}
497-
routeDiscovery.manifestPath =
498-
userRouteDiscovery.manifestPath ?? "/__manifest";
472+
473+
let manifestPath = userAndPresetConfigs.routeDiscovery.manifestPath;
474+
if (manifestPath != null && !manifestPath.startsWith("/")) {
475+
return err(
476+
"The `routeDiscovery.manifestPath` config must be a root-relative " +
477+
'pathname beginning with a slash (i.e., "/__manifest")'
478+
);
499479
}
500480
}
501481

packages/react-router/lib/dom/ssr/components.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import type {
2929
import { singleFetchUrl } from "./single-fetch";
3030
import { DataRouterContext, DataRouterStateContext } from "../../context";
3131
import { useLocation } from "../../hooks";
32-
import { getPartialManifest } from "./fog-of-war";
32+
import { getPartialManifest, isFogOfWarEnabled } from "./fog-of-war";
3333
import type { PageLinkDescriptor } from "../../router/links";
3434

3535
function useDataRouterContext() {
@@ -643,11 +643,17 @@ export type ScriptsProps = Omit<
643643
@category Components
644644
*/
645645
export function Scripts(props: ScriptsProps) {
646-
let { manifest, serverHandoffString, isSpaMode, renderMeta, routeDiscovery } =
647-
useFrameworkContext();
646+
let {
647+
manifest,
648+
serverHandoffString,
649+
isSpaMode,
650+
renderMeta,
651+
routeDiscovery,
652+
ssr,
653+
} = useFrameworkContext();
648654
let { router, static: isStatic, staticContext } = useDataRouterContext();
649655
let { matches: routerMatches } = useDataRouterStateContext();
650-
let enableFogOfWar = routeDiscovery.mode === "lazy";
656+
let enableFogOfWar = isFogOfWarEnabled(routeDiscovery, ssr);
651657

652658
// Let <ServerRouter> know that we hydrated and we should render the single
653659
// fetch streaming scripts

packages/react-router/lib/dom/ssr/fog-of-war.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ const discoveredPaths = new Set<string>();
2727
// https://stackoverflow.com/a/417184
2828
const URL_LIMIT = 7680;
2929

30+
export function isFogOfWarEnabled(
31+
routeDiscovery: ServerBuild["routeDiscovery"],
32+
ssr: boolean
33+
) {
34+
return routeDiscovery.mode === "lazy" && ssr === true;
35+
}
36+
3037
export function getPartialManifest(
3138
{ sri, ...manifest }: AssetsManifest,
3239
router: DataRouter
@@ -73,7 +80,7 @@ export function getPatchRoutesOnNavigationFunction(
7380
isSpaMode: boolean,
7481
basename: string | undefined
7582
): PatchRoutesOnNavigationFunction | undefined {
76-
if (routeDiscovery.mode !== "lazy") {
83+
if (!isFogOfWarEnabled(routeDiscovery, ssr)) {
7784
return undefined;
7885
}
7986

@@ -107,7 +114,7 @@ export function useFogOFWarDiscovery(
107114
React.useEffect(() => {
108115
// Don't prefetch if not enabled or if the user has `saveData` enabled
109116
if (
110-
routeDiscovery.mode !== "lazy" ||
117+
!isFogOfWarEnabled(routeDiscovery, ssr) ||
111118
navigator.connection?.saveData === true
112119
) {
113120
return;
@@ -188,6 +195,19 @@ export function useFogOFWarDiscovery(
188195
}, [ssr, isSpaMode, manifest, routeModules, router, routeDiscovery]);
189196
}
190197

198+
export function getManifestPath(
199+
_manifestPath: string | undefined,
200+
basename: string | undefined
201+
) {
202+
let manifestPath = _manifestPath || "/_manifest";
203+
204+
if (basename == null) {
205+
return manifestPath;
206+
}
207+
208+
return `${basename}${manifestPath}`.replace(/\/+/g, "/");
209+
}
210+
191211
const MANIFEST_VERSION_STORAGE_KEY = "react-router-manifest-version";
192212

193213
export async function fetchAndApplyManifestPatches(
@@ -198,14 +218,14 @@ export async function fetchAndApplyManifestPatches(
198218
ssr: boolean,
199219
isSpaMode: boolean,
200220
basename: string | undefined,
201-
_manifestPath: string,
221+
manifestPath: string,
202222
patchRoutes: DataRouter["patchRoutes"],
203223
signal?: AbortSignal
204224
): Promise<void> {
205-
let manifestPath = `${
206-
basename != null ? basename : "/"
207-
}${_manifestPath}`.replace(/\/+/g, "/");
208-
let url = new URL(manifestPath, window.location.origin);
225+
let url = new URL(
226+
getManifestPath(manifestPath, basename),
227+
window.location.origin
228+
);
209229
paths.sort().forEach((path) => url.searchParams.append("p", path));
210230
url.searchParams.set("version", manifest.version);
211231

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import type {
4141
} from "../dom/ssr/single-fetch";
4242
import { SingleFetchRedirectSymbol } from "../dom/ssr/single-fetch";
4343
import type { MiddlewareEnabled } from "../types/future";
44+
import { getManifestPath } from "../dom/ssr/fog-of-war";
4445

4546
export type RequestHandler = (
4647
request: Request,
@@ -199,11 +200,10 @@ export const createRequestHandler: CreateRequestHandlerFunction = (
199200
}
200201

201202
// Manifest request for fog of war
202-
let manifestUrl =
203-
`${normalizedBasename}${_build.routeDiscovery.manifestPath}`.replace(
204-
/\/+/g,
205-
"/"
206-
);
203+
let manifestUrl = getManifestPath(
204+
_build.routeDiscovery.manifestPath,
205+
normalizedBasename
206+
);
207207
if (url.pathname === manifestUrl) {
208208
try {
209209
let res = await handleManifestRequest(_build, routes, url);

0 commit comments

Comments
 (0)