@@ -2,6 +2,7 @@ import type { VueHeadClient } from '@unhead/vue'
2
2
import type { Component } from 'vue'
3
3
import type { RouterOptions , ViteSSGClientOptions , ViteSSGContext } from '../types'
4
4
import { createHead } from '@unhead/vue/client'
5
+ import { createHead as createSSRHead } from '@unhead/vue/server'
5
6
import { createApp as createClientApp , createSSRApp } from 'vue'
6
7
import { createMemoryHistory , createRouter , createWebHistory } from 'vue-router'
7
8
import { documentReady } from '../utils/document-ready'
@@ -14,33 +15,30 @@ export function ViteSSG(
14
15
App : Component ,
15
16
routerOptions : RouterOptions ,
16
17
fn ?: ( context : ViteSSGContext < true > ) => Promise < void > | void ,
17
- options : ViteSSGClientOptions = { } ,
18
+ options ? : ViteSSGClientOptions ,
18
19
) {
19
20
const {
20
21
transformState,
21
22
registerComponents = true ,
22
23
useHead = true ,
23
24
rootContainer = '#app' ,
24
- hydration = false ,
25
- } = options
26
- const isClient = typeof window !== 'undefined'
25
+ } = options ?? { }
27
26
28
- async function createApp ( client = false , routePath ?: string ) {
29
- const app = client && ! hydration
30
- ? createClientApp ( App )
31
- : createSSRApp ( App )
27
+ async function createApp ( _client = false , routePath ?: string ) {
28
+ const app = import . meta . env . SSR || options ?. hydration
29
+ ? createSSRApp ( App )
30
+ : createClientApp ( App )
32
31
33
32
let head : VueHeadClient | undefined
34
33
35
34
if ( useHead ) {
36
- head = createHead ( )
37
- app . use ( head )
35
+ app . use ( head = import . meta. env . SSR ? createSSRHead ( ) : createHead ( ) )
38
36
}
39
37
40
38
const router = createRouter ( {
41
- history : client
42
- ? createWebHistory ( routerOptions . base )
43
- : createMemoryHistory ( routerOptions . base ) ,
39
+ history : import . meta . env . SSR
40
+ ? createMemoryHistory ( routerOptions . base )
41
+ : createWebHistory ( routerOptions . base ) ,
44
42
...routerOptions ,
45
43
} )
46
44
@@ -50,16 +48,16 @@ export function ViteSSG(
50
48
app . component ( 'ClientOnly' , ClientOnly )
51
49
52
50
const appRenderCallbacks : ( ( ) => void ) [ ] = [ ]
53
- const onSSRAppRendered = client
54
- ? ( ) => { }
55
- : ( cb : ( ) => void ) => appRenderCallbacks . push ( cb )
51
+ const onSSRAppRendered = import . meta . env . SSR
52
+ ? ( cb : ( ) => void ) => appRenderCallbacks . push ( cb )
53
+ : ( ) => { }
56
54
const triggerOnSSRAppRendered = ( ) => {
57
55
return Promise . all ( appRenderCallbacks . map ( cb => cb ( ) ) )
58
56
}
59
57
const context : ViteSSGContext < true > = {
60
58
app,
61
59
head,
62
- isClient,
60
+ isClient : ! import . meta . env . SSR ,
63
61
router,
64
62
routes,
65
63
onSSRAppRendered,
@@ -69,7 +67,7 @@ export function ViteSSG(
69
67
routePath,
70
68
}
71
69
72
- if ( client ) {
70
+ if ( ! import . meta . env . SSR ) {
73
71
await documentReady ( )
74
72
// @ts -expect-error global variable
75
73
context . initialState = transformState ?.( window . __INITIAL_STATE__ || { } ) || deserializeState ( window . __INITIAL_STATE__ )
@@ -92,7 +90,7 @@ export function ViteSSG(
92
90
next ( )
93
91
} )
94
92
95
- if ( ! client ) {
93
+ if ( ! import . meta . env . SSR ) {
96
94
const route = context . routePath ?? '/'
97
95
router . push ( route )
98
96
@@ -108,9 +106,9 @@ export function ViteSSG(
108
106
} as ViteSSGContext < true >
109
107
}
110
108
111
- if ( isClient ) {
109
+ if ( ! import . meta . env . SSR ) {
112
110
( async ( ) => {
113
- const { app, router } = await createApp ( true )
111
+ const { app, router } = await createApp ( )
114
112
// wait until page component is fetched before mounting
115
113
await router . isReady ( )
116
114
app . mount ( rootContainer , true )
0 commit comments