vite-ssg-optimized 0.28.0-optimized.2 → 0.28.0-optimized.4

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.
@@ -1,10 +1,11 @@
1
1
  import { createHead as createHead$1 } from '@unhead/vue/client';
2
2
  import { createHead } from '@unhead/vue/server';
3
- import { createSSRApp, createApp } from 'vue';
4
- import { C as ClientOnly, d as documentReady } from '../shared/vite-ssg-optimized.ETIvV-80.mjs';
3
+ import { createApp, createSSRApp } from 'vue';
4
+ import { g as getImportMetaEnv, C as ClientOnly, d as documentReady } from '../shared/vite-ssg-optimized.D6MbDXbJ.mjs';
5
5
  import { d as deserializeState } from '../shared/vite-ssg-optimized.C6pK7rvr.mjs';
6
6
 
7
7
  function ViteSSG(App, fn, options) {
8
+ const importMetaEnv = getImportMetaEnv();
8
9
  const {
9
10
  transformState,
10
11
  registerComponents = true,
@@ -12,15 +13,15 @@ function ViteSSG(App, fn, options) {
12
13
  rootContainer = "#app"
13
14
  } = options ?? {};
14
15
  async function createApp$1() {
15
- const isClient = !import.meta.env.SSR;
16
+ const isClient = !importMetaEnv.SSR;
16
17
  const isHydrationMode = options?.hydration || isClient && document.querySelectorAll("[data-server-rendered]").length > 0;
17
- const app = import.meta.env.SSR || isHydrationMode ? createSSRApp(App) : createApp(App);
18
+ const app = isClient && !isHydrationMode ? createApp(App) : createSSRApp(App);
18
19
  let head;
19
20
  if (useHead) {
20
- app.use(head = import.meta.env.SSR ? createHead() : createHead$1());
21
+ app.use(head = importMetaEnv.SSR ? createHead() : createHead$1());
21
22
  }
22
23
  const appRenderCallbacks = [];
23
- const onSSRAppRendered = import.meta.env.SSR ? (cb) => appRenderCallbacks.push(cb) : () => {
24
+ const onSSRAppRendered = importMetaEnv.SSR ? (cb) => appRenderCallbacks.push(cb) : () => {
24
25
  };
25
26
  const triggerOnSSRAppRendered = () => {
26
27
  return Promise.all(appRenderCallbacks.map((cb) => cb()));
@@ -28,7 +29,7 @@ function ViteSSG(App, fn, options) {
28
29
  const context = {
29
30
  app,
30
31
  head,
31
- isClient: !import.meta.env.SSR,
32
+ isClient: !importMetaEnv.SSR,
32
33
  router: void 0,
33
34
  routes: void 0,
34
35
  initialState: {},
@@ -38,7 +39,7 @@ function ViteSSG(App, fn, options) {
38
39
  };
39
40
  if (registerComponents)
40
41
  app.component("ClientOnly", ClientOnly);
41
- if (!import.meta.env.SSR) {
42
+ if (!importMetaEnv.SSR) {
42
43
  await documentReady();
43
44
  context.initialState = transformState?.(window.__INITIAL_STATE__ || {}) || deserializeState(window.__INITIAL_STATE__);
44
45
  }
@@ -49,7 +50,7 @@ function ViteSSG(App, fn, options) {
49
50
  initialState
50
51
  };
51
52
  }
52
- if (!import.meta.env.SSR) {
53
+ if (!importMetaEnv.SSR) {
53
54
  (async () => {
54
55
  const { app } = await createApp$1();
55
56
  app.mount(rootContainer, true);
package/dist/index.mjs CHANGED
@@ -2,7 +2,7 @@ import { createHead as createHead$1 } from '@unhead/vue/client';
2
2
  import { createHead } from '@unhead/vue/server';
3
3
  import { createApp, createSSRApp } from 'vue';
4
4
  import { createRouter, createMemoryHistory, createWebHistory } from 'vue-router';
5
- import { C as ClientOnly, d as documentReady } from './shared/vite-ssg-optimized.ETIvV-80.mjs';
5
+ import { g as getImportMetaEnv, C as ClientOnly, d as documentReady } from './shared/vite-ssg-optimized.D6MbDXbJ.mjs';
6
6
  import { d as deserializeState } from './shared/vite-ssg-optimized.C6pK7rvr.mjs';
7
7
 
8
8
  function ViteSSG(App, routerOptions, fn, options) {
@@ -12,23 +12,37 @@ function ViteSSG(App, routerOptions, fn, options) {
12
12
  useHead = true,
13
13
  rootContainer = "#app"
14
14
  } = options ?? {};
15
+ const importMetaEnv = getImportMetaEnv();
15
16
  async function createApp$1(routePath) {
16
- const isClient = !import.meta.env.SSR;
17
- const isHydrationMode = options?.hydration || isClient && document.querySelectorAll("[data-server-rendered]").length > 0;
18
- const app = import.meta.env.SSR || isHydrationMode ? createApp(App) : createSSRApp(App);
17
+ const isClient = !importMetaEnv.SSR;
18
+ async function detectHydrationMode() {
19
+ if (!isClient) return false;
20
+ return new Promise((resolve) => {
21
+ const fnResolve = () => {
22
+ if (document.readyState !== "loading") {
23
+ resolve(document.querySelectorAll("[data-server-rendered]").length > 0);
24
+ document.removeEventListener("readystatechange", fnResolve);
25
+ }
26
+ };
27
+ document.addEventListener("readystatechange", fnResolve);
28
+ fnResolve();
29
+ });
30
+ }
31
+ const isHydrationMode = options?.hydration || await detectHydrationMode();
32
+ const app = isClient && !isHydrationMode ? createApp(App) : createSSRApp(App);
19
33
  let head;
20
34
  if (useHead) {
21
- app.use(head = import.meta.env.SSR ? createHead() : createHead$1());
35
+ app.use(head = importMetaEnv.SSR ? createHead() : createHead$1());
22
36
  }
23
37
  const router = createRouter({
24
- history: import.meta.env.SSR ? createMemoryHistory(routerOptions.base) : createWebHistory(routerOptions.base),
38
+ history: importMetaEnv.SSR ? createMemoryHistory(routerOptions.base) : createWebHistory(routerOptions.base),
25
39
  ...routerOptions
26
40
  });
27
41
  const { routes } = routerOptions;
28
42
  if (registerComponents)
29
43
  app.component("ClientOnly", ClientOnly);
30
44
  const appRenderCallbacks = [];
31
- const onSSRAppRendered = import.meta.env.SSR ? (cb) => appRenderCallbacks.push(cb) : () => {
45
+ const onSSRAppRendered = importMetaEnv.SSR ? (cb) => appRenderCallbacks.push(cb) : () => {
32
46
  };
33
47
  const triggerOnSSRAppRendered = () => {
34
48
  return Promise.all(appRenderCallbacks.map((cb) => cb()));
@@ -36,7 +50,7 @@ function ViteSSG(App, routerOptions, fn, options) {
36
50
  const context = {
37
51
  app,
38
52
  head,
39
- isClient: !import.meta.env.SSR,
53
+ isClient: !importMetaEnv.SSR,
40
54
  router,
41
55
  routes,
42
56
  onSSRAppRendered,
@@ -45,7 +59,7 @@ function ViteSSG(App, routerOptions, fn, options) {
45
59
  transformState,
46
60
  routePath
47
61
  };
48
- if (!import.meta.env.SSR) {
62
+ if (!importMetaEnv.SSR) {
49
63
  await documentReady();
50
64
  context.initialState = transformState?.(window.__INITIAL_STATE__ || {}) || deserializeState(window.__INITIAL_STATE__);
51
65
  }
@@ -61,7 +75,7 @@ function ViteSSG(App, routerOptions, fn, options) {
61
75
  }
62
76
  next();
63
77
  });
64
- if (import.meta.env.SSR) {
78
+ if (importMetaEnv.SSR) {
65
79
  const route = context.routePath ?? "/";
66
80
  router.push(route);
67
81
  await router.isReady();
@@ -73,7 +87,7 @@ function ViteSSG(App, routerOptions, fn, options) {
73
87
  initialState
74
88
  };
75
89
  }
76
- if (!import.meta.env.SSR) {
90
+ if (!importMetaEnv.SSR) {
77
91
  (async () => {
78
92
  const { app, router } = await createApp$1();
79
93
  await router.isReady();
@@ -21,4 +21,10 @@ const ClientOnly = defineComponent({
21
21
  }
22
22
  });
23
23
 
24
- export { ClientOnly as C, documentReady as d };
24
+ function getImportMetaEnv() {
25
+ return "env" in import.meta ? import.meta.env : {
26
+ SSR: typeof setImmediate === "function" && "unref" in setTimeout(() => void 0)
27
+ };
28
+ }
29
+
30
+ export { ClientOnly as C, documentReady as d, getImportMetaEnv as g };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-ssg-optimized",
3
3
  "type": "module",
4
- "version": "0.28.0-optimized.2",
4
+ "version": "0.28.0-optimized.4",
5
5
  "description": "Server-side generation for Vite",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",