200 lines
6.1 KiB
JavaScript
200 lines
6.1 KiB
JavaScript
/**
|
|
* react-router v7.1.5
|
|
*
|
|
* Copyright (c) Remix Software Inc.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE.md file in the root directory of this source tree.
|
|
*
|
|
* @license MIT
|
|
*/
|
|
import {
|
|
FrameworkContext,
|
|
RemixErrorBoundary,
|
|
RouterProvider,
|
|
createBrowserHistory,
|
|
createClientRoutes,
|
|
createClientRoutesWithHMRRevalidationOptOut,
|
|
createRouter,
|
|
decodeViaTurboStream,
|
|
deserializeErrors,
|
|
getPatchRoutesOnNavigationFunction,
|
|
getSingleFetchDataStrategy,
|
|
invariant,
|
|
mapRouteProperties,
|
|
matchRoutes,
|
|
shouldHydrateRouteLoader,
|
|
useFogOFWarDiscovery
|
|
} from "./chunk-IR6S3I6Y.mjs";
|
|
|
|
// lib/dom-export/dom-router-provider.tsx
|
|
import * as React from "react";
|
|
import * as ReactDOM from "react-dom";
|
|
function RouterProvider2(props) {
|
|
return /* @__PURE__ */ React.createElement(RouterProvider, { flushSync: ReactDOM.flushSync, ...props });
|
|
}
|
|
|
|
// lib/dom-export/hydrated-router.tsx
|
|
import * as React2 from "react";
|
|
var ssrInfo = null;
|
|
var router = null;
|
|
function initSsrInfo() {
|
|
if (!ssrInfo && window.__reactRouterContext && window.__reactRouterManifest && window.__reactRouterRouteModules) {
|
|
ssrInfo = {
|
|
context: window.__reactRouterContext,
|
|
manifest: window.__reactRouterManifest,
|
|
routeModules: window.__reactRouterRouteModules,
|
|
stateDecodingPromise: void 0,
|
|
router: void 0,
|
|
routerInitialized: false
|
|
};
|
|
}
|
|
}
|
|
function createHydratedRouter() {
|
|
initSsrInfo();
|
|
if (!ssrInfo) {
|
|
throw new Error(
|
|
"You must be using the SSR features of React Router in order to skip passing a `router` prop to `<RouterProvider>`"
|
|
);
|
|
}
|
|
let localSsrInfo = ssrInfo;
|
|
if (!ssrInfo.stateDecodingPromise) {
|
|
let stream = ssrInfo.context.stream;
|
|
invariant(stream, "No stream found for single fetch decoding");
|
|
ssrInfo.context.stream = void 0;
|
|
ssrInfo.stateDecodingPromise = decodeViaTurboStream(stream, window).then((value) => {
|
|
ssrInfo.context.state = value.value;
|
|
localSsrInfo.stateDecodingPromise.value = true;
|
|
}).catch((e) => {
|
|
localSsrInfo.stateDecodingPromise.error = e;
|
|
});
|
|
}
|
|
if (ssrInfo.stateDecodingPromise.error) {
|
|
throw ssrInfo.stateDecodingPromise.error;
|
|
}
|
|
if (!ssrInfo.stateDecodingPromise.value) {
|
|
throw ssrInfo.stateDecodingPromise;
|
|
}
|
|
let routes = createClientRoutes(
|
|
ssrInfo.manifest.routes,
|
|
ssrInfo.routeModules,
|
|
ssrInfo.context.state,
|
|
ssrInfo.context.isSpaMode
|
|
);
|
|
let hydrationData = void 0;
|
|
if (!ssrInfo.context.isSpaMode) {
|
|
hydrationData = {
|
|
...ssrInfo.context.state,
|
|
loaderData: { ...ssrInfo.context.state.loaderData }
|
|
};
|
|
let initialMatches = matchRoutes(
|
|
routes,
|
|
window.location,
|
|
window.__reactRouterContext?.basename
|
|
);
|
|
if (initialMatches) {
|
|
for (let match of initialMatches) {
|
|
let routeId = match.route.id;
|
|
let route = ssrInfo.routeModules[routeId];
|
|
let manifestRoute = ssrInfo.manifest.routes[routeId];
|
|
if (route && manifestRoute && shouldHydrateRouteLoader(
|
|
manifestRoute,
|
|
route,
|
|
ssrInfo.context.isSpaMode
|
|
) && (route.HydrateFallback || !manifestRoute.hasLoader)) {
|
|
delete hydrationData.loaderData[routeId];
|
|
} else if (manifestRoute && !manifestRoute.hasLoader) {
|
|
hydrationData.loaderData[routeId] = null;
|
|
}
|
|
}
|
|
}
|
|
if (hydrationData && hydrationData.errors) {
|
|
hydrationData.errors = deserializeErrors(hydrationData.errors);
|
|
}
|
|
}
|
|
let router2 = createRouter({
|
|
routes,
|
|
history: createBrowserHistory(),
|
|
basename: ssrInfo.context.basename,
|
|
hydrationData,
|
|
mapRouteProperties,
|
|
dataStrategy: getSingleFetchDataStrategy(
|
|
ssrInfo.manifest,
|
|
ssrInfo.routeModules,
|
|
() => router2
|
|
),
|
|
patchRoutesOnNavigation: getPatchRoutesOnNavigationFunction(
|
|
ssrInfo.manifest,
|
|
ssrInfo.routeModules,
|
|
ssrInfo.context.isSpaMode,
|
|
ssrInfo.context.basename
|
|
)
|
|
});
|
|
ssrInfo.router = router2;
|
|
if (router2.state.initialized) {
|
|
ssrInfo.routerInitialized = true;
|
|
router2.initialize();
|
|
}
|
|
router2.createRoutesForHMR = /* spacer so ts-ignore does not affect the right hand of the assignment */
|
|
createClientRoutesWithHMRRevalidationOptOut;
|
|
window.__reactRouterDataRouter = router2;
|
|
return router2;
|
|
}
|
|
function HydratedRouter() {
|
|
if (!router) {
|
|
router = createHydratedRouter();
|
|
}
|
|
let [criticalCss, setCriticalCss] = React2.useState(
|
|
process.env.NODE_ENV === "development" ? ssrInfo?.context.criticalCss : void 0
|
|
);
|
|
if (process.env.NODE_ENV === "development") {
|
|
if (ssrInfo) {
|
|
window.__reactRouterClearCriticalCss = () => setCriticalCss(void 0);
|
|
}
|
|
}
|
|
let [location, setLocation] = React2.useState(router.state.location);
|
|
React2.useLayoutEffect(() => {
|
|
if (ssrInfo && ssrInfo.router && !ssrInfo.routerInitialized) {
|
|
ssrInfo.routerInitialized = true;
|
|
ssrInfo.router.initialize();
|
|
}
|
|
}, []);
|
|
React2.useLayoutEffect(() => {
|
|
if (ssrInfo && ssrInfo.router) {
|
|
return ssrInfo.router.subscribe((newState) => {
|
|
if (newState.location !== location) {
|
|
setLocation(newState.location);
|
|
}
|
|
});
|
|
}
|
|
}, [location]);
|
|
invariant(ssrInfo, "ssrInfo unavailable for HydratedRouter");
|
|
useFogOFWarDiscovery(
|
|
router,
|
|
ssrInfo.manifest,
|
|
ssrInfo.routeModules,
|
|
ssrInfo.context.isSpaMode
|
|
);
|
|
return (
|
|
// This fragment is important to ensure we match the <ServerRouter> JSX
|
|
// structure so that useId values hydrate correctly
|
|
/* @__PURE__ */ React2.createElement(React2.Fragment, null, /* @__PURE__ */ React2.createElement(
|
|
FrameworkContext.Provider,
|
|
{
|
|
value: {
|
|
manifest: ssrInfo.manifest,
|
|
routeModules: ssrInfo.routeModules,
|
|
future: ssrInfo.context.future,
|
|
criticalCss,
|
|
isSpaMode: ssrInfo.context.isSpaMode
|
|
}
|
|
},
|
|
/* @__PURE__ */ React2.createElement(RemixErrorBoundary, { location }, /* @__PURE__ */ React2.createElement(RouterProvider2, { router }))
|
|
), /* @__PURE__ */ React2.createElement(React2.Fragment, null))
|
|
);
|
|
}
|
|
export {
|
|
HydratedRouter,
|
|
RouterProvider2 as RouterProvider
|
|
};
|