Skip to content

Commit 55edbe8

Browse files
committed
fix: index route for ssg with strategy prefix
1 parent af812f8 commit 55edbe8

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

src/pages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export async function setupPages({ localeCodes, options }: I18nNuxtContext, nuxt
8484

8585
// keep root when using prefixed routing without prerendering
8686
const indexPage = pages.find(x => x.path === '/')
87-
if (!nuxt.options._generate && options.strategy === 'prefix' && indexPage != null) {
87+
if (options.strategy === 'prefix' && indexPage != null) {
8888
localizedPages.unshift(indexPage as NarrowedNuxtPage)
8989
}
9090

src/prepare/strategy.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@ import type { Nuxt } from '@nuxt/schema'
33

44
export function prepareStrategy({ options, normalizedLocales }: I18nNuxtContext, nuxt: Nuxt) {
55
if (options.strategy === 'prefix' && nuxt.options._generate) {
6+
// add localized routes as entry pages for prerendering
67
const localizedEntryPages = normalizedLocales.map(x => ['/', x.code].join(''))
78
nuxt.hook('nitro:config', config => {
89
config.prerender ??= {}
9-
10-
// ignore `/` which is added by nitro by default
11-
config.prerender.ignore ??= []
12-
config.prerender.ignore.push(/^\/$/)
13-
14-
// add localized routes as entry pages for prerendering
1510
config.prerender.routes ??= []
1611
config.prerender.routes.push(...localizedEntryPages)
1712
})

src/runtime/server/plugin.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { stringify } from 'devalue'
22
import { defineI18nMiddleware } from '@intlify/h3'
3-
import { defineNitroPlugin } from 'nitropack/runtime'
3+
import { isString } from '@intlify/shared'
4+
import { defineNitroPlugin, useRuntimeConfig } from 'nitropack/runtime'
45
import { tryUseI18nContext, createI18nContext } from './context'
56
import { createUserLocaleDetector } from './utils/locale-detector'
67
import { pickNested } from './utils/messages-utils'
@@ -12,17 +13,51 @@ import { localeDetector } from '#internal/i18n/locale.detector.mjs'
1213

1314
import type { H3Event } from 'h3'
1415
import type { CoreOptions } from '@intlify/core'
16+
import { localeCodes } from '#internal/i18n/options.mjs'
17+
import { genString } from 'knitwork'
18+
import type { I18nPublicRuntimeConfig, RootRedirectOptions } from '#internal-i18n-types'
1519

1620
export default defineNitroPlugin(async nitro => {
1721
const options = await setupVueI18nOptions()
22+
const runtimeI18n = useRuntimeConfig().public.i18n as I18nPublicRuntimeConfig
23+
const { useCookie, cookieKey } = runtimeI18n.detectBrowserLanguage || {}
1824
const localeConfigs = createLocaleConfigs(options.fallbackLocale)
1925

2026
nitro.hooks.hook('request', async (event: H3Event) => {
2127
event.context.nuxtI18n = createI18nContext()
2228
event.context.nuxtI18n.localeConfigs = localeConfigs
2329
})
2430

31+
function getPrefixRedirectionScript() {
32+
if (runtimeI18n.rootRedirect) {
33+
return `
34+
if(${JSON.stringify(!!runtimeI18n.rootRedirect)}) {
35+
window.location.replace('${isString(runtimeI18n.rootRedirect) ? runtimeI18n.rootRedirect : (runtimeI18n.rootRedirect as RootRedirectOptions).path}');
36+
}`
37+
}
38+
return `
39+
function getCookieValue(cookieName) {
40+
if (${JSON.stringify(!useCookie)}) return '';
41+
for (const cookie of document.cookie.split('; ')) {
42+
const [name, value] = cookie.split('=');
43+
if (name === ${genString(cookieKey || __DEFAULT_COOKIE_KEY__)}) return value;
44+
}
45+
}
46+
const locale = getCookieValue() || navigator.language;
47+
const locales = ${JSON.stringify(localeCodes)};
48+
const defaultLocale = ${genString(options.locale || localeCodes[0])};
49+
window.location.replace('/' + (locales.includes(locale) ? locale : defaultLocale));`
50+
}
51+
const ssgPrefixRedirectionScript =
52+
__IS_SSG__ && __I18N_STRATEGY__ === 'prefix' && `<script>${getPrefixRedirectionScript()}</script>`
53+
2554
nitro.hooks.hook('render:html', (htmlContext, { event }) => {
55+
if (ssgPrefixRedirectionScript && event.path === '/') {
56+
htmlContext.body.length = 0
57+
htmlContext.bodyAppend.unshift(ssgPrefixRedirectionScript)
58+
return
59+
}
60+
2661
if (__I18N_PRELOAD__) {
2762
const ctx = tryUseI18nContext(event)
2863
if (ctx == null || Object.keys(ctx.messages ?? {}).length == 0) return

src/runtime/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@ export async function navigate(redirectPath: string, routePath: string, locale:
262262
const { rootRedirect, skipSettingLocaleOnNavigate } = nuxt.$config.public.i18n as I18nPublicRuntimeConfig
263263
const ctx = useNuxtI18nContext(nuxt)
264264

265+
if (__I18N_STRATEGY__ === 'prefix' && __IS_SSG__ && routePath === '/') {
266+
return
267+
}
268+
265269
if (routePath === '/' && rootRedirect) {
266270
let redirectCode = 302
267271
if (isString(rootRedirect)) {

0 commit comments

Comments
 (0)