| 1 | |
| 2 | * react-router v8.3.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 | */
|
| 11 | import { StreamTransfer } from "./single-fetch.js";
|
| 12 | import { RemixErrorBoundary } from "./errorBoundaries.js";
|
| 13 | import { createServerRoutes, shouldHydrateRouteLoader } from "./routes.js";
|
| 14 | import { FrameworkContext } from "./components.js";
|
| 15 | import { StaticRouterProvider, createStaticRouter } from "../server.js";
|
| 16 | import * as React$1 from "react";
|
| 17 |
|
| 18 | |
| 19 | * The server entry point for a React Router app in Framework Mode. This
|
| 20 | * component is used to generate the HTML in the response from the server. See
|
| 21 | * [`entry.server.tsx`](../framework-conventions/entry.server.tsx).
|
| 22 | *
|
| 23 | * @public
|
| 24 | * @category Framework Routers
|
| 25 | * @mode framework
|
| 26 | * @param props Props
|
| 27 | * @param {ServerRouterProps.context} props.context n/a
|
| 28 | * @param {ServerRouterProps.nonce} props.nonce n/a
|
| 29 | * @param {ServerRouterProps.url} props.url n/a
|
| 30 | * @returns A React element that represents the server-rendered application.
|
| 31 | */
|
| 32 | function ServerRouter({ context, url, nonce }) {
|
| 33 | if (typeof url === "string") url = new URL(url);
|
| 34 | let { manifest, routeModules, criticalCss, serverHandoffString } = context;
|
| 35 | let routes = createServerRoutes(manifest.routes, routeModules, context.future, context.isSpaMode);
|
| 36 | context.staticHandlerContext.loaderData = { ...context.staticHandlerContext.loaderData };
|
| 37 | for (let match of context.staticHandlerContext.matches) {
|
| 38 | let routeId = match.route.id;
|
| 39 | let route = routeModules[routeId];
|
| 40 | let manifestRoute = context.manifest.routes[routeId];
|
| 41 | if (route && manifestRoute && shouldHydrateRouteLoader(routeId, route.clientLoader, manifestRoute.hasLoader, context.isSpaMode) && (route.HydrateFallback || !manifestRoute.hasLoader)) delete context.staticHandlerContext.loaderData[routeId];
|
| 42 | }
|
| 43 | let router = createStaticRouter(routes, context.staticHandlerContext, { branches: context.branches });
|
| 44 | return React$1.createElement(React$1.Fragment, null, React$1.createElement(FrameworkContext.Provider, { value: {
|
| 45 | manifest,
|
| 46 | routeModules,
|
| 47 | criticalCss,
|
| 48 | serverHandoffString,
|
| 49 | future: context.future,
|
| 50 | ssr: context.ssr,
|
| 51 | isSpaMode: context.isSpaMode,
|
| 52 | routeDiscovery: context.routeDiscovery,
|
| 53 | nonce,
|
| 54 | serializeError: context.serializeError,
|
| 55 | renderMeta: context.renderMeta
|
| 56 | } }, React$1.createElement(RemixErrorBoundary, { location: router.state.location }, React$1.createElement(StaticRouterProvider, {
|
| 57 | router,
|
| 58 | context: context.staticHandlerContext,
|
| 59 | hydrate: false
|
| 60 | }))), context.serverHandoffStream ? React$1.createElement(React$1.Suspense, null, React$1.createElement(StreamTransfer, {
|
| 61 | context,
|
| 62 | identifier: 0,
|
| 63 | reader: context.serverHandoffStream.getReader(),
|
| 64 | textDecoder: new TextDecoder(),
|
| 65 | nonce
|
| 66 | })) : null);
|
| 67 | }
|
| 68 |
|
| 69 | export { ServerRouter };
|