forked from antfu-collective/vite-ssg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
89 lines (83 loc) · 2.8 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import devalue from '@nuxt/devalue'
import routes from 'virtual:generated-pages'
import { ViteSSG } from 'vite-ssg/i18n'
import { createPinia } from 'pinia'
import { locales, defaultLocale, defaultLocaleOnUrl } from '../ssg-i18n-options.json'
import { useRootStore } from './store/root'
import App from './App.vue'
// import i18n resources
// https://vitejs.dev/guide/features.html#glob-import
const globalMessages: Record<string, any> = Object.fromEntries(Object.entries(import.meta.globEager('../locales/*.yml'))
.map(([key, value]) => [key.slice(11, -4), value.default]),
)
export const createApp = ViteSSG(
App,
{ routes },
{
transformState(state) {
return import.meta.env.SSR ? devalue(state) : state
},
i18nOptions: {
locales,
defaultLocale,
defaultLocaleOnUrl,
globalMessages,
// async(locale, to) => {
// try {
// const messagesModule = await import(/* @vite-ignore */ `../pages/${to.meta.rawI18nPath}.json5`)
// // const messagesModule = await import(/* @vite-ignore */ `../../locales/pages/${to.meta.rawI18nPath}.json5`)
// // should use default
// return messagesModule.default || messagesModule
// }
// catch (e) {
// console.error('uppps', e)
// return undefined
// }
// },
async headConfigurer(route, headObject, i18nComposer, locale) {
const meta = route.meta
if (meta && meta.injectI18nMeta) {
// you can delegate to default behavior
headObject.value = meta.injectI18nMeta(
headObject.value,
locale,
i18nComposer,
)
// you can customize the entire head object
/*
const routeName = route.name?.toString() || route.path
// we can add what we want, also change the entire headObject
meta.injectI18nMeta(
headObject.value,
locale,
i18nComposer,
i18nComposer.t(`page-${routeName}.title`),
i18nComposer.t(`page-${routeName}.description`),
)
*/
// or you can change the entire head object page
// todo@userquin: include example with dynamic import
// headObject = await import()
}
return true
},
},
},
({ app, router, initialState, i18n }) => {
const pinia = createPinia()
app.use(pinia)
if (import.meta.env.SSR) {
// this will be stringified and set to window.__INITIAL_STATE__
initialState.pinia = pinia.state.value
}
else {
// on the client side, we restore the state
pinia.state.value = initialState.pinia || {}
}
router.beforeEach((to, from, next) => {
const store = useRootStore(pinia)
store.initialize()
next()
})
},
)