Skip to content

Commit b0f04f1

Browse files
committed
feat: add experimental option
1 parent e7cb993 commit b0f04f1

File tree

8 files changed

+32
-24
lines changed

8 files changed

+32
-24
lines changed

src/bundler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ export function getDefineConfig({ options, fullStatic }: I18nNuxtContext, server
9292
__I18N_PRELOAD__: JSON.stringify(!!options.experimental.preload),
9393
// eslint-disable-next-line @typescript-eslint/no-base-to-string
9494
__I18N_ROUTING__: JSON.stringify(nuxt.options.pages.toString() && options.strategy !== 'no_prefix'),
95-
__I18N_STRICT_SEO__: JSON.stringify(!!options.experimental.strictSeo)
95+
__I18N_STRICT_SEO__: JSON.stringify(!!options.experimental.strictSeo),
96+
__I18N_SERVER_REDIRECT__: JSON.stringify(!!options.experimental.nitroContextDetection)
9697
}
9798

9899
if (nuxt.options.ssr || !server) {

src/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export const DEFAULT_OPTIONS = {
3535
cacheLifetime: undefined,
3636
stripMessagesPayload: false,
3737
preload: false,
38-
strictSeo: false
38+
strictSeo: false,
39+
nitroContextDetection: true
3940
},
4041
bundle: {
4142
compositionOnly: true,

src/env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ declare let __I18N_PRELOAD__: boolean
2828
/** Project has pages and strategy is not `no_prefix` */
2929
declare let __I18N_ROUTING__: boolean
3030
declare let __I18N_STRICT_SEO__: boolean
31+
declare let __I18N_SERVER_REDIRECT__: boolean

src/prepare/strategy.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ export function prepareStrategy({ options, localeCodes }: I18nNuxtContext, nuxt:
77
nuxt.hook('nitro:config', config => {
88
config.prerender ??= {}
99

10-
config.routeRules ??= {}
11-
// config.routeRules['/'] = { prerender: false, ssr: true }
12-
1310
// ignore `/` which is added by nitro by default
1411
config.prerender.ignore ??= []
1512
config.prerender.ignore.push(/^\/$/)

src/runtime/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export function createNuxtI18nContext(nuxt: NuxtApp, vueI18n: I18n, defaultLocal
8181
domainFromLocale(runtimeI18n.domainLocales, useRequestURL({ xForwardedHost: true }), locale)
8282
const baseUrl = createBaseUrlGetter(nuxt, runtimeI18n.baseUrl, defaultLocale, getDomainFromLocale)
8383
const resolvedLocale = useResolvedLocale()
84-
if (import.meta.server && nuxt.ssrContext?.event?.context?.nuxtI18n?.detectLocale) {
84+
if (__I18N_SERVER_REDIRECT__ && import.meta.server && nuxt.ssrContext?.event?.context?.nuxtI18n?.detectLocale) {
8585
resolvedLocale.value = nuxt.ssrContext.event.context.nuxtI18n.detectLocale
8686
}
8787

src/runtime/plugins/route-locale-detect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default defineNuxtPlugin({
1515
)
1616

1717
// no pages or no prefixes - do not register route middleware
18-
if (!__I18N_ROUTING__ || import.meta.server) return
18+
if (!__I18N_ROUTING__ || (import.meta.server && __I18N_SERVER_REDIRECT__)) return
1919

2020
addRouteMiddleware(
2121
'locale-changing',

src/runtime/server/plugin.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,31 +135,33 @@ export default defineNitroPlugin(async nitro => {
135135

136136
const baseUrlGetter = createBaseUrlGetter()
137137
nitro.hooks.hook('request', async (event: H3Event) => {
138-
const detector = useDetectors(event, detection)
139-
const localeSegment = detector.route(event.path)
140-
const pathLocale = (isSupportedLocale(localeSegment) && localeSegment) || undefined
141-
const path = (pathLocale && event.path.slice(pathLocale.length + 1)) || event.path
142-
143-
// attempt to only run i18n detection for nuxt pages and i18n server routes
144-
if (!event.path.includes('/_i18n/') && !isExistingNuxtRoute(path)) {
145-
return
146-
}
147-
148138
const options = await setupVueI18nOptions(getDefaultLocaleForDomain(getHost(event)) || _defaultLocale)
149139
const ctx = createI18nContext()
140+
event.context.nuxtI18n = ctx
141+
142+
if (__I18N_SERVER_REDIRECT__) {
143+
const detector = useDetectors(event, detection)
144+
const localeSegment = detector.route(event.path)
145+
const pathLocale = (isSupportedLocale(localeSegment) && localeSegment) || undefined
146+
const path = (pathLocale && event.path.slice(pathLocale.length + 1)) || event.path
150147

151-
const resolved = resolveRedirectPath(event, path, pathLocale, options.defaultLocale, detector)
152-
if (resolved.path) {
153-
ctx.detectLocale = resolved.locale
154-
detection.useCookie && setCookie(event, detection.cookieKey, resolved.locale, cookieOptions)
155-
await sendRedirect(event, joinURL(baseUrlGetter(event, options.defaultLocale), resolved.path), resolved.code)
156-
return
148+
// attempt to only run i18n detection for nuxt pages and i18n server routes
149+
if (!event.path.includes('/_i18n/') && !isExistingNuxtRoute(path)) {
150+
return
151+
}
152+
153+
const resolved = resolveRedirectPath(event, path, pathLocale, options.defaultLocale, detector)
154+
if (resolved.path) {
155+
ctx.detectLocale = resolved.locale
156+
detection.useCookie && setCookie(event, detection.cookieKey, resolved.locale, cookieOptions)
157+
await sendRedirect(event, joinURL(baseUrlGetter(event, options.defaultLocale), resolved.path), resolved.code)
158+
return
159+
}
157160
}
158161

159162
const localeConfigs = createLocaleConfigs(options.fallbackLocale)
160163
ctx.vueI18nOptions = options
161164
ctx.localeConfigs = localeConfigs
162-
event.context.nuxtI18n = ctx
163165
})
164166

165167
nitro.hooks.hook('render:html', (htmlContext, { event }) => {

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ export interface ExperimentalFeatures {
114114
*/
115115
stripMessagesPayload?: boolean
116116
strictSeo?: boolean | SeoAttributesOptions
117+
/**
118+
* Enables Nitro context detection and allows for more reliable detection and redirection behavior especially in setups using prerendering.
119+
* It is recommended to keep it enabled, but can be disabled if this causes issues, this option might be removed in v11.
120+
* @default true
121+
*/
122+
nitroContextDetection?: boolean
117123
}
118124

119125
export interface BundleOptions

0 commit comments

Comments
 (0)