UNPKG

9.03 kBJavaScriptView Raw
1"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }/**
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";
12
13
14var _chunkK7YFBME3js = require('./chunk-K7YFBME3.js');
15
16// lib/dom-export/dom-router-provider.tsx
17var _react = require('react'); var React = _interopRequireWildcard(_react); var React2 = _interopRequireWildcard(_react);
18var _reactdom = require('react-dom'); var ReactDOM = _interopRequireWildcard(_reactdom);
19var _reactrouter = require('react-router');
20function RouterProvider(props) {
21 return /* @__PURE__ */ React.createElement(_reactrouter.RouterProvider, { flushSync: ReactDOM.flushSync, ...props });
22}
23
24// lib/dom-export/hydrated-router.tsx
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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 (_optionalChain([importMap, 'optionalAccess', _ => _.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 _reactrouter.UNSAFE_invariant.call(void 0, stream, "No stream found for single fetch decoding");
82 ssrInfo.context.stream = void 0;
83 ssrInfo.stateDecodingPromise = _reactrouter.UNSAFE_decodeViaTurboStream.call(void 0, 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 = _reactrouter.UNSAFE_createClientRoutes.call(void 0,
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 (_optionalChain([ssrInfo, 'access', _2 => _2.manifest, 'access', _3 => _3.routes, 'access', _4 => _4.root, 'optionalAccess', _5 => _5.hasLoader]) && loaderData && "root" in loaderData) {
107 hydrationData = {
108 loaderData: {
109 root: loaderData.root
110 }
111 };
112 }
113 } else {
114 hydrationData = _reactrouter.UNSAFE_getHydrationData.call(void 0,
115 ssrInfo.context.state,
116 routes,
117 (routeId) => ({
118 clientLoader: _optionalChain([ssrInfo, 'access', _6 => _6.routeModules, 'access', _7 => _7[routeId], 'optionalAccess', _8 => _8.clientLoader]),
119 hasLoader: _optionalChain([ssrInfo, 'access', _9 => _9.manifest, 'access', _10 => _10.routes, 'access', _11 => _11[routeId], 'optionalAccess', _12 => _12.hasLoader]) === true,
120 hasHydrateFallback: _optionalChain([ssrInfo, 'access', _13 => _13.routeModules, 'access', _14 => _14[routeId], 'optionalAccess', _15 => _15.HydrateFallback]) != null
121 }),
122 window.location,
123 _optionalChain([window, 'access', _16 => _16.__reactRouterContext, 'optionalAccess', _17 => _17.basename]),
124 ssrInfo.context.isSpaMode
125 );
126 if (hydrationData && hydrationData.errors) {
127 hydrationData.errors = _reactrouter.UNSAFE_deserializeErrors.call(void 0, hydrationData.errors);
128 }
129 }
130 let router2 = _reactrouter.UNSAFE_createRouter.call(void 0, {
131 routes,
132 history: _reactrouter.UNSAFE_createBrowserHistory.call(void 0, ),
133 basename: ssrInfo.context.basename,
134 unstable_getContext,
135 hydrationData,
136 hydrationRouteProperties: _reactrouter.UNSAFE_hydrationRouteProperties,
137 mapRouteProperties: _reactrouter.UNSAFE_mapRouteProperties,
138 future: {
139 unstable_middleware: ssrInfo.context.future.unstable_middleware
140 },
141 dataStrategy: _reactrouter.UNSAFE_getTurboStreamSingleFetchDataStrategy.call(void 0,
142 () => router2,
143 ssrInfo.manifest,
144 ssrInfo.routeModules,
145 ssrInfo.context.ssr,
146 ssrInfo.context.basename
147 ),
148 patchRoutesOnNavigation: _reactrouter.UNSAFE_getPatchRoutesOnNavigationFunction.call(void 0,
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 _reactrouter.UNSAFE_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" ? _optionalChain([ssrInfo, 'optionalAccess', _18 => _18.context, 'access', _19 => _19.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(`[${_chunkK7YFBME3js.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 _reactrouter.UNSAFE_invariant.call(void 0, ssrInfo, "ssrInfo unavailable for HydratedRouter");
203 _reactrouter.UNSAFE_useFogOFWarDiscovery.call(void 0,
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 _reactrouter.UNSAFE_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(_reactrouter.UNSAFE_RemixErrorBoundary, { location }, /* @__PURE__ */ React2.createElement(RouterProvider, { router }))
228 ), /* @__PURE__ */ React2.createElement(React2.Fragment, null))
229 );
230}
231
232
233
234exports.HydratedRouter = HydratedRouter; exports.RouterProvider = RouterProvider;