UNPKG

8.95 kB JavaScript View Raw
1/**
2 * react-router v7.6.2
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 strict";
12var __create = Object.create;
13var __defProp = Object.defineProperty;
14var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
15var __getOwnPropNames = Object.getOwnPropertyNames;
16var __getProtoOf = Object.getPrototypeOf;
17var __hasOwnProp = Object.prototype.hasOwnProperty;
18var __export = (target, all) => {
19 for (var name in all)
20 __defProp(target, name, { get: all[name], enumerable: true });
21};
22var __copyProps = (to, from, except, desc) => {
23 if (from && typeof from === "object" || typeof from === "function") {
24 for (let key of __getOwnPropNames(from))
25 if (!__hasOwnProp.call(to, key) && key !== except)
26 __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
27 }
28 return to;
29};
30var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
31 // If the importer is in node compatibility mode or this is not an ESM
32 // file that has been converted to a CommonJS file using a Babel-
33 // compatible transform (i.e. "__esModule" has not been set), then set
34 // "default" to the CommonJS "module.exports" for node compatibility.
35 isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
36 mod
37));
38var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
39
40// dom-export.ts
41var dom_export_exports = {};
42__export(dom_export_exports, {
43 HydratedRouter: () => HydratedRouter,
44 RouterProvider: () => RouterProvider
45});
46module.exports = __toCommonJS(dom_export_exports);
47
48// lib/dom-export/dom-router-provider.tsx
49var React = __toESM(require("react"));
50var ReactDOM = __toESM(require("react-dom"));
51var import_react_router = require("react-router");
52function RouterProvider(props) {
53 return /* @__PURE__ */ React.createElement(import_react_router.RouterProvider, { flushSync: ReactDOM.flushSync, ...props });
54}
55
56// lib/dom-export/hydrated-router.tsx
57var React2 = __toESM(require("react"));
58var import_react_router2 = require("react-router");
59var ssrInfo = null;
60var router = null;
61function initSsrInfo() {
62 if (!ssrInfo && window.__reactRouterContext && window.__reactRouterManifest && window.__reactRouterRouteModules) {
63 if (window.__reactRouterManifest.sri === true) {
64 const importMap = document.querySelector("script[rr-importmap]");
65 if (importMap?.textContent) {
66 try {
67 window.__reactRouterManifest.sri = JSON.parse(
68 importMap.textContent
69 ).integrity;
70 } catch (err) {
71 console.error("Failed to parse import map", err);
72 }
73 }
74 }
75 ssrInfo = {
76 context: window.__reactRouterContext,
77 manifest: window.__reactRouterManifest,
78 routeModules: window.__reactRouterRouteModules,
79 stateDecodingPromise: void 0,
80 router: void 0,
81 routerInitialized: false
82 };
83 }
84}
85function createHydratedRouter({
86 unstable_getContext
87}) {
88 initSsrInfo();
89 if (!ssrInfo) {
90 throw new Error(
91 "You must be using the SSR features of React Router in order to skip passing a `router` prop to `<RouterProvider>`"
92 );
93 }
94 let localSsrInfo = ssrInfo;
95 if (!ssrInfo.stateDecodingPromise) {
96 let stream = ssrInfo.context.stream;
97 (0, import_react_router2.UNSAFE_invariant)(stream, "No stream found for single fetch decoding");
98 ssrInfo.context.stream = void 0;
99 ssrInfo.stateDecodingPromise = (0, import_react_router2.UNSAFE_decodeViaTurboStream)(stream, window).then((value) => {
100 ssrInfo.context.state = value.value;
101 localSsrInfo.stateDecodingPromise.value = true;
102 }).catch((e) => {
103 localSsrInfo.stateDecodingPromise.error = e;
104 });
105 }
106 if (ssrInfo.stateDecodingPromise.error) {
107 throw ssrInfo.stateDecodingPromise.error;
108 }
109 if (!ssrInfo.stateDecodingPromise.value) {
110 throw ssrInfo.stateDecodingPromise;
111 }
112 let routes = (0, import_react_router2.UNSAFE_createClientRoutes)(
113 ssrInfo.manifest.routes,
114 ssrInfo.routeModules,
115 ssrInfo.context.state,
116 ssrInfo.context.ssr,
117 ssrInfo.context.isSpaMode
118 );
119 let hydrationData = void 0;
120 if (ssrInfo.context.isSpaMode) {
121 let { loaderData } = ssrInfo.context.state;
122 if (ssrInfo.manifest.routes.root?.hasLoader && loaderData && "root" in loaderData) {
123 hydrationData = {
124 loaderData: {
125 root: loaderData.root
126 }
127 };
128 }
129 } else {
130 hydrationData = (0, import_react_router2.UNSAFE_getHydrationData)(
131 ssrInfo.context.state,
132 routes,
133 (routeId) => ({
134 clientLoader: ssrInfo.routeModules[routeId]?.clientLoader,
135 hasLoader: ssrInfo.manifest.routes[routeId]?.hasLoader === true,
136 hasHydrateFallback: ssrInfo.routeModules[routeId]?.HydrateFallback != null
137 }),
138 window.location,
139 window.__reactRouterContext?.basename,
140 ssrInfo.context.isSpaMode
141 );
142 if (hydrationData && hydrationData.errors) {
143 hydrationData.errors = (0, import_react_router2.UNSAFE_deserializeErrors)(hydrationData.errors);
144 }
145 }
146 let router2 = (0, import_react_router2.UNSAFE_createRouter)({
147 routes,
148 history: (0, import_react_router2.UNSAFE_createBrowserHistory)(),
149 basename: ssrInfo.context.basename,
150 unstable_getContext,
151 hydrationData,
152 hydrationRouteProperties: import_react_router2.UNSAFE_hydrationRouteProperties,
153 mapRouteProperties: import_react_router2.UNSAFE_mapRouteProperties,
154 future: {
155 unstable_middleware: ssrInfo.context.future.unstable_middleware
156 },
157 dataStrategy: (0, import_react_router2.UNSAFE_getTurboStreamSingleFetchDataStrategy)(
158 () => router2,
159 ssrInfo.manifest,
160 ssrInfo.routeModules,
161 ssrInfo.context.ssr,
162 ssrInfo.context.basename
163 ),
164 patchRoutesOnNavigation: (0, import_react_router2.UNSAFE_getPatchRoutesOnNavigationFunction)(
165 ssrInfo.manifest,
166 ssrInfo.routeModules,
167 ssrInfo.context.ssr,
168 ssrInfo.context.routeDiscovery,
169 ssrInfo.context.isSpaMode,
170 ssrInfo.context.basename
171 )
172 });
173 ssrInfo.router = router2;
174 if (router2.state.initialized) {
175 ssrInfo.routerInitialized = true;
176 router2.initialize();
177 }
178 router2.createRoutesForHMR = /* spacer so ts-ignore does not affect the right hand of the assignment */
179 import_react_router2.UNSAFE_createClientRoutesWithHMRRevalidationOptOut;
180 window.__reactRouterDataRouter = router2;
181 return router2;
182}
183function HydratedRouter(props) {
184 if (!router) {
185 router = createHydratedRouter({
186 unstable_getContext: props.unstable_getContext
187 });
188 }
189 let [criticalCss, setCriticalCss] = React2.useState(
190 process.env.NODE_ENV === "development" ? ssrInfo?.context.criticalCss : void 0
191 );
192 if (process.env.NODE_ENV === "development") {
193 if (ssrInfo) {
194 window.__reactRouterClearCriticalCss = () => setCriticalCss(void 0);
195 }
196 }
197 let [location, setLocation] = React2.useState(router.state.location);
198 React2.useLayoutEffect(() => {
199 if (ssrInfo && ssrInfo.router && !ssrInfo.routerInitialized) {
200 ssrInfo.routerInitialized = true;
201 ssrInfo.router.initialize();
202 }
203 }, []);
204 React2.useLayoutEffect(() => {
205 if (ssrInfo && ssrInfo.router) {
206 return ssrInfo.router.subscribe((newState) => {
207 if (newState.location !== location) {
208 setLocation(newState.location);
209 }
210 });
211 }
212 }, [location]);
213 (0, import_react_router2.UNSAFE_invariant)(ssrInfo, "ssrInfo unavailable for HydratedRouter");
214 (0, import_react_router2.UNSAFE_useFogOFWarDiscovery)(
215 router,
216 ssrInfo.manifest,
217 ssrInfo.routeModules,
218 ssrInfo.context.ssr,
219 ssrInfo.context.routeDiscovery,
220 ssrInfo.context.isSpaMode
221 );
222 return (
223 // This fragment is important to ensure we match the <ServerRouter> JSX
224 // structure so that useId values hydrate correctly
225 /* @__PURE__ */ React2.createElement(React2.Fragment, null, /* @__PURE__ */ React2.createElement(
226 import_react_router2.UNSAFE_FrameworkContext.Provider,
227 {
228 value: {
229 manifest: ssrInfo.manifest,
230 routeModules: ssrInfo.routeModules,
231 future: ssrInfo.context.future,
232 criticalCss,
233 ssr: ssrInfo.context.ssr,
234 isSpaMode: ssrInfo.context.isSpaMode,
235 routeDiscovery: ssrInfo.context.routeDiscovery
236 }
237 },
238 /* @__PURE__ */ React2.createElement(import_react_router2.UNSAFE_RemixErrorBoundary, { location }, /* @__PURE__ */ React2.createElement(RouterProvider, { router }))
239 ), /* @__PURE__ */ React2.createElement(React2.Fragment, null))
240 );
241}
242// Annotate the CommonJS export names for ESM import in node:
2430 && (module.exports = {
244 HydratedRouter,
245 RouterProvider
246});