refactor(i18n): isolate Vue I18n instances per request for SSR
Replace global I18n singleton with a factory function Inject request-specific I18n instances into Nuxt app and SEO metadata Prevent cross-request locale state pollution during server-side rendering
This commit is contained in:
@@ -1,5 +1,15 @@
|
||||
import { i18n } from '../src/i18n';
|
||||
import { createPokopiaI18n, setActiveI18n } from '../src/i18n';
|
||||
|
||||
export default defineNuxtPlugin((nuxtApp) => {
|
||||
const i18n = createPokopiaI18n();
|
||||
if (import.meta.client) {
|
||||
setActiveI18n(i18n);
|
||||
}
|
||||
|
||||
nuxtApp.vueApp.use(i18n);
|
||||
return {
|
||||
provide: {
|
||||
pokopiaI18n: i18n
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import { computed, ref } from 'vue';
|
||||
import { onLocaleChange } from '../src/i18n';
|
||||
import { applyRouteSeo, onSeoChange, resolveRouteSeo, type ResolvedSeoConfig } from '../src/seo';
|
||||
import { applyRouteSeo, onSeoChange, resolveRouteSeo, setSeoTranslator, type ResolvedSeoConfig } from '../src/seo';
|
||||
|
||||
export default defineNuxtPlugin(() => {
|
||||
const router = useRouter();
|
||||
const nuxtApp = useNuxtApp();
|
||||
const t = (nuxtApp.$pokopiaI18n as { global: { t: (key: string, values?: Record<string, string | number>) => string } }).global.t;
|
||||
const dynamicSeo = ref<ResolvedSeoConfig | null>(null);
|
||||
const activeSeo = computed(() => dynamicSeo.value ?? resolveRouteSeo(router.currentRoute.value));
|
||||
const activeSeo = computed(() => dynamicSeo.value ?? resolveRouteSeo(router.currentRoute.value, t));
|
||||
|
||||
useHead(() => ({
|
||||
title: activeSeo.value.title,
|
||||
@@ -42,6 +44,7 @@ export default defineNuxtPlugin(() => {
|
||||
return;
|
||||
}
|
||||
|
||||
setSeoTranslator(t);
|
||||
onSeoChange((seo) => {
|
||||
dynamicSeo.value = seo;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user