Skip to content

Commit

Permalink
refactor(react-router): Fix useRoutesImpl for SSR (#10395)
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 authored Apr 25, 2023
1 parent 7110596 commit af76d50
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
"none": "45.8 kB"
},
"packages/react-router/dist/react-router.production.min.js": {
"none": "12.9 kB"
"none": "13 kB"
},
"packages/react-router/dist/umd/react-router.production.min.js": {
"none": "15.3 kB"
Expand Down
5 changes: 4 additions & 1 deletion packages/react-router-dom/__tests__/exports-test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import * as ReactRouter from "react-router";
import * as ReactRouterDOM from "react-router-dom";

let nonReExportedKeys = new Set(["UNSAFE_mapRouteProperties"]);
let nonReExportedKeys = new Set([
"UNSAFE_mapRouteProperties",
"UNSAFE_useRoutesImpl",
]);

describe("react-router-dom", () => {
for (let key in ReactRouter) {
Expand Down
21 changes: 14 additions & 7 deletions packages/react-router-dom/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
StaticHandlerContext,
CreateStaticHandlerOptions as RouterCreateStaticHandlerOptions,
UNSAFE_RouteManifest as RouteManifest,
RouterState,
} from "@remix-run/router";
import {
IDLE_BLOCKER,
Expand All @@ -17,7 +18,10 @@ import {
createStaticHandler as routerCreateStaticHandler,
UNSAFE_convertRoutesToDataRoutes as convertRoutesToDataRoutes,
} from "@remix-run/router";
import { UNSAFE_mapRouteProperties as mapRouteProperties } from "react-router";
import {
UNSAFE_mapRouteProperties as mapRouteProperties,
UNSAFE_useRoutesImpl as useRoutesImpl,
} from "react-router";
import type {
DataRouteObject,
Location,
Expand All @@ -28,7 +32,6 @@ import {
createPath,
parsePath,
Router,
useRoutes,
UNSAFE_DataRouterContext as DataRouterContext,
UNSAFE_DataRouterStateContext as DataRouterStateContext,
} from "react-router-dom";
Expand Down Expand Up @@ -122,17 +125,19 @@ export function StaticRouterProvider({
hydrateScript = `window.__staticRouterHydrationData = JSON.parse(${json});`;
}

let { state } = dataRouterContext.router;

return (
<>
<DataRouterContext.Provider value={dataRouterContext}>
<DataRouterStateContext.Provider value={dataRouterContext.router.state}>
<DataRouterStateContext.Provider value={state}>
<Router
basename={dataRouterContext.basename}
location={dataRouterContext.router.state.location}
navigationType={dataRouterContext.router.state.historyAction}
location={state.location}
navigationType={state.historyAction}
navigator={dataRouterContext.navigator}
>
<DataRoutes routes={router.routes} />
<DataRoutes routes={router.routes} state={state} />
</Router>
</DataRouterStateContext.Provider>
</DataRouterContext.Provider>
Expand All @@ -149,10 +154,12 @@ export function StaticRouterProvider({

function DataRoutes({
routes,
state,
}: {
routes: DataRouteObject[];
state: RouterState;
}): React.ReactElement | null {
return useRoutes(routes);
return useRoutesImpl(routes, undefined, state);
}

function serializeErrors(
Expand Down
5 changes: 4 additions & 1 deletion packages/react-router-native/__tests__/exports-test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import * as ReactRouter from "react-router";
import * as ReactRouterNative from "react-router-native";

let nonReExportedKeys = new Set(["UNSAFE_mapRouteProperties"]);
let nonReExportedKeys = new Set([
"UNSAFE_mapRouteProperties",
"UNSAFE_useRoutesImpl",
]);

describe("react-router-native", () => {
for (let key in ReactRouter) {
Expand Down
2 changes: 2 additions & 0 deletions packages/react-router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ import {
useRevalidator,
useRouteError,
useRouteLoaderData,
useRoutesImpl,
} from "./lib/hooks";

// Exported for backwards compatibility, but not being used internally anymore
Expand Down Expand Up @@ -299,4 +300,5 @@ export {
DataRouterStateContext as UNSAFE_DataRouterStateContext,
mapRouteProperties as UNSAFE_mapRouteProperties,
useRouteId as UNSAFE_useRouteId,
useRoutesImpl as UNSAFE_useRoutesImpl,
};
7 changes: 4 additions & 3 deletions packages/react-router/lib/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
To,
LazyRouteFunction,
RelativeRoutingType,
RouterState,
} from "@remix-run/router";
import {
Action as NavigationType,
Expand Down Expand Up @@ -116,7 +117,7 @@ export function RouterProvider({
navigator={navigator}
>
{router.state.initialized ? (
<DataRoutes routes={router.routes} />
<DataRoutes routes={router.routes} state={state} />
) : (
fallbackElement
)}
Expand All @@ -130,11 +131,11 @@ export function RouterProvider({

function DataRoutes({
routes,
state,
}: {
routes: DataRouteObject[];
state: RouterState;
}): React.ReactElement | null {
let state = React.useContext(DataRouterStateContext);
invariant(state, "No Router state available for DataRoutes");
return useRoutesImpl(routes, undefined, state);
}

Expand Down

0 comments on commit af76d50

Please sign in to comment.