vite-react-ssg 0.0.3 → 0.1.1

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.
@@ -6,85 +6,85 @@ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'defau
6
6
 
7
7
  const JSDOM__default = /*#__PURE__*/_interopDefaultCompat(JSDOM);
8
8
 
9
- /*
10
- MIT License
11
-
12
- Copyright for portions of global-jsdom are held by Rico Sta. Cruz, 2016 as part of
13
- jsdom-global. All other copyright for global-jsdom are held by jonathan schatz, 2017.
14
-
15
- Permission is hereby granted, free of charge, to any person obtaining a copy
16
- of this software and associated documentation files (the "Software"), to deal
17
- in the Software without restriction, including without limitation the rights
18
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
- copies of the Software, and to permit persons to whom the Software is
20
- furnished to do so, subject to the following conditions:
21
-
22
- The above copyright notice and this permission notice shall be included in all
23
- copies or substantial portions of the Software.
24
-
25
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
- SOFTWARE.
32
- */
33
-
34
-
35
- const defaultHtml = '<!doctype html><html><head><meta charset="utf-8"></head><body></body></html>';
36
-
37
- // define this here so that we only ever dynamically populate KEYS once.
38
-
39
- const KEYS = [];
40
-
41
- function jsdomGlobal(html = defaultHtml, options = {}) {
42
- // Idempotency
43
- if (global.navigator
44
- && global.navigator.userAgent
45
- && global.navigator.userAgent.includes('Node.js')
46
- && global.document
47
- && typeof global.document.destroy === 'function')
48
- return global.document.destroy
49
-
50
- // set a default url if we don't get one - otherwise things explode when we copy localstorage keys
51
- if (!('url' in options))
52
- Object.assign(options, { url: 'http://localhost:3000' });
53
-
54
- // enable pretendToBeVisual by default since react needs
55
- // window.requestAnimationFrame, see https://github.com/jsdom/jsdom#pretending-to-be-a-visual-browser
56
- if (!('pretendToBeVisual' in options))
57
- Object.assign(options, { pretendToBeVisual: true });
58
-
59
- const jsdom = new JSDOM__default.JSDOM(html, options);
60
- const { window } = jsdom;
61
- const { document } = window;
62
-
63
- // generate our list of keys by enumerating document.window - this list may vary
64
- // based on the jsdom version. filter out internal methods as well as anything
65
- // that node already defines
66
-
67
- if (KEYS.length === 0) {
68
- KEYS.push(...Object.getOwnPropertyNames(window).filter(k => !k.startsWith('_')).filter(k => !(k in global)));
69
- // going to add our jsdom instance, see below
70
- KEYS.push('$jsdom');
71
- }
72
-
73
- KEYS.forEach(key => global[key] = window[key]);
74
-
75
- // setup document / window / window.console
76
- global.document = document;
77
- global.window = window;
78
- window.console = global.console;
79
-
80
- // add access to our jsdom instance
81
- global.$jsdom = jsdom;
82
-
83
- const cleanup = () => KEYS.forEach(key => delete global[key]);
84
-
85
- document.destroy = cleanup;
86
-
87
- return cleanup
9
+ /*
10
+ MIT License
11
+
12
+ Copyright for portions of global-jsdom are held by Rico Sta. Cruz, 2016 as part of
13
+ jsdom-global. All other copyright for global-jsdom are held by jonathan schatz, 2017.
14
+
15
+ Permission is hereby granted, free of charge, to any person obtaining a copy
16
+ of this software and associated documentation files (the "Software"), to deal
17
+ in the Software without restriction, including without limitation the rights
18
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
+ copies of the Software, and to permit persons to whom the Software is
20
+ furnished to do so, subject to the following conditions:
21
+
22
+ The above copyright notice and this permission notice shall be included in all
23
+ copies or substantial portions of the Software.
24
+
25
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
+ SOFTWARE.
32
+ */
33
+
34
+
35
+ const defaultHtml = '<!doctype html><html><head><meta charset="utf-8"></head><body></body></html>';
36
+
37
+ // define this here so that we only ever dynamically populate KEYS once.
38
+
39
+ const KEYS = [];
40
+
41
+ function jsdomGlobal(html = defaultHtml, options = {}) {
42
+ // Idempotency
43
+ if (global.navigator
44
+ && global.navigator.userAgent
45
+ && global.navigator.userAgent.includes('Node.js')
46
+ && global.document
47
+ && typeof global.document.destroy === 'function')
48
+ return global.document.destroy
49
+
50
+ // set a default url if we don't get one - otherwise things explode when we copy localstorage keys
51
+ if (!('url' in options))
52
+ Object.assign(options, { url: 'http://localhost:3000' });
53
+
54
+ // enable pretendToBeVisual by default since react needs
55
+ // window.requestAnimationFrame, see https://github.com/jsdom/jsdom#pretending-to-be-a-visual-browser
56
+ if (!('pretendToBeVisual' in options))
57
+ Object.assign(options, { pretendToBeVisual: true });
58
+
59
+ const jsdom = new JSDOM__default.JSDOM(html, options);
60
+ const { window } = jsdom;
61
+ const { document } = window;
62
+
63
+ // generate our list of keys by enumerating document.window - this list may vary
64
+ // based on the jsdom version. filter out internal methods as well as anything
65
+ // that node already defines
66
+
67
+ if (KEYS.length === 0) {
68
+ KEYS.push(...Object.getOwnPropertyNames(window).filter(k => !k.startsWith('_')).filter(k => !(k in global)));
69
+ // going to add our jsdom instance, see below
70
+ KEYS.push('$jsdom');
71
+ }
72
+
73
+ KEYS.forEach(key => global[key] = window[key]);
74
+
75
+ // setup document / window / window.console
76
+ global.document = document;
77
+ global.window = window;
78
+ window.console = global.console;
79
+
80
+ // add access to our jsdom instance
81
+ global.$jsdom = jsdom;
82
+
83
+ const cleanup = () => KEYS.forEach(key => delete global[key]);
84
+
85
+ document.destroy = cleanup;
86
+
87
+ return cleanup
88
88
  }
89
89
 
90
90
  exports.jsdomGlobal = jsdomGlobal;
@@ -1,84 +1,84 @@
1
1
  import JSDOM from 'jsdom';
2
2
 
3
- /*
4
- MIT License
5
-
6
- Copyright for portions of global-jsdom are held by Rico Sta. Cruz, 2016 as part of
7
- jsdom-global. All other copyright for global-jsdom are held by jonathan schatz, 2017.
8
-
9
- Permission is hereby granted, free of charge, to any person obtaining a copy
10
- of this software and associated documentation files (the "Software"), to deal
11
- in the Software without restriction, including without limitation the rights
12
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
- copies of the Software, and to permit persons to whom the Software is
14
- furnished to do so, subject to the following conditions:
15
-
16
- The above copyright notice and this permission notice shall be included in all
17
- copies or substantial portions of the Software.
18
-
19
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
- SOFTWARE.
26
- */
27
-
28
-
29
- const defaultHtml = '<!doctype html><html><head><meta charset="utf-8"></head><body></body></html>';
30
-
31
- // define this here so that we only ever dynamically populate KEYS once.
32
-
33
- const KEYS = [];
34
-
35
- function jsdomGlobal(html = defaultHtml, options = {}) {
36
- // Idempotency
37
- if (global.navigator
38
- && global.navigator.userAgent
39
- && global.navigator.userAgent.includes('Node.js')
40
- && global.document
41
- && typeof global.document.destroy === 'function')
42
- return global.document.destroy
43
-
44
- // set a default url if we don't get one - otherwise things explode when we copy localstorage keys
45
- if (!('url' in options))
46
- Object.assign(options, { url: 'http://localhost:3000' });
47
-
48
- // enable pretendToBeVisual by default since react needs
49
- // window.requestAnimationFrame, see https://github.com/jsdom/jsdom#pretending-to-be-a-visual-browser
50
- if (!('pretendToBeVisual' in options))
51
- Object.assign(options, { pretendToBeVisual: true });
52
-
53
- const jsdom = new JSDOM.JSDOM(html, options);
54
- const { window } = jsdom;
55
- const { document } = window;
56
-
57
- // generate our list of keys by enumerating document.window - this list may vary
58
- // based on the jsdom version. filter out internal methods as well as anything
59
- // that node already defines
60
-
61
- if (KEYS.length === 0) {
62
- KEYS.push(...Object.getOwnPropertyNames(window).filter(k => !k.startsWith('_')).filter(k => !(k in global)));
63
- // going to add our jsdom instance, see below
64
- KEYS.push('$jsdom');
65
- }
66
-
67
- KEYS.forEach(key => global[key] = window[key]);
68
-
69
- // setup document / window / window.console
70
- global.document = document;
71
- global.window = window;
72
- window.console = global.console;
73
-
74
- // add access to our jsdom instance
75
- global.$jsdom = jsdom;
76
-
77
- const cleanup = () => KEYS.forEach(key => delete global[key]);
78
-
79
- document.destroy = cleanup;
80
-
81
- return cleanup
3
+ /*
4
+ MIT License
5
+
6
+ Copyright for portions of global-jsdom are held by Rico Sta. Cruz, 2016 as part of
7
+ jsdom-global. All other copyright for global-jsdom are held by jonathan schatz, 2017.
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ of this software and associated documentation files (the "Software"), to deal
11
+ in the Software without restriction, including without limitation the rights
12
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ copies of the Software, and to permit persons to whom the Software is
14
+ furnished to do so, subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in all
17
+ copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ SOFTWARE.
26
+ */
27
+
28
+
29
+ const defaultHtml = '<!doctype html><html><head><meta charset="utf-8"></head><body></body></html>';
30
+
31
+ // define this here so that we only ever dynamically populate KEYS once.
32
+
33
+ const KEYS = [];
34
+
35
+ function jsdomGlobal(html = defaultHtml, options = {}) {
36
+ // Idempotency
37
+ if (global.navigator
38
+ && global.navigator.userAgent
39
+ && global.navigator.userAgent.includes('Node.js')
40
+ && global.document
41
+ && typeof global.document.destroy === 'function')
42
+ return global.document.destroy
43
+
44
+ // set a default url if we don't get one - otherwise things explode when we copy localstorage keys
45
+ if (!('url' in options))
46
+ Object.assign(options, { url: 'http://localhost:3000' });
47
+
48
+ // enable pretendToBeVisual by default since react needs
49
+ // window.requestAnimationFrame, see https://github.com/jsdom/jsdom#pretending-to-be-a-visual-browser
50
+ if (!('pretendToBeVisual' in options))
51
+ Object.assign(options, { pretendToBeVisual: true });
52
+
53
+ const jsdom = new JSDOM.JSDOM(html, options);
54
+ const { window } = jsdom;
55
+ const { document } = window;
56
+
57
+ // generate our list of keys by enumerating document.window - this list may vary
58
+ // based on the jsdom version. filter out internal methods as well as anything
59
+ // that node already defines
60
+
61
+ if (KEYS.length === 0) {
62
+ KEYS.push(...Object.getOwnPropertyNames(window).filter(k => !k.startsWith('_')).filter(k => !(k in global)));
63
+ // going to add our jsdom instance, see below
64
+ KEYS.push('$jsdom');
65
+ }
66
+
67
+ KEYS.forEach(key => global[key] = window[key]);
68
+
69
+ // setup document / window / window.console
70
+ global.document = document;
71
+ global.window = window;
72
+ window.console = global.console;
73
+
74
+ // add access to our jsdom instance
75
+ global.$jsdom = jsdom;
76
+
77
+ const cleanup = () => KEYS.forEach(key => delete global[key]);
78
+
79
+ document.destroy = cleanup;
80
+
81
+ return cleanup
82
82
  }
83
83
 
84
84
  export { jsdomGlobal };
package/dist/index.cjs CHANGED
@@ -19,10 +19,96 @@ function documentReady(_passThrough) {
19
19
  return Promise.resolve(_passThrough);
20
20
  }
21
21
 
22
+ function useIsClient() {
23
+ const [isBrowser, setIsBrowser] = React.useState(false);
24
+ React.useEffect(() => {
25
+ setIsBrowser(true);
26
+ }, []);
27
+ return isBrowser;
28
+ }
29
+
30
+ function ClientOnly({
31
+ children,
32
+ fallback
33
+ }) {
34
+ const isBrowser = useIsClient();
35
+ if (isBrowser) {
36
+ if (typeof children !== "function" && process.env.NODE_ENV === "development") {
37
+ throw new Error(`vite-react-ssg error: The children of <ClientOnly> must be a "render function", e.g. <ClientOnly>{() => <span>{window.location.href}</span>}</ClientOnly>.
38
+ Current type: ${React.isValidElement(children) ? "React element" : typeof children}`);
39
+ }
40
+ return /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, children?.());
41
+ }
42
+ return fallback ?? null;
43
+ }
44
+
45
+ const Link = React.forwardRef((props, ref) => {
46
+ const {
47
+ replace,
48
+ state,
49
+ target,
50
+ preventScrollReset,
51
+ relative,
52
+ to,
53
+ onClick
54
+ } = props;
55
+ const internalOnClick = reactRouterDom.useLinkClickHandler(to, {
56
+ replace,
57
+ state,
58
+ target,
59
+ preventScrollReset,
60
+ relative
61
+ });
62
+ function handleClick(event) {
63
+ if (onClick)
64
+ onClick(event);
65
+ if (!event.defaultPrevented) {
66
+ React__default.startTransition(() => {
67
+ internalOnClick(event);
68
+ });
69
+ }
70
+ event.defaultPrevented = true;
71
+ event.preventDefault();
72
+ }
73
+ return /* @__PURE__ */ React__default.createElement(reactRouterDom.Link, { ...props, ref, onClick: handleClick });
74
+ });
75
+ const NavLink = React.forwardRef((props, ref) => {
76
+ const {
77
+ replace,
78
+ state,
79
+ target,
80
+ preventScrollReset,
81
+ relative,
82
+ to,
83
+ onClick
84
+ } = props;
85
+ const internalOnClick = reactRouterDom.useLinkClickHandler(to, {
86
+ replace,
87
+ state,
88
+ target,
89
+ preventScrollReset,
90
+ relative
91
+ });
92
+ function handleClick(event) {
93
+ if (onClick)
94
+ onClick(event);
95
+ if (!event.defaultPrevented) {
96
+ React__default.startTransition(() => {
97
+ internalOnClick(event);
98
+ });
99
+ }
100
+ event.defaultPrevented = true;
101
+ event.preventDefault();
102
+ }
103
+ return /* @__PURE__ */ React__default.createElement(reactRouterDom.NavLink, { ...props, ref, onClick: handleClick });
104
+ });
105
+
22
106
  function ViteReactSSG(routerOptions, fn, options = {}) {
23
107
  const {
24
108
  transformState,
25
- rootContainer = "#root"
109
+ rootContainer = "#root",
110
+ ssrWhenDev = true,
111
+ getStyleCollector = null
26
112
  } = options;
27
113
  const isClient = typeof window !== "undefined";
28
114
  async function createRoot(client = false, routePath) {
@@ -34,7 +120,6 @@ function ViteReactSSG(routerOptions, fn, options = {}) {
34
120
  return Promise.all(appRenderCallbacks.map((cb) => cb()));
35
121
  };
36
122
  const context = {
37
- // app: App,
38
123
  isClient,
39
124
  routes: routerOptions.routes,
40
125
  router: browserRouter,
@@ -43,7 +128,8 @@ function ViteReactSSG(routerOptions, fn, options = {}) {
43
128
  triggerOnSSRAppRendered,
44
129
  initialState: {},
45
130
  transformState,
46
- routePath
131
+ routePath,
132
+ getStyleCollector
47
133
  };
48
134
  if (client) {
49
135
  await documentReady();
@@ -64,11 +150,24 @@ function ViteReactSSG(routerOptions, fn, options = {}) {
64
150
  (async () => {
65
151
  const container = typeof rootContainer === "string" ? document.querySelector(rootContainer) : rootContainer;
66
152
  const { router } = await createRoot(true);
67
- client.hydrateRoot(container, /* @__PURE__ */ React__default.createElement(reactHelmetAsync.HelmetProvider, null, /* @__PURE__ */ React__default.createElement(SiteMetadataDefaults.SiteMetadataDefaults, null), /* @__PURE__ */ React__default.createElement(reactRouterDom.RouterProvider, { router })));
153
+ const app = /* @__PURE__ */ React__default.createElement(reactHelmetAsync.HelmetProvider, null, /* @__PURE__ */ React__default.createElement(SiteMetadataDefaults.SiteMetadataDefaults, null), /* @__PURE__ */ React__default.createElement(reactRouterDom.RouterProvider, { router }));
154
+ if (!ssrWhenDev && undefined.DEV) {
155
+ const root = client.createRoot(container);
156
+ React__default.startTransition(() => {
157
+ root.render(app);
158
+ });
159
+ } else {
160
+ React__default.startTransition(() => {
161
+ client.hydrateRoot(container, app);
162
+ });
163
+ }
68
164
  })();
69
165
  }
70
166
  return createRoot;
71
167
  }
72
168
 
73
169
  exports.Head = SiteMetadataDefaults.Head;
170
+ exports.ClientOnly = ClientOnly;
171
+ exports.Link = Link;
172
+ exports.NavLink = NavLink;
74
173
  exports.ViteReactSSG = ViteReactSSG;
package/dist/index.d.ts CHANGED
@@ -1,15 +1,24 @@
1
- import { R as RouterOptions, V as ViteReactSSGContext, a as ViteReactSSGClientOptions } from './types-82f34756.js';
2
- export { I as IndexRouteRecord, N as NonIndexRouteRecord, c as RouteRecord, b as ViteReactSSGOptions } from './types-82f34756.js';
3
- import { ReactNode } from 'react';
1
+ import { R as RouterOptions, V as ViteReactSSGContext, a as ViteReactSSGClientOptions } from './types-408e974a.js';
2
+ export { I as IndexRouteRecord, N as NonIndexRouteRecord, c as RouteRecord, S as StyleCollector, b as ViteReactSSGOptions } from './types-408e974a.js';
3
+ import React, { ReactNode } from 'react';
4
4
  import { HelmetProps } from 'react-helmet-async';
5
+ import { LinkProps, NavLinkProps } from 'react-router-dom';
5
6
  import 'critters';
6
- import 'react-router-dom';
7
7
 
8
8
  type Props = HelmetProps & {
9
9
  children: ReactNode;
10
10
  };
11
11
  declare function Head(props: Props): JSX.Element;
12
12
 
13
+ interface ClientOnlyProps {
14
+ children?: () => JSX.Element;
15
+ fallback?: JSX.Element;
16
+ }
17
+ declare function ClientOnly({ children, fallback, }: ClientOnlyProps): JSX.Element | null;
18
+
19
+ declare const Link: React.ForwardRefExoticComponent<LinkProps & React.RefAttributes<HTMLAnchorElement>>;
20
+ declare const NavLink: React.ForwardRefExoticComponent<NavLinkProps & React.RefAttributes<HTMLAnchorElement>>;
21
+
13
22
  declare function ViteReactSSG(routerOptions: RouterOptions, fn?: (context: ViteReactSSGContext<true>) => Promise<void> | void, options?: ViteReactSSGClientOptions): (client?: boolean, routePath?: string) => Promise<ViteReactSSGContext<true>>;
14
23
 
15
- export { Head, RouterOptions, ViteReactSSG, ViteReactSSGClientOptions, ViteReactSSGContext };
24
+ export { ClientOnly, Head, Link, NavLink, RouterOptions, ViteReactSSG, ViteReactSSGClientOptions, ViteReactSSGContext };
package/dist/index.mjs CHANGED
@@ -1,8 +1,8 @@
1
- import React from 'react';
2
- import { hydrateRoot } from 'react-dom/client';
1
+ import React, { useState, useEffect, isValidElement, forwardRef } from 'react';
2
+ import { createRoot, hydrateRoot } from 'react-dom/client';
3
3
  import { HelmetProvider } from 'react-helmet-async';
4
- import { RouterProvider, createBrowserRouter } from 'react-router-dom';
5
- import { S as SiteMetadataDefaults, d as deserializeState } from './shared/vite-react-ssg.9d005d5e.mjs';
4
+ import { useLinkClickHandler, Link as Link$1, NavLink as NavLink$1, createBrowserRouter, RouterProvider } from 'react-router-dom';
5
+ import { d as deserializeState, S as SiteMetadataDefaults } from './shared/vite-react-ssg.9d005d5e.mjs';
6
6
  export { H as Head } from './shared/vite-react-ssg.9d005d5e.mjs';
7
7
 
8
8
  function documentReady(_passThrough) {
@@ -14,13 +14,99 @@ function documentReady(_passThrough) {
14
14
  return Promise.resolve(_passThrough);
15
15
  }
16
16
 
17
+ function useIsClient() {
18
+ const [isBrowser, setIsBrowser] = useState(false);
19
+ useEffect(() => {
20
+ setIsBrowser(true);
21
+ }, []);
22
+ return isBrowser;
23
+ }
24
+
25
+ function ClientOnly({
26
+ children,
27
+ fallback
28
+ }) {
29
+ const isBrowser = useIsClient();
30
+ if (isBrowser) {
31
+ if (typeof children !== "function" && process.env.NODE_ENV === "development") {
32
+ throw new Error(`vite-react-ssg error: The children of <ClientOnly> must be a "render function", e.g. <ClientOnly>{() => <span>{window.location.href}</span>}</ClientOnly>.
33
+ Current type: ${isValidElement(children) ? "React element" : typeof children}`);
34
+ }
35
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, children?.());
36
+ }
37
+ return fallback ?? null;
38
+ }
39
+
40
+ const Link = forwardRef((props, ref) => {
41
+ const {
42
+ replace,
43
+ state,
44
+ target,
45
+ preventScrollReset,
46
+ relative,
47
+ to,
48
+ onClick
49
+ } = props;
50
+ const internalOnClick = useLinkClickHandler(to, {
51
+ replace,
52
+ state,
53
+ target,
54
+ preventScrollReset,
55
+ relative
56
+ });
57
+ function handleClick(event) {
58
+ if (onClick)
59
+ onClick(event);
60
+ if (!event.defaultPrevented) {
61
+ React.startTransition(() => {
62
+ internalOnClick(event);
63
+ });
64
+ }
65
+ event.defaultPrevented = true;
66
+ event.preventDefault();
67
+ }
68
+ return /* @__PURE__ */ React.createElement(Link$1, { ...props, ref, onClick: handleClick });
69
+ });
70
+ const NavLink = forwardRef((props, ref) => {
71
+ const {
72
+ replace,
73
+ state,
74
+ target,
75
+ preventScrollReset,
76
+ relative,
77
+ to,
78
+ onClick
79
+ } = props;
80
+ const internalOnClick = useLinkClickHandler(to, {
81
+ replace,
82
+ state,
83
+ target,
84
+ preventScrollReset,
85
+ relative
86
+ });
87
+ function handleClick(event) {
88
+ if (onClick)
89
+ onClick(event);
90
+ if (!event.defaultPrevented) {
91
+ React.startTransition(() => {
92
+ internalOnClick(event);
93
+ });
94
+ }
95
+ event.defaultPrevented = true;
96
+ event.preventDefault();
97
+ }
98
+ return /* @__PURE__ */ React.createElement(NavLink$1, { ...props, ref, onClick: handleClick });
99
+ });
100
+
17
101
  function ViteReactSSG(routerOptions, fn, options = {}) {
18
102
  const {
19
103
  transformState,
20
- rootContainer = "#root"
104
+ rootContainer = "#root",
105
+ ssrWhenDev = true,
106
+ getStyleCollector = null
21
107
  } = options;
22
108
  const isClient = typeof window !== "undefined";
23
- async function createRoot(client = false, routePath) {
109
+ async function createRoot$1(client = false, routePath) {
24
110
  const browserRouter = client ? createBrowserRouter(routerOptions.routes) : void 0;
25
111
  const appRenderCallbacks = [];
26
112
  const onSSRAppRendered = client ? () => {
@@ -29,7 +115,6 @@ function ViteReactSSG(routerOptions, fn, options = {}) {
29
115
  return Promise.all(appRenderCallbacks.map((cb) => cb()));
30
116
  };
31
117
  const context = {
32
- // app: App,
33
118
  isClient,
34
119
  routes: routerOptions.routes,
35
120
  router: browserRouter,
@@ -38,7 +123,8 @@ function ViteReactSSG(routerOptions, fn, options = {}) {
38
123
  triggerOnSSRAppRendered,
39
124
  initialState: {},
40
125
  transformState,
41
- routePath
126
+ routePath,
127
+ getStyleCollector
42
128
  };
43
129
  if (client) {
44
130
  await documentReady();
@@ -58,11 +144,21 @@ function ViteReactSSG(routerOptions, fn, options = {}) {
58
144
  if (isClient) {
59
145
  (async () => {
60
146
  const container = typeof rootContainer === "string" ? document.querySelector(rootContainer) : rootContainer;
61
- const { router } = await createRoot(true);
62
- hydrateRoot(container, /* @__PURE__ */ React.createElement(HelmetProvider, null, /* @__PURE__ */ React.createElement(SiteMetadataDefaults, null), /* @__PURE__ */ React.createElement(RouterProvider, { router })));
147
+ const { router } = await createRoot$1(true);
148
+ const app = /* @__PURE__ */ React.createElement(HelmetProvider, null, /* @__PURE__ */ React.createElement(SiteMetadataDefaults, null), /* @__PURE__ */ React.createElement(RouterProvider, { router }));
149
+ if (!ssrWhenDev && import.meta.env.DEV) {
150
+ const root = createRoot(container);
151
+ React.startTransition(() => {
152
+ root.render(app);
153
+ });
154
+ } else {
155
+ React.startTransition(() => {
156
+ hydrateRoot(container, app);
157
+ });
158
+ }
63
159
  })();
64
160
  }
65
- return createRoot;
161
+ return createRoot$1;
66
162
  }
67
163
 
68
- export { ViteReactSSG };
164
+ export { ClientOnly, Link, NavLink, ViteReactSSG };