UNPKG

6.85 kBJavaScriptView Raw
1/**
2 * react-router v7.4.0
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-SXJRHUAY.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 unstable_getContext
55}) {
56 initSsrInfo();
57 if (!ssrInfo) {
58 throw new Error(
59 "You must be using the SSR features of React Router in order to skip passing a `router` prop to `<RouterProvider>`"
60 );
61 }
62 let localSsrInfo = ssrInfo;
63 if (!ssrInfo.stateDecodingPromise) {
64 let stream = ssrInfo.context.stream;
65 invariant(stream, "No stream found for single fetch decoding");
66 ssrInfo.context.stream = void 0;
67 ssrInfo.stateDecodingPromise = decodeViaTurboStream(stream, window).then((value) => {
68 ssrInfo.context.state = value.value;
69 localSsrInfo.stateDecodingPromise.value = true;
70 }).catch((e) => {
71 localSsrInfo.stateDecodingPromise.error = e;
72 });
73 }
74 if (ssrInfo.stateDecodingPromise.error) {
75 throw ssrInfo.stateDecodingPromise.error;
76 }
77 if (!ssrInfo.stateDecodingPromise.value) {
78 throw ssrInfo.stateDecodingPromise;
79 }
80 let routes = createClientRoutes(
81 ssrInfo.manifest.routes,
82 ssrInfo.routeModules,
83 ssrInfo.context.state,
84 ssrInfo.context.ssr,
85 ssrInfo.context.isSpaMode
86 );
87 let hydrationData = void 0;
88 let loaderData = ssrInfo.context.state.loaderData;
89 if (ssrInfo.context.isSpaMode) {
90 if (ssrInfo.manifest.routes.root?.hasLoader && loaderData && "root" in loaderData) {
91 hydrationData = {
92 loaderData: {
93 root: loaderData.root
94 }
95 };
96 }
97 } else {
98 hydrationData = {
99 ...ssrInfo.context.state,
100 loaderData: { ...loaderData }
101 };
102 let initialMatches = matchRoutes(
103 routes,
104 window.location,
105 window.__reactRouterContext?.basename
106 );
107 if (initialMatches) {
108 for (let match of initialMatches) {
109 let routeId = match.route.id;
110 let route = ssrInfo.routeModules[routeId];
111 let manifestRoute = ssrInfo.manifest.routes[routeId];
112 if (route && manifestRoute && shouldHydrateRouteLoader(
113 manifestRoute,
114 route,
115 ssrInfo.context.isSpaMode
116 ) && (route.HydrateFallback || !manifestRoute.hasLoader)) {
117 delete hydrationData.loaderData[routeId];
118 } else if (manifestRoute && !manifestRoute.hasLoader) {
119 hydrationData.loaderData[routeId] = null;
120 }
121 }
122 }
123 if (hydrationData && hydrationData.errors) {
124 hydrationData.errors = deserializeErrors(hydrationData.errors);
125 }
126 }
127 let router2 = createRouter({
128 routes,
129 history: createBrowserHistory(),
130 basename: ssrInfo.context.basename,
131 unstable_getContext,
132 hydrationData,
133 mapRouteProperties,
134 future: {
135 unstable_middleware: ssrInfo.context.future.unstable_middleware
136 },
137 dataStrategy: getSingleFetchDataStrategy(
138 ssrInfo.manifest,
139 ssrInfo.routeModules,
140 ssrInfo.context.ssr,
141 ssrInfo.context.basename,
142 () => router2
143 ),
144 patchRoutesOnNavigation: getPatchRoutesOnNavigationFunction(
145 ssrInfo.manifest,
146 ssrInfo.routeModules,
147 ssrInfo.context.ssr,
148 ssrInfo.context.isSpaMode,
149 ssrInfo.context.basename
150 )
151 });
152 ssrInfo.router = router2;
153 if (router2.state.initialized) {
154 ssrInfo.routerInitialized = true;
155 router2.initialize();
156 }
157 router2.createRoutesForHMR = /* spacer so ts-ignore does not affect the right hand of the assignment */
158 createClientRoutesWithHMRRevalidationOptOut;
159 window.__reactRouterDataRouter = router2;
160 return router2;
161}
162function HydratedRouter(props) {
163 if (!router) {
164 router = createHydratedRouter({
165 unstable_getContext: props.unstable_getContext
166 });
167 }
168 let [criticalCss, setCriticalCss] = React2.useState(
169 process.env.NODE_ENV === "development" ? ssrInfo?.context.criticalCss : void 0
170 );
171 if (process.env.NODE_ENV === "development") {
172 if (ssrInfo) {
173 window.__reactRouterClearCriticalCss = () => setCriticalCss(void 0);
174 }
175 }
176 let [location, setLocation] = React2.useState(router.state.location);
177 React2.useLayoutEffect(() => {
178 if (ssrInfo && ssrInfo.router && !ssrInfo.routerInitialized) {
179 ssrInfo.routerInitialized = true;
180 ssrInfo.router.initialize();
181 }
182 }, []);
183 React2.useLayoutEffect(() => {
184 if (ssrInfo && ssrInfo.router) {
185 return ssrInfo.router.subscribe((newState) => {
186 if (newState.location !== location) {
187 setLocation(newState.location);
188 }
189 });
190 }
191 }, [location]);
192 invariant(ssrInfo, "ssrInfo unavailable for HydratedRouter");
193 useFogOFWarDiscovery(
194 router,
195 ssrInfo.manifest,
196 ssrInfo.routeModules,
197 ssrInfo.context.ssr,
198 ssrInfo.context.isSpaMode
199 );
200 return (
201 // This fragment is important to ensure we match the <ServerRouter> JSX
202 // structure so that useId values hydrate correctly
203 /* @__PURE__ */ React2.createElement(React2.Fragment, null, /* @__PURE__ */ React2.createElement(
204 FrameworkContext.Provider,
205 {
206 value: {
207 manifest: ssrInfo.manifest,
208 routeModules: ssrInfo.routeModules,
209 future: ssrInfo.context.future,
210 criticalCss,
211 ssr: ssrInfo.context.ssr,
212 isSpaMode: ssrInfo.context.isSpaMode
213 }
214 },
215 /* @__PURE__ */ React2.createElement(RemixErrorBoundary, { location }, /* @__PURE__ */ React2.createElement(RouterProvider2, { router }))
216 ), /* @__PURE__ */ React2.createElement(React2.Fragment, null))
217 );
218}
219export {
220 HydratedRouter,
221 RouterProvider2 as RouterProvider
222};