-
Notifications
You must be signed in to change notification settings - Fork 3
/
site.ts
37 lines (33 loc) · 1.02 KB
/
site.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
import type { NuxtApp, NuxtError } from '#app'
import { siteQuery } from '~/queries'
export default defineNuxtPlugin(async (nuxtApp) => {
const site = useSite()
const i18n = nuxtApp.$i18n as NuxtApp['$i18n']
if (import.meta.server) {
// Load the site data server-side
await updateSite()
} else if (import.meta.client) {
// Update the site data on locale change
nuxtApp.hook(
'i18n:beforeLocaleSwitch',
async ({ oldLocale, newLocale }) => {
if (oldLocale !== newLocale) {
if (import.meta.dev)
// eslint-disable-next-line no-console
console.log('Locale changed:', oldLocale, '->', newLocale)
await updateSite(newLocale)
}
},
)
}
async function updateSite(newLocale?: string) {
try {
const data = await $kql(siteQuery, {
language: newLocale || i18n.locale.value,
})
site.value = data?.result || {}
} catch (e) {
console.error('Failed to fetch site data:', (e as NuxtError).message)
}
}
})