vite-react-ssg 0.7.3 → 0.8.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/README.md +7 -1
  2. package/dist/chunks/jsdomGlobal.cjs +10 -65
  3. package/dist/chunks/jsdomGlobal.mjs +10 -65
  4. package/dist/client/single-page.cjs +8 -6
  5. package/dist/client/single-page.d.cts +3 -4
  6. package/dist/client/single-page.d.mts +3 -4
  7. package/dist/client/single-page.d.ts +3 -4
  8. package/dist/client/single-page.mjs +9 -7
  9. package/dist/index.cjs +19 -12
  10. package/dist/index.d.cts +3 -4
  11. package/dist/index.d.mts +3 -4
  12. package/dist/index.d.ts +3 -4
  13. package/dist/index.mjs +17 -10
  14. package/dist/node/cli.cjs +9 -8
  15. package/dist/node/cli.mjs +7 -6
  16. package/dist/node.cjs +22 -1713
  17. package/dist/node.d.cts +1 -2
  18. package/dist/node.d.mts +1 -2
  19. package/dist/node.d.ts +1 -2
  20. package/dist/node.mjs +17 -1707
  21. package/dist/shared/vite-react-ssg.4y2OEiyV.mjs +31 -0
  22. package/dist/shared/{vite-react-ssg.a292c181.mjs → vite-react-ssg.B6SGTWpD.mjs} +5 -4
  23. package/dist/shared/vite-react-ssg.BoLst82g.mjs +1036 -0
  24. package/dist/shared/{vite-react-ssg.c1d49976.cjs → vite-react-ssg.BuZVBVT6.cjs} +5 -8
  25. package/dist/shared/{vite-react-ssg.054e813a.mjs → vite-react-ssg.C0y5wbxl.mjs} +1 -30
  26. package/dist/shared/{vite-react-ssg.0408f7e1.cjs → vite-react-ssg.C2GpVZF1.cjs} +0 -34
  27. package/dist/shared/vite-react-ssg.CFQGqC60.cjs +36 -0
  28. package/dist/shared/vite-react-ssg.CjIppNIS.mjs +30 -0
  29. package/dist/shared/vite-react-ssg.D0EGioth.cjs +35 -0
  30. package/dist/shared/vite-react-ssg.Dyp0kt3B.cjs +1045 -0
  31. package/dist/shared/{vite-react-ssg.de54c34e.d.cts → vite-react-ssg.Txy2EJkI.d.cts} +25 -6
  32. package/dist/shared/{vite-react-ssg.de54c34e.d.mts → vite-react-ssg.Txy2EJkI.d.mts} +25 -6
  33. package/dist/shared/{vite-react-ssg.de54c34e.d.ts → vite-react-ssg.Txy2EJkI.d.ts} +25 -6
  34. package/dist/tanstack.cjs +150 -0
  35. package/dist/tanstack.d.cts +29 -0
  36. package/dist/tanstack.d.mts +29 -0
  37. package/dist/tanstack.d.ts +29 -0
  38. package/dist/tanstack.mjs +143 -0
  39. package/package.json +30 -17
  40. /package/dist/shared/{vite-react-ssg.579feabb.mjs → vite-react-ssg.C6pK7rvr.mjs} +0 -0
  41. /package/dist/shared/{vite-react-ssg.bee8a5a9.cjs → vite-react-ssg.CjsEygxB.cjs} +0 -0
  42. /package/dist/shared/{vite-react-ssg.faf3855a.d.cts → vite-react-ssg.Cm9gBlfg.d.cts} +0 -0
  43. /package/dist/shared/{vite-react-ssg.faf3855a.d.mts → vite-react-ssg.Cm9gBlfg.d.mts} +0 -0
  44. /package/dist/shared/{vite-react-ssg.faf3855a.d.ts → vite-react-ssg.Cm9gBlfg.d.ts} +0 -0
@@ -1,9 +1,27 @@
1
- import { Options } from 'critters';
2
1
  import { ReactNode, ReactElement } from 'react';
3
2
  import { NonIndexRouteObject, IndexRouteObject, createBrowserRouter } from 'react-router-dom';
4
3
 
5
4
  type Router = ReturnType<typeof createBrowserRouter>;
6
- interface ViteReactSSGOptions {
5
+ interface CrittersOptions {
6
+ path?: string;
7
+ publicPath?: string;
8
+ external?: boolean;
9
+ inlineThreshold?: number;
10
+ minimumExternalSize?: number;
11
+ pruneSource?: boolean;
12
+ mergeStylesheets?: boolean;
13
+ additionalStylesheets?: string[];
14
+ preload?: 'body' | 'media' | 'swap' | 'js' | 'js-lazy';
15
+ noscriptFallback?: boolean;
16
+ inlineFonts?: boolean;
17
+ preloadFonts?: boolean;
18
+ fonts?: boolean;
19
+ keyframes?: string;
20
+ compress?: boolean;
21
+ logLevel?: 'info' | 'warn' | 'error' | 'trace' | 'debug' | 'silent';
22
+ reduceInlineStyles?: boolean;
23
+ }
24
+ interface ViteReactSSGOptions<Context = ViteReactSSGContext> {
7
25
  /**
8
26
  * Set the scripts' loading mode. Only works for `type="module"`.
9
27
  *
@@ -62,7 +80,7 @@ interface ViteReactSSGOptions {
62
80
  *
63
81
  * @see https://github.com/GoogleChromeLabs/critters
64
82
  */
65
- crittersOptions?: Options | false;
83
+ crittersOptions?: CrittersOptions | false;
66
84
  /**
67
85
  * Custom function to modify the routes to do the SSG.
68
86
  *
@@ -80,7 +98,7 @@ interface ViteReactSSGOptions {
80
98
  * To do so, you can change the 'index.html' file contents (passed in through the `indexHTML` parameter), and return it.
81
99
  * The returned value will then be passed to renderer.
82
100
  */
83
- onBeforePageRender?: (route: string, indexHTML: string, appCtx: ViteReactSSGContext<true>) => Promise<string | null | undefined> | string | null | undefined;
101
+ onBeforePageRender?: (route: string, indexHTML: string, appCtx: Context) => Promise<string | null | undefined> | string | null | undefined;
84
102
  /**
85
103
  * Callback to be called on every rendered page.
86
104
  *
@@ -89,7 +107,7 @@ interface ViteReactSSGOptions {
89
107
  * To do so, you can transform the route's rendered HTML (passed in through the `renderedHTML` parameter), and return it.
90
108
  * The returned value will be used as the HTML of the route.
91
109
  */
92
- onPageRendered?: (route: string, renderedHTML: string, appCtx: ViteReactSSGContext<true>) => Promise<string | null | undefined> | string | null | undefined;
110
+ onPageRendered?: (route: string, renderedHTML: string, appCtx: Context) => Promise<string | null | undefined> | string | null | undefined;
93
111
  onFinished?: () => Promise<void> | void;
94
112
  /**
95
113
  * The application's root container `id`.
@@ -120,6 +138,7 @@ interface ViteReactSSGContext<HasRouter extends boolean = true> {
120
138
  base: string;
121
139
  getStyleCollector: (() => StyleCollector | Promise<StyleCollector>) | null;
122
140
  app?: HasRouter extends true ? never : ReactNode;
141
+ routerType: 'remix' | 'single-page';
123
142
  }
124
143
  interface ViteReactSSGClientOptions {
125
144
  transformState?: (state: any) => any;
@@ -176,4 +195,4 @@ declare module 'vite' {
176
195
  }
177
196
  }
178
197
 
179
- export type { IndexRouteRecord as I, NonIndexRouteRecord as N, RouteRecord as R, StyleCollector as S, ViteReactSSGContext as V, ViteReactSSGClientOptions as a, ViteReactSSGOptions as b, RouterOptions as c };
198
+ export type { CrittersOptions as C, IndexRouteRecord as I, NonIndexRouteRecord as N, RouteRecord as R, StyleCollector as S, ViteReactSSGContext as V, ViteReactSSGClientOptions as a, ViteReactSSGOptions as b, RouterOptions as c };
@@ -1,9 +1,27 @@
1
- import { Options } from 'critters';
2
1
  import { ReactNode, ReactElement } from 'react';
3
2
  import { NonIndexRouteObject, IndexRouteObject, createBrowserRouter } from 'react-router-dom';
4
3
 
5
4
  type Router = ReturnType<typeof createBrowserRouter>;
6
- interface ViteReactSSGOptions {
5
+ interface CrittersOptions {
6
+ path?: string;
7
+ publicPath?: string;
8
+ external?: boolean;
9
+ inlineThreshold?: number;
10
+ minimumExternalSize?: number;
11
+ pruneSource?: boolean;
12
+ mergeStylesheets?: boolean;
13
+ additionalStylesheets?: string[];
14
+ preload?: 'body' | 'media' | 'swap' | 'js' | 'js-lazy';
15
+ noscriptFallback?: boolean;
16
+ inlineFonts?: boolean;
17
+ preloadFonts?: boolean;
18
+ fonts?: boolean;
19
+ keyframes?: string;
20
+ compress?: boolean;
21
+ logLevel?: 'info' | 'warn' | 'error' | 'trace' | 'debug' | 'silent';
22
+ reduceInlineStyles?: boolean;
23
+ }
24
+ interface ViteReactSSGOptions<Context = ViteReactSSGContext> {
7
25
  /**
8
26
  * Set the scripts' loading mode. Only works for `type="module"`.
9
27
  *
@@ -62,7 +80,7 @@ interface ViteReactSSGOptions {
62
80
  *
63
81
  * @see https://github.com/GoogleChromeLabs/critters
64
82
  */
65
- crittersOptions?: Options | false;
83
+ crittersOptions?: CrittersOptions | false;
66
84
  /**
67
85
  * Custom function to modify the routes to do the SSG.
68
86
  *
@@ -80,7 +98,7 @@ interface ViteReactSSGOptions {
80
98
  * To do so, you can change the 'index.html' file contents (passed in through the `indexHTML` parameter), and return it.
81
99
  * The returned value will then be passed to renderer.
82
100
  */
83
- onBeforePageRender?: (route: string, indexHTML: string, appCtx: ViteReactSSGContext<true>) => Promise<string | null | undefined> | string | null | undefined;
101
+ onBeforePageRender?: (route: string, indexHTML: string, appCtx: Context) => Promise<string | null | undefined> | string | null | undefined;
84
102
  /**
85
103
  * Callback to be called on every rendered page.
86
104
  *
@@ -89,7 +107,7 @@ interface ViteReactSSGOptions {
89
107
  * To do so, you can transform the route's rendered HTML (passed in through the `renderedHTML` parameter), and return it.
90
108
  * The returned value will be used as the HTML of the route.
91
109
  */
92
- onPageRendered?: (route: string, renderedHTML: string, appCtx: ViteReactSSGContext<true>) => Promise<string | null | undefined> | string | null | undefined;
110
+ onPageRendered?: (route: string, renderedHTML: string, appCtx: Context) => Promise<string | null | undefined> | string | null | undefined;
93
111
  onFinished?: () => Promise<void> | void;
94
112
  /**
95
113
  * The application's root container `id`.
@@ -120,6 +138,7 @@ interface ViteReactSSGContext<HasRouter extends boolean = true> {
120
138
  base: string;
121
139
  getStyleCollector: (() => StyleCollector | Promise<StyleCollector>) | null;
122
140
  app?: HasRouter extends true ? never : ReactNode;
141
+ routerType: 'remix' | 'single-page';
123
142
  }
124
143
  interface ViteReactSSGClientOptions {
125
144
  transformState?: (state: any) => any;
@@ -176,4 +195,4 @@ declare module 'vite' {
176
195
  }
177
196
  }
178
197
 
179
- export type { IndexRouteRecord as I, NonIndexRouteRecord as N, RouteRecord as R, StyleCollector as S, ViteReactSSGContext as V, ViteReactSSGClientOptions as a, ViteReactSSGOptions as b, RouterOptions as c };
198
+ export type { CrittersOptions as C, IndexRouteRecord as I, NonIndexRouteRecord as N, RouteRecord as R, StyleCollector as S, ViteReactSSGContext as V, ViteReactSSGClientOptions as a, ViteReactSSGOptions as b, RouterOptions as c };
@@ -1,9 +1,27 @@
1
- import { Options } from 'critters';
2
1
  import { ReactNode, ReactElement } from 'react';
3
2
  import { NonIndexRouteObject, IndexRouteObject, createBrowserRouter } from 'react-router-dom';
4
3
 
5
4
  type Router = ReturnType<typeof createBrowserRouter>;
6
- interface ViteReactSSGOptions {
5
+ interface CrittersOptions {
6
+ path?: string;
7
+ publicPath?: string;
8
+ external?: boolean;
9
+ inlineThreshold?: number;
10
+ minimumExternalSize?: number;
11
+ pruneSource?: boolean;
12
+ mergeStylesheets?: boolean;
13
+ additionalStylesheets?: string[];
14
+ preload?: 'body' | 'media' | 'swap' | 'js' | 'js-lazy';
15
+ noscriptFallback?: boolean;
16
+ inlineFonts?: boolean;
17
+ preloadFonts?: boolean;
18
+ fonts?: boolean;
19
+ keyframes?: string;
20
+ compress?: boolean;
21
+ logLevel?: 'info' | 'warn' | 'error' | 'trace' | 'debug' | 'silent';
22
+ reduceInlineStyles?: boolean;
23
+ }
24
+ interface ViteReactSSGOptions<Context = ViteReactSSGContext> {
7
25
  /**
8
26
  * Set the scripts' loading mode. Only works for `type="module"`.
9
27
  *
@@ -62,7 +80,7 @@ interface ViteReactSSGOptions {
62
80
  *
63
81
  * @see https://github.com/GoogleChromeLabs/critters
64
82
  */
65
- crittersOptions?: Options | false;
83
+ crittersOptions?: CrittersOptions | false;
66
84
  /**
67
85
  * Custom function to modify the routes to do the SSG.
68
86
  *
@@ -80,7 +98,7 @@ interface ViteReactSSGOptions {
80
98
  * To do so, you can change the 'index.html' file contents (passed in through the `indexHTML` parameter), and return it.
81
99
  * The returned value will then be passed to renderer.
82
100
  */
83
- onBeforePageRender?: (route: string, indexHTML: string, appCtx: ViteReactSSGContext<true>) => Promise<string | null | undefined> | string | null | undefined;
101
+ onBeforePageRender?: (route: string, indexHTML: string, appCtx: Context) => Promise<string | null | undefined> | string | null | undefined;
84
102
  /**
85
103
  * Callback to be called on every rendered page.
86
104
  *
@@ -89,7 +107,7 @@ interface ViteReactSSGOptions {
89
107
  * To do so, you can transform the route's rendered HTML (passed in through the `renderedHTML` parameter), and return it.
90
108
  * The returned value will be used as the HTML of the route.
91
109
  */
92
- onPageRendered?: (route: string, renderedHTML: string, appCtx: ViteReactSSGContext<true>) => Promise<string | null | undefined> | string | null | undefined;
110
+ onPageRendered?: (route: string, renderedHTML: string, appCtx: Context) => Promise<string | null | undefined> | string | null | undefined;
93
111
  onFinished?: () => Promise<void> | void;
94
112
  /**
95
113
  * The application's root container `id`.
@@ -120,6 +138,7 @@ interface ViteReactSSGContext<HasRouter extends boolean = true> {
120
138
  base: string;
121
139
  getStyleCollector: (() => StyleCollector | Promise<StyleCollector>) | null;
122
140
  app?: HasRouter extends true ? never : ReactNode;
141
+ routerType: 'remix' | 'single-page';
123
142
  }
124
143
  interface ViteReactSSGClientOptions {
125
144
  transformState?: (state: any) => any;
@@ -176,4 +195,4 @@ declare module 'vite' {
176
195
  }
177
196
  }
178
197
 
179
- export type { IndexRouteRecord as I, NonIndexRouteRecord as N, RouteRecord as R, StyleCollector as S, ViteReactSSGContext as V, ViteReactSSGClientOptions as a, ViteReactSSGOptions as b, RouterOptions as c };
198
+ export type { CrittersOptions as C, IndexRouteRecord as I, NonIndexRouteRecord as N, RouteRecord as R, StyleCollector as S, ViteReactSSGContext as V, ViteReactSSGClientOptions as a, ViteReactSSGOptions as b, RouterOptions as c };
@@ -0,0 +1,150 @@
1
+ 'use strict';
2
+
3
+ const jsxRuntime = require('react/jsx-runtime');
4
+ const React = require('react');
5
+ const client = require('react-dom/client');
6
+ const reactHelmetAsync = require('react-helmet-async');
7
+ const reactRouter = require('@tanstack/react-router');
8
+ const start = require('@tanstack/start');
9
+ const ClientOnly = require('./shared/vite-react-ssg.BuZVBVT6.cjs');
10
+ const state = require('./shared/vite-react-ssg.CjsEygxB.cjs');
11
+ const tanstackRouter = require('./shared/vite-react-ssg.D0EGioth.cjs');
12
+ const path = require('./shared/vite-react-ssg.CFQGqC60.cjs');
13
+
14
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
15
+
16
+ const React__default = /*#__PURE__*/_interopDefaultCompat(React);
17
+
18
+ const HAS_ADD_META_FLAG_KEY = "HAS_ADD_META_FLAG_KEY";
19
+ function ViteReactSSG(routerOptions, fn, options = {}) {
20
+ const {
21
+ transformState,
22
+ rootContainer = "#root",
23
+ ssrWhenDev,
24
+ getStyleCollector = null
25
+ } = options;
26
+ if (undefined.DEV && ssrWhenDev !== void 0)
27
+ console.warn("[vite-react-ssg] `ssrWhenDev` option is no longer needed. If you want to use csr, just replace `vite-react-ssg dev` with `vite`.");
28
+ const isClient = typeof window !== "undefined";
29
+ const BASE_URL = routerOptions.basename ?? "/";
30
+ const routeTree = routerOptions.routes;
31
+ const OriginComponent = routeTree.options.component;
32
+ if (!OriginComponent[HAS_ADD_META_FLAG_KEY]) {
33
+ const component = () => /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
34
+ /* @__PURE__ */ jsxRuntime.jsx(OriginComponent, {}),
35
+ /* @__PURE__ */ jsxRuntime.jsx("div", { id: tanstackRouter.META_CONTAINER_ID, style: { display: "none" }, children: /* @__PURE__ */ jsxRuntime.jsx(start.Meta, {}) })
36
+ ] });
37
+ component[HAS_ADD_META_FLAG_KEY] = true;
38
+ routeTree.update({
39
+ component
40
+ });
41
+ }
42
+ async function createRoot(client = false, routePath) {
43
+ const routes = await tanstackRouter.convertRouteTreeToRouteOption(
44
+ routerOptions.routes,
45
+ client,
46
+ client ? (node) => {
47
+ const isSSR = document.querySelector("[data-server-rendered=true]") !== null;
48
+ if (!isSSR)
49
+ return;
50
+ node.options.loader = async (ctx) => {
51
+ let pathname = ctx.location.pathname;
52
+ if (undefined.DEV) {
53
+ const routeId = encodeURIComponent(node.id);
54
+ const dataQuery = `_data=${routeId}`;
55
+ const href = ctx.location.href;
56
+ const url = href.includes("?") ? `${href}&${dataQuery}` : `${href}?${dataQuery}`;
57
+ const res = await fetch(url);
58
+ const header = res.headers;
59
+ const contentType = header.get("content-type");
60
+ if (contentType?.startsWith("application/json"))
61
+ return res.json();
62
+ return res.text();
63
+ }
64
+ let staticLoadData;
65
+ if (window.__VITE_REACT_SSG_STATIC_LOADER_DATA__) {
66
+ staticLoadData = window.__VITE_REACT_SSG_STATIC_LOADER_DATA__;
67
+ } else {
68
+ const manifestUrl = path.joinUrlSegments(BASE_URL, `static-loader-data-manifest-${window.__VITE_REACT_SSG_HASH__}.json`);
69
+ staticLoadData = await (await fetch(path.withLeadingSlash(manifestUrl))).json();
70
+ window.__VITE_REACT_SSG_STATIC_LOADER_DATA__ = staticLoadData;
71
+ }
72
+ if (BASE_URL !== "/") {
73
+ pathname = path.stripBase(pathname, BASE_URL);
74
+ }
75
+ const routeData = staticLoadData?.[pathname]?.find((item) => item.id === node.id);
76
+ return routeData?.loaderData ?? null;
77
+ };
78
+ } : void 0
79
+ );
80
+ const router = routerOptions.router;
81
+ router.options.isServer = !client;
82
+ const appRenderCallbacks = [];
83
+ const onSSRAppRendered = client ? () => {
84
+ } : (cb) => appRenderCallbacks.push(cb);
85
+ const triggerOnSSRAppRendered = () => {
86
+ return Promise.all(appRenderCallbacks.map((cb) => cb()));
87
+ };
88
+ const context = {
89
+ isClient,
90
+ routes,
91
+ routeTree,
92
+ router,
93
+ routerOptions: {
94
+ routes,
95
+ basename: BASE_URL
96
+ },
97
+ onSSRAppRendered,
98
+ triggerOnSSRAppRendered,
99
+ initialState: {},
100
+ transformState,
101
+ routePath,
102
+ base: BASE_URL,
103
+ getStyleCollector,
104
+ routerType: "tanstack"
105
+ };
106
+ if (client) {
107
+ await ClientOnly.documentReady();
108
+ context.initialState = transformState?.(window.__INITIAL_STATE__ || {}) || state.deserializeState(window.__INITIAL_STATE__);
109
+ }
110
+ await fn?.(context);
111
+ const initialState = context.initialState;
112
+ return {
113
+ ...context,
114
+ initialState
115
+ };
116
+ }
117
+ if (isClient) {
118
+ (async () => {
119
+ const container = typeof rootContainer === "string" ? document.querySelector(rootContainer) : rootContainer;
120
+ if (!container) {
121
+ if (typeof $jsdom === "undefined")
122
+ console.warn("[vite-react-ssg] Root container not found.");
123
+ return;
124
+ }
125
+ const { router } = await createRoot(true);
126
+ const isSSR = document.querySelector("[data-server-rendered=true]") !== null;
127
+ if (!isSSR && undefined.DEV) {
128
+ const root = client.createRoot(container);
129
+ React__default.startTransition(() => {
130
+ root.render(
131
+ /* @__PURE__ */ jsxRuntime.jsx(reactHelmetAsync.HelmetProvider, { children: /* @__PURE__ */ jsxRuntime.jsx(reactRouter.RouterProvider, { router }) })
132
+ );
133
+ });
134
+ } else {
135
+ React__default.startTransition(() => {
136
+ client.hydrateRoot(
137
+ container,
138
+ /* @__PURE__ */ jsxRuntime.jsx(reactHelmetAsync.HelmetProvider, { children: /* @__PURE__ */ jsxRuntime.jsx(start.StartClient, { router }) })
139
+ );
140
+ });
141
+ }
142
+ })();
143
+ }
144
+ return createRoot;
145
+ }
146
+
147
+ exports.ClientOnly = ClientOnly.ClientOnly;
148
+ exports.Head = ClientOnly.Head;
149
+ exports.registerPaths = tanstackRouter.registerPaths;
150
+ exports.ViteReactSSG = ViteReactSSG;
@@ -0,0 +1,29 @@
1
+ import { AnyRouter } from '@tanstack/react-router';
2
+ import { V as ViteReactSSGContext$1, a as ViteReactSSGClientOptions } from './shared/vite-react-ssg.Txy2EJkI.cjs';
3
+ export { C as CrittersOptions, I as IndexRouteRecord, N as NonIndexRouteRecord, R as RouteRecord, S as StyleCollector, b as ViteReactSSGOptions } from './shared/vite-react-ssg.Txy2EJkI.cjs';
4
+ export { C as ClientOnly, H as Head } from './shared/vite-react-ssg.Cm9gBlfg.cjs';
5
+ import 'react';
6
+ import 'react-router-dom';
7
+ import 'react-helmet-async';
8
+
9
+ declare function registerPaths(id: string, getStaticPaths: () => string[] | Promise<string[]>): void;
10
+
11
+ interface RouterOptions {
12
+ router: AnyRouter;
13
+ routes: AnyRouter['routeTree'];
14
+ basename?: string;
15
+ }
16
+ type ViteReactSSGContext<HasRouter extends boolean = true> = Omit<ViteReactSSGContext$1, 'router' | 'routerType'> & {
17
+ router: HasRouter extends true ? AnyRouter : undefined;
18
+ routeTree?: AnyRouter['routeTree'];
19
+ routerType: 'tanstack';
20
+ };
21
+ declare function ViteReactSSG(routerOptions: RouterOptions, fn?: (context: ViteReactSSGContext<true>) => Promise<void> | void, options?: ViteReactSSGClientOptions): (client?: boolean, routePath?: string) => Promise<ViteReactSSGContext<true>>;
22
+ declare global {
23
+ interface Window {
24
+ __VITE_REACT_SSG_STATIC_LOADER_DATA__: any;
25
+ __VITE_REACT_SSG_HASH__: string;
26
+ }
27
+ }
28
+
29
+ export { type RouterOptions, ViteReactSSG, ViteReactSSGClientOptions, type ViteReactSSGContext, registerPaths };
@@ -0,0 +1,29 @@
1
+ import { AnyRouter } from '@tanstack/react-router';
2
+ import { V as ViteReactSSGContext$1, a as ViteReactSSGClientOptions } from './shared/vite-react-ssg.Txy2EJkI.mjs';
3
+ export { C as CrittersOptions, I as IndexRouteRecord, N as NonIndexRouteRecord, R as RouteRecord, S as StyleCollector, b as ViteReactSSGOptions } from './shared/vite-react-ssg.Txy2EJkI.mjs';
4
+ export { C as ClientOnly, H as Head } from './shared/vite-react-ssg.Cm9gBlfg.mjs';
5
+ import 'react';
6
+ import 'react-router-dom';
7
+ import 'react-helmet-async';
8
+
9
+ declare function registerPaths(id: string, getStaticPaths: () => string[] | Promise<string[]>): void;
10
+
11
+ interface RouterOptions {
12
+ router: AnyRouter;
13
+ routes: AnyRouter['routeTree'];
14
+ basename?: string;
15
+ }
16
+ type ViteReactSSGContext<HasRouter extends boolean = true> = Omit<ViteReactSSGContext$1, 'router' | 'routerType'> & {
17
+ router: HasRouter extends true ? AnyRouter : undefined;
18
+ routeTree?: AnyRouter['routeTree'];
19
+ routerType: 'tanstack';
20
+ };
21
+ declare function ViteReactSSG(routerOptions: RouterOptions, fn?: (context: ViteReactSSGContext<true>) => Promise<void> | void, options?: ViteReactSSGClientOptions): (client?: boolean, routePath?: string) => Promise<ViteReactSSGContext<true>>;
22
+ declare global {
23
+ interface Window {
24
+ __VITE_REACT_SSG_STATIC_LOADER_DATA__: any;
25
+ __VITE_REACT_SSG_HASH__: string;
26
+ }
27
+ }
28
+
29
+ export { type RouterOptions, ViteReactSSG, ViteReactSSGClientOptions, type ViteReactSSGContext, registerPaths };
@@ -0,0 +1,29 @@
1
+ import { AnyRouter } from '@tanstack/react-router';
2
+ import { V as ViteReactSSGContext$1, a as ViteReactSSGClientOptions } from './shared/vite-react-ssg.Txy2EJkI.js';
3
+ export { C as CrittersOptions, I as IndexRouteRecord, N as NonIndexRouteRecord, R as RouteRecord, S as StyleCollector, b as ViteReactSSGOptions } from './shared/vite-react-ssg.Txy2EJkI.js';
4
+ export { C as ClientOnly, H as Head } from './shared/vite-react-ssg.Cm9gBlfg.js';
5
+ import 'react';
6
+ import 'react-router-dom';
7
+ import 'react-helmet-async';
8
+
9
+ declare function registerPaths(id: string, getStaticPaths: () => string[] | Promise<string[]>): void;
10
+
11
+ interface RouterOptions {
12
+ router: AnyRouter;
13
+ routes: AnyRouter['routeTree'];
14
+ basename?: string;
15
+ }
16
+ type ViteReactSSGContext<HasRouter extends boolean = true> = Omit<ViteReactSSGContext$1, 'router' | 'routerType'> & {
17
+ router: HasRouter extends true ? AnyRouter : undefined;
18
+ routeTree?: AnyRouter['routeTree'];
19
+ routerType: 'tanstack';
20
+ };
21
+ declare function ViteReactSSG(routerOptions: RouterOptions, fn?: (context: ViteReactSSGContext<true>) => Promise<void> | void, options?: ViteReactSSGClientOptions): (client?: boolean, routePath?: string) => Promise<ViteReactSSGContext<true>>;
22
+ declare global {
23
+ interface Window {
24
+ __VITE_REACT_SSG_STATIC_LOADER_DATA__: any;
25
+ __VITE_REACT_SSG_HASH__: string;
26
+ }
27
+ }
28
+
29
+ export { type RouterOptions, ViteReactSSG, ViteReactSSGClientOptions, type ViteReactSSGContext, registerPaths };
@@ -0,0 +1,143 @@
1
+ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
2
+ import React from 'react';
3
+ import { createRoot, hydrateRoot } from 'react-dom/client';
4
+ import { HelmetProvider } from 'react-helmet-async';
5
+ import { RouterProvider } from '@tanstack/react-router';
6
+ import { StartClient, Meta } from '@tanstack/start';
7
+ import { d as documentReady } from './shared/vite-react-ssg.B6SGTWpD.mjs';
8
+ export { C as ClientOnly, H as Head } from './shared/vite-react-ssg.B6SGTWpD.mjs';
9
+ import { d as deserializeState } from './shared/vite-react-ssg.C6pK7rvr.mjs';
10
+ import { M as META_CONTAINER_ID, c as convertRouteTreeToRouteOption } from './shared/vite-react-ssg.4y2OEiyV.mjs';
11
+ export { r as registerPaths } from './shared/vite-react-ssg.4y2OEiyV.mjs';
12
+ import { j as joinUrlSegments, w as withLeadingSlash, s as stripBase } from './shared/vite-react-ssg.CjIppNIS.mjs';
13
+
14
+ const HAS_ADD_META_FLAG_KEY = "HAS_ADD_META_FLAG_KEY";
15
+ function ViteReactSSG(routerOptions, fn, options = {}) {
16
+ const {
17
+ transformState,
18
+ rootContainer = "#root",
19
+ ssrWhenDev,
20
+ getStyleCollector = null
21
+ } = options;
22
+ if (import.meta.env.DEV && ssrWhenDev !== void 0)
23
+ console.warn("[vite-react-ssg] `ssrWhenDev` option is no longer needed. If you want to use csr, just replace `vite-react-ssg dev` with `vite`.");
24
+ const isClient = typeof window !== "undefined";
25
+ const BASE_URL = routerOptions.basename ?? "/";
26
+ const routeTree = routerOptions.routes;
27
+ const OriginComponent = routeTree.options.component;
28
+ if (!OriginComponent[HAS_ADD_META_FLAG_KEY]) {
29
+ const component = () => /* @__PURE__ */ jsxs(Fragment, { children: [
30
+ /* @__PURE__ */ jsx(OriginComponent, {}),
31
+ /* @__PURE__ */ jsx("div", { id: META_CONTAINER_ID, style: { display: "none" }, children: /* @__PURE__ */ jsx(Meta, {}) })
32
+ ] });
33
+ component[HAS_ADD_META_FLAG_KEY] = true;
34
+ routeTree.update({
35
+ component
36
+ });
37
+ }
38
+ async function createRoot$1(client = false, routePath) {
39
+ const routes = await convertRouteTreeToRouteOption(
40
+ routerOptions.routes,
41
+ client,
42
+ client ? (node) => {
43
+ const isSSR = document.querySelector("[data-server-rendered=true]") !== null;
44
+ if (!isSSR)
45
+ return;
46
+ node.options.loader = async (ctx) => {
47
+ let pathname = ctx.location.pathname;
48
+ if (import.meta.env.DEV) {
49
+ const routeId = encodeURIComponent(node.id);
50
+ const dataQuery = `_data=${routeId}`;
51
+ const href = ctx.location.href;
52
+ const url = href.includes("?") ? `${href}&${dataQuery}` : `${href}?${dataQuery}`;
53
+ const res = await fetch(url);
54
+ const header = res.headers;
55
+ const contentType = header.get("content-type");
56
+ if (contentType?.startsWith("application/json"))
57
+ return res.json();
58
+ return res.text();
59
+ }
60
+ let staticLoadData;
61
+ if (window.__VITE_REACT_SSG_STATIC_LOADER_DATA__) {
62
+ staticLoadData = window.__VITE_REACT_SSG_STATIC_LOADER_DATA__;
63
+ } else {
64
+ const manifestUrl = joinUrlSegments(BASE_URL, `static-loader-data-manifest-${window.__VITE_REACT_SSG_HASH__}.json`);
65
+ staticLoadData = await (await fetch(withLeadingSlash(manifestUrl))).json();
66
+ window.__VITE_REACT_SSG_STATIC_LOADER_DATA__ = staticLoadData;
67
+ }
68
+ if (BASE_URL !== "/") {
69
+ pathname = stripBase(pathname, BASE_URL);
70
+ }
71
+ const routeData = staticLoadData?.[pathname]?.find((item) => item.id === node.id);
72
+ return routeData?.loaderData ?? null;
73
+ };
74
+ } : void 0
75
+ );
76
+ const router = routerOptions.router;
77
+ router.options.isServer = !client;
78
+ const appRenderCallbacks = [];
79
+ const onSSRAppRendered = client ? () => {
80
+ } : (cb) => appRenderCallbacks.push(cb);
81
+ const triggerOnSSRAppRendered = () => {
82
+ return Promise.all(appRenderCallbacks.map((cb) => cb()));
83
+ };
84
+ const context = {
85
+ isClient,
86
+ routes,
87
+ routeTree,
88
+ router,
89
+ routerOptions: {
90
+ routes,
91
+ basename: BASE_URL
92
+ },
93
+ onSSRAppRendered,
94
+ triggerOnSSRAppRendered,
95
+ initialState: {},
96
+ transformState,
97
+ routePath,
98
+ base: BASE_URL,
99
+ getStyleCollector,
100
+ routerType: "tanstack"
101
+ };
102
+ if (client) {
103
+ await documentReady();
104
+ context.initialState = transformState?.(window.__INITIAL_STATE__ || {}) || deserializeState(window.__INITIAL_STATE__);
105
+ }
106
+ await fn?.(context);
107
+ const initialState = context.initialState;
108
+ return {
109
+ ...context,
110
+ initialState
111
+ };
112
+ }
113
+ if (isClient) {
114
+ (async () => {
115
+ const container = typeof rootContainer === "string" ? document.querySelector(rootContainer) : rootContainer;
116
+ if (!container) {
117
+ if (typeof $jsdom === "undefined")
118
+ console.warn("[vite-react-ssg] Root container not found.");
119
+ return;
120
+ }
121
+ const { router } = await createRoot$1(true);
122
+ const isSSR = document.querySelector("[data-server-rendered=true]") !== null;
123
+ if (!isSSR && import.meta.env.DEV) {
124
+ const root = createRoot(container);
125
+ React.startTransition(() => {
126
+ root.render(
127
+ /* @__PURE__ */ jsx(HelmetProvider, { children: /* @__PURE__ */ jsx(RouterProvider, { router }) })
128
+ );
129
+ });
130
+ } else {
131
+ React.startTransition(() => {
132
+ hydrateRoot(
133
+ container,
134
+ /* @__PURE__ */ jsx(HelmetProvider, { children: /* @__PURE__ */ jsx(StartClient, { router }) })
135
+ );
136
+ });
137
+ }
138
+ })();
139
+ }
140
+ return createRoot$1;
141
+ }
142
+
143
+ export { ViteReactSSG };