UNPKG

7.32 kBJavaScriptView Raw
1/**
2 * react-router v7.7.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 */
11"use client";
12import {
13 deserializeErrors,
14 getHydrationData
15} from "./chunk-KIUJAIYX.mjs";
16import {
17 CRITICAL_CSS_DATA_ATTRIBUTE,
18 FrameworkContext,
19 RemixErrorBoundary,
20 RouterProvider,
21 createBrowserHistory,
22 createClientRoutes,
23 createClientRoutesWithHMRRevalidationOptOut,
24 createRouter,
25 decodeViaTurboStream,
26 getPatchRoutesOnNavigationFunction,
27 getTurboStreamSingleFetchDataStrategy,
28 hydrationRouteProperties,
29 invariant,
30 mapRouteProperties,
31 useFogOFWarDiscovery
32} from "./chunk-C37GKA54.mjs";
33
34// lib/dom-export/dom-router-provider.tsx
35import * as React from "react";
36import * as ReactDOM from "react-dom";
37function RouterProvider2(props) {
38 return /* @__PURE__ */ React.createElement(RouterProvider, { flushSync: ReactDOM.flushSync, ...props });
39}
40
41// lib/dom-export/hydrated-router.tsx
42import * as React2 from "react";
43var ssrInfo = null;
44var router = null;
45function initSsrInfo() {
46 if (!ssrInfo && window.__reactRouterContext && window.__reactRouterManifest && window.__reactRouterRouteModules) {
47 if (window.__reactRouterManifest.sri === true) {
48 const importMap = document.querySelector("script[rr-importmap]");
49 if (importMap?.textContent) {
50 try {
51 window.__reactRouterManifest.sri = JSON.parse(
52 importMap.textContent
53 ).integrity;
54 } catch (err) {
55 console.error("Failed to parse import map", err);
56 }
57 }
58 }
59 ssrInfo = {
60 context: window.__reactRouterContext,
61 manifest: window.__reactRouterManifest,
62 routeModules: window.__reactRouterRouteModules,
63 stateDecodingPromise: void 0,
64 router: void 0,
65 routerInitialized: false
66 };
67 }
68}
69function createHydratedRouter({
70 unstable_getContext
71}) {
72 initSsrInfo();
73 if (!ssrInfo) {
74 throw new Error(
75 "You must be using the SSR features of React Router in order to skip passing a `router` prop to `<RouterProvider>`"
76 );
77 }
78 let localSsrInfo = ssrInfo;
79 if (!ssrInfo.stateDecodingPromise) {
80 let stream = ssrInfo.context.stream;
81 invariant(stream, "No stream found for single fetch decoding");
82 ssrInfo.context.stream = void 0;
83 ssrInfo.stateDecodingPromise = decodeViaTurboStream(stream, window).then((value) => {
84 ssrInfo.context.state = value.value;
85 localSsrInfo.stateDecodingPromise.value = true;
86 }).catch((e) => {
87 localSsrInfo.stateDecodingPromise.error = e;
88 });
89 }
90 if (ssrInfo.stateDecodingPromise.error) {
91 throw ssrInfo.stateDecodingPromise.error;
92 }
93 if (!ssrInfo.stateDecodingPromise.value) {
94 throw ssrInfo.stateDecodingPromise;
95 }
96 let routes = createClientRoutes(
97 ssrInfo.manifest.routes,
98 ssrInfo.routeModules,
99 ssrInfo.context.state,
100 ssrInfo.context.ssr,
101 ssrInfo.context.isSpaMode
102 );
103 let hydrationData = void 0;
104 if (ssrInfo.context.isSpaMode) {
105 let { loaderData } = ssrInfo.context.state;
106 if (ssrInfo.manifest.routes.root?.hasLoader && loaderData && "root" in loaderData) {
107 hydrationData = {
108 loaderData: {
109 root: loaderData.root
110 }
111 };
112 }
113 } else {
114 hydrationData = getHydrationData(
115 ssrInfo.context.state,
116 routes,
117 (routeId) => ({
118 clientLoader: ssrInfo.routeModules[routeId]?.clientLoader,
119 hasLoader: ssrInfo.manifest.routes[routeId]?.hasLoader === true,
120 hasHydrateFallback: ssrInfo.routeModules[routeId]?.HydrateFallback != null
121 }),
122 window.location,
123 window.__reactRouterContext?.basename,
124 ssrInfo.context.isSpaMode
125 );
126 if (hydrationData && hydrationData.errors) {
127 hydrationData.errors = deserializeErrors(hydrationData.errors);
128 }
129 }
130 let router2 = createRouter({
131 routes,
132 history: createBrowserHistory(),
133 basename: ssrInfo.context.basename,
134 unstable_getContext,
135 hydrationData,
136 hydrationRouteProperties,
137 mapRouteProperties,
138 future: {
139 unstable_middleware: ssrInfo.context.future.unstable_middleware
140 },
141 dataStrategy: getTurboStreamSingleFetchDataStrategy(
142 () => router2,
143 ssrInfo.manifest,
144 ssrInfo.routeModules,
145 ssrInfo.context.ssr,
146 ssrInfo.context.basename
147 ),
148 patchRoutesOnNavigation: getPatchRoutesOnNavigationFunction(
149 ssrInfo.manifest,
150 ssrInfo.routeModules,
151 ssrInfo.context.ssr,
152 ssrInfo.context.routeDiscovery,
153 ssrInfo.context.isSpaMode,
154 ssrInfo.context.basename
155 )
156 });
157 ssrInfo.router = router2;
158 if (router2.state.initialized) {
159 ssrInfo.routerInitialized = true;
160 router2.initialize();
161 }
162 router2.createRoutesForHMR = /* spacer so ts-ignore does not affect the right hand of the assignment */
163 createClientRoutesWithHMRRevalidationOptOut;
164 window.__reactRouterDataRouter = router2;
165 return router2;
166}
167function HydratedRouter(props) {
168 if (!router) {
169 router = createHydratedRouter({
170 unstable_getContext: props.unstable_getContext
171 });
172 }
173 let [criticalCss, setCriticalCss] = React2.useState(
174 process.env.NODE_ENV === "development" ? ssrInfo?.context.criticalCss : void 0
175 );
176 React2.useEffect(() => {
177 if (process.env.NODE_ENV === "development") {
178 setCriticalCss(void 0);
179 }
180 }, []);
181 React2.useEffect(() => {
182 if (process.env.NODE_ENV === "development" && criticalCss === void 0) {
183 document.querySelectorAll(`[${CRITICAL_CSS_DATA_ATTRIBUTE}]`).forEach((element) => element.remove());
184 }
185 }, [criticalCss]);
186 let [location, setLocation] = React2.useState(router.state.location);
187 React2.useLayoutEffect(() => {
188 if (ssrInfo && ssrInfo.router && !ssrInfo.routerInitialized) {
189 ssrInfo.routerInitialized = true;
190 ssrInfo.router.initialize();
191 }
192 }, []);
193 React2.useLayoutEffect(() => {
194 if (ssrInfo && ssrInfo.router) {
195 return ssrInfo.router.subscribe((newState) => {
196 if (newState.location !== location) {
197 setLocation(newState.location);
198 }
199 });
200 }
201 }, [location]);
202 invariant(ssrInfo, "ssrInfo unavailable for HydratedRouter");
203 useFogOFWarDiscovery(
204 router,
205 ssrInfo.manifest,
206 ssrInfo.routeModules,
207 ssrInfo.context.ssr,
208 ssrInfo.context.routeDiscovery,
209 ssrInfo.context.isSpaMode
210 );
211 return (
212 // This fragment is important to ensure we match the <ServerRouter> JSX
213 // structure so that useId values hydrate correctly
214 /* @__PURE__ */ React2.createElement(React2.Fragment, null, /* @__PURE__ */ React2.createElement(
215 FrameworkContext.Provider,
216 {
217 value: {
218 manifest: ssrInfo.manifest,
219 routeModules: ssrInfo.routeModules,
220 future: ssrInfo.context.future,
221 criticalCss,
222 ssr: ssrInfo.context.ssr,
223 isSpaMode: ssrInfo.context.isSpaMode,
224 routeDiscovery: ssrInfo.context.routeDiscovery
225 }
226 },
227 /* @__PURE__ */ React2.createElement(RemixErrorBoundary, { location }, /* @__PURE__ */ React2.createElement(RouterProvider2, { router }))
228 ), /* @__PURE__ */ React2.createElement(React2.Fragment, null))
229 );
230}
231export {
232 HydratedRouter,
233 RouterProvider2 as RouterProvider
234};