|
1 | 1 | import { stringify } from 'devalue' |
2 | 2 | import { defineI18nMiddleware } from '@intlify/h3' |
3 | 3 | import { defineNitroPlugin, useStorage } from 'nitropack/runtime' |
4 | | -import { tryUseI18nContext, createI18nContext } from './context' |
| 4 | +import { tryUseI18nContext, createI18nContext, useI18nContext } from './context' |
5 | 5 | import { createUserLocaleDetector } from './utils/locale-detector' |
6 | 6 | import { pickNested } from './utils/messages-utils' |
7 | 7 | import { createLocaleConfigs, getDefaultLocaleForDomain, isSupportedLocale } from '../shared/locales' |
@@ -142,39 +142,48 @@ export default defineNitroPlugin(async nitro => { |
142 | 142 | } |
143 | 143 |
|
144 | 144 | const baseUrlGetter = createBaseUrlGetter() |
145 | | - nitro.hooks.hook('request', async (event: H3Event) => { |
| 145 | + |
| 146 | + async function initialize(event: H3Event) { |
146 | 147 | const options = await setupVueI18nOptions(getDefaultLocaleForDomain(getHost(event)) || _defaultLocale) |
147 | | - const url = getRequestURL(event) |
| 148 | + const localeConfigs = createLocaleConfigs(options.fallbackLocale) |
148 | 149 | const ctx = createI18nContext() |
149 | 150 |
|
150 | | - const localeConfigs = createLocaleConfigs(options.fallbackLocale) |
151 | 151 | ctx.vueI18nOptions = options |
152 | 152 | ctx.localeConfigs = localeConfigs |
153 | 153 |
|
154 | 154 | event.context.nuxtI18n = ctx |
| 155 | + return ctx |
| 156 | + } |
| 157 | + |
| 158 | + nitro.hooks.hook('request', async (event: H3Event) => { |
| 159 | + await initialize(event) |
| 160 | + }) |
155 | 161 |
|
156 | | - if (__I18N_SERVER_REDIRECT__) { |
157 | | - const detector = useDetectors(event, detection) |
158 | | - const localeSegment = detector.route(event.path) |
159 | | - const pathLocale = (isSupportedLocale(localeSegment) && localeSegment) || undefined |
160 | | - const path = (pathLocale && url.pathname.slice(pathLocale.length + 1)) || url.pathname |
| 162 | + nitro.hooks.hook('render:before', async ({ event }) => { |
| 163 | + if (!__I18N_SERVER_REDIRECT__) return |
161 | 164 |
|
162 | | - // attempt to only run i18n detection for nuxt pages and i18n server routes |
163 | | - if (!url.pathname.includes('/_i18n/') && !isExistingNuxtRoute(path)) { |
164 | | - return |
165 | | - } |
| 165 | + const ctx = import.meta.prerender && !event.context.nuxtI18n ? await initialize(event) : useI18nContext(event) |
| 166 | + const url = getRequestURL(event) |
| 167 | + const detector = useDetectors(event, detection) |
| 168 | + const localeSegment = detector.route(event.path) |
| 169 | + const pathLocale = (isSupportedLocale(localeSegment) && localeSegment) || undefined |
| 170 | + const path = (pathLocale && url.pathname.slice(pathLocale.length + 1)) || url.pathname |
| 171 | + |
| 172 | + // attempt to only run i18n detection for nuxt pages and i18n server routes |
| 173 | + if (!url.pathname.includes('/_i18n/') && !isExistingNuxtRoute(path)) { |
| 174 | + return |
| 175 | + } |
166 | 176 |
|
167 | | - const resolved = resolveRedirectPath(event, path, pathLocale, options.defaultLocale, detector) |
168 | | - if (resolved.path && resolved.path !== url.pathname) { |
169 | | - ctx.detectLocale = resolved.locale |
170 | | - detection.useCookie && setCookie(event, detection.cookieKey, resolved.locale, cookieOptions) |
171 | | - await sendRedirect( |
172 | | - event, |
173 | | - joinURL(baseUrlGetter(event, options.defaultLocale), resolved.path + url.search), |
174 | | - resolved.code |
175 | | - ) |
176 | | - return |
177 | | - } |
| 177 | + const resolved = resolveRedirectPath(event, path, pathLocale, ctx.vueI18nOptions!.defaultLocale, detector) |
| 178 | + if (resolved.path && resolved.path !== url.pathname) { |
| 179 | + ctx.detectLocale = resolved.locale |
| 180 | + detection.useCookie && setCookie(event, detection.cookieKey, resolved.locale, cookieOptions) |
| 181 | + await sendRedirect( |
| 182 | + event, |
| 183 | + joinURL(baseUrlGetter(event, ctx.vueI18nOptions!.defaultLocale), resolved.path + url.search), |
| 184 | + resolved.code, |
| 185 | + ) |
| 186 | + return |
178 | 187 | } |
179 | 188 | }) |
180 | 189 |
|
|
0 commit comments