UNPKG

6.24 kBJavaScriptView Raw
1/**
2 * react-router v7.0.1
3 *
4 * Copyright (c) Remix Software Inc.
5 *
6 * This source code is licensed under the MIT license found in the
7 * LICENSE.md file in the root directory of this source tree.
8 *
9 * @license MIT
10 */
11import {
12 FrameworkContext,
13 RemixErrorBoundary,
14 RouterProvider,
15 createBrowserHistory,
16 createClientRoutes,
17 createClientRoutesWithHMRRevalidationOptOut,
18 createRouter,
19 decodeViaTurboStream,
20 deserializeErrors,
21 getPatchRoutesOnNavigationFunction,
22 getSingleFetchDataStrategy,
23 invariant,
24 mapRouteProperties,
25 matchRoutes,
26 shouldHydrateRouteLoader,
27 useFogOFWarDiscovery
28} from "./chunk-WFMEQ3WA.mjs";
29
30// lib/dom-export/dom-router-provider.tsx
31import * as React from "react";
32import * as ReactDOM from "react-dom";
33function RouterProvider2(props) {
34 return /* @__PURE__ */ React.createElement(RouterProvider, { flushSync: ReactDOM.flushSync, ...props });
35}
36
37// lib/dom-export/hydrated-router.tsx
38import * as React2 from "react";
39var ssrInfo = null;
40var router = null;
41function initSsrInfo() {
42 if (!ssrInfo && window.__reactRouterContext && window.__reactRouterManifest && window.__reactRouterRouteModules) {
43 ssrInfo = {
44 context: window.__reactRouterContext,
45 manifest: window.__reactRouterManifest,
46 routeModules: window.__reactRouterRouteModules,
47 stateDecodingPromise: void 0,
48 router: void 0,
49 routerInitialized: false
50 };
51 }
52}
53function createHydratedRouter() {
54 initSsrInfo();
55 if (!ssrInfo) {
56 throw new Error(
57 "You must be using the SSR features of React Router in order to skip passing a `router` prop to `<RouterProvider>`"
58 );
59 }
60 let localSsrInfo = ssrInfo;
61 if (!ssrInfo.stateDecodingPromise) {
62 let stream = ssrInfo.context.stream;
63 invariant(stream, "No stream found for single fetch decoding");
64 ssrInfo.context.stream = void 0;
65 ssrInfo.stateDecodingPromise = decodeViaTurboStream(stream, window).then((value) => {
66 ssrInfo.context.state = value.value;
67 localSsrInfo.stateDecodingPromise.value = true;
68 }).catch((e) => {
69 localSsrInfo.stateDecodingPromise.error = e;
70 });
71 }
72 if (ssrInfo.stateDecodingPromise.error) {
73 throw ssrInfo.stateDecodingPromise.error;
74 }
75 if (!ssrInfo.stateDecodingPromise.value) {
76 throw ssrInfo.stateDecodingPromise;
77 }
78 let routes = createClientRoutes(
79 ssrInfo.manifest.routes,
80 ssrInfo.routeModules,
81 ssrInfo.context.state,
82 ssrInfo.context.isSpaMode
83 );
84 let hydrationData = void 0;
85 if (!ssrInfo.context.isSpaMode) {
86 hydrationData = {
87 ...ssrInfo.context.state,
88 loaderData: { ...ssrInfo.context.state.loaderData }
89 };
90 let initialMatches = matchRoutes(
91 routes,
92 window.location,
93 window.__reactRouterContext?.basename
94 );
95 if (initialMatches) {
96 for (let match of initialMatches) {
97 let routeId = match.route.id;
98 let route = ssrInfo.routeModules[routeId];
99 let manifestRoute = ssrInfo.manifest.routes[routeId];
100 if (route && manifestRoute && shouldHydrateRouteLoader(
101 manifestRoute,
102 route,
103 ssrInfo.context.isSpaMode
104 ) && (route.HydrateFallback || !manifestRoute.hasLoader)) {
105 delete hydrationData.loaderData[routeId];
106 } else if (manifestRoute && !manifestRoute.hasLoader) {
107 hydrationData.loaderData[routeId] = null;
108 }
109 }
110 }
111 if (hydrationData && hydrationData.errors) {
112 hydrationData.errors = deserializeErrors(hydrationData.errors);
113 }
114 }
115 let router2 = createRouter({
116 routes,
117 history: createBrowserHistory(),
118 basename: ssrInfo.context.basename,
119 hydrationData,
120 mapRouteProperties,
121 dataStrategy: getSingleFetchDataStrategy(
122 ssrInfo.manifest,
123 ssrInfo.routeModules,
124 () => router2
125 ),
126 patchRoutesOnNavigation: getPatchRoutesOnNavigationFunction(
127 ssrInfo.manifest,
128 ssrInfo.routeModules,
129 ssrInfo.context.isSpaMode,
130 ssrInfo.context.basename
131 )
132 });
133 ssrInfo.router = router2;
134 if (router2.state.initialized) {
135 ssrInfo.routerInitialized = true;
136 router2.initialize();
137 }
138 router2.createRoutesForHMR = /* spacer so ts-ignore does not affect the right hand of the assignment */
139 createClientRoutesWithHMRRevalidationOptOut;
140 window.__reactRouterDataRouter = router2;
141 return router2;
142}
143function HydratedRouter() {
144 if (!router) {
145 router = createHydratedRouter();
146 }
147 let [criticalCss, setCriticalCss] = React2.useState(
148 process.env.NODE_ENV === "development" ? ssrInfo?.context.criticalCss : void 0
149 );
150 if (process.env.NODE_ENV === "development") {
151 if (ssrInfo) {
152 window.__reactRouterClearCriticalCss = () => setCriticalCss(void 0);
153 }
154 }
155 let [location, setLocation] = React2.useState(router.state.location);
156 React2.useLayoutEffect(() => {
157 if (ssrInfo && ssrInfo.router && !ssrInfo.routerInitialized) {
158 ssrInfo.routerInitialized = true;
159 ssrInfo.router.initialize();
160 }
161 }, []);
162 React2.useLayoutEffect(() => {
163 if (ssrInfo && ssrInfo.router) {
164 return ssrInfo.router.subscribe((newState) => {
165 if (newState.location !== location) {
166 setLocation(newState.location);
167 }
168 });
169 }
170 }, [location]);
171 invariant(ssrInfo, "ssrInfo unavailable for HydratedRouter");
172 useFogOFWarDiscovery(
173 router,
174 ssrInfo.manifest,
175 ssrInfo.routeModules,
176 ssrInfo.context.isSpaMode
177 );
178 return (
179 // This fragment is important to ensure we match the <ServerRouter> JSX
180 // structure so that useId values hydrate correctly
181 /* @__PURE__ */ React2.createElement(React2.Fragment, null, /* @__PURE__ */ React2.createElement(
182 FrameworkContext.Provider,
183 {
184 value: {
185 manifest: ssrInfo.manifest,
186 routeModules: ssrInfo.routeModules,
187 future: ssrInfo.context.future,
188 criticalCss,
189 isSpaMode: ssrInfo.context.isSpaMode
190 }
191 },
192 /* @__PURE__ */ React2.createElement(RemixErrorBoundary, { location }, /* @__PURE__ */ React2.createElement(RouterProvider2, { router }))
193 ), /* @__PURE__ */ React2.createElement(React2.Fragment, null))
194 );
195}
196export {
197 HydratedRouter,
198 RouterProvider2 as RouterProvider
199};