UNPKG

vite-plugin-react-pages

Version:

<p> <a href="https://www.npmjs.com/package/vite-plugin-react-pages" target="_blank" rel="noopener"><img src="https://img.shields.io/npm/v/vite-plugin-react-pages.svg" alt="npm package" /></a> </p>

69 lines (64 loc) 2.02 kB
import { useState } from 'react'; import { hydrateRoot } from 'react-dom/client'; import { d as dataCacheCtx, s as setDataCacheCtx, A as App } from './App-wZagFupr.js'; import { HashRouter, BrowserRouter } from 'react-router-dom'; import { jsx } from 'react/jsx-runtime'; import initialPages from '/@react-pages/pages'; import 'dequal'; import '/@react-pages/theme'; import 'react-dom'; /// <reference types="vite/client" /> // @ts-expect-error const Router = __HASH_ROUTER__ ? HashRouter : BrowserRouter; // @ts-expect-error const basename = __HASH_ROUTER__ ? undefined : import.meta.env.BASE_URL?.replace(/\/$/, ''); const ClientAppWrapper = ({ initCache, children }) => { const [dataCache, setDataCache] = useState(initCache ?? {}); return /*#__PURE__*/jsx(Router, { basename: basename, children: /*#__PURE__*/jsx(dataCacheCtx.Provider, { value: dataCache, children: /*#__PURE__*/jsx(setDataCacheCtx.Provider, { value: setDataCache, children: children }) }) }); }; /** * This is the entry for static-site-generation(ssg)'s client side hydration. * Used in: "ssr mode" * ("ssr" is used in many places in this project to refer to "ssg". Should have called it ssg mode...) */ const routePath = window._vitePagesSSR?.routePath; if (!routePath) { console.error(`[vite-pages]: window._vitePagesSSR?.routePath should be defined`); } const pageDataImporter = initialPages[routePath]?.data; if (pageDataImporter) { // load current page data before hydrate pageDataImporter().then(({ default: pageLoaded }) => { const initCache = { [routePath]: { ...pageLoaded } }; hydrate(initCache); }); } else { // there is no page data for current page hydrate({}); } function hydrate(initCache) { const container = document.getElementById('root'); hydrateRoot(container, /*#__PURE__*/jsx(ClientAppWrapper, { initCache: initCache, children: /*#__PURE__*/jsx(App, {}) })); } //# sourceMappingURL=ssg-client.mjs.map