Skip to content

Commit b40fbbd

Browse files
authored
feat: configurable message server route prefix (#3853)
1 parent d50fa36 commit b40fbbd

File tree

11 files changed

+22
-9
lines changed

11 files changed

+22
-9
lines changed

docs/content/docs/04.api/00.options.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,3 +624,10 @@ Used to configure the directory used to resolve i18n files.
624624
::callout{icon="i-heroicons-exclamation-triangle" color="warning"}
625625
This feature relies on [Nuxt's Auto-imports](https://nuxt.com/docs/guide/concepts/auto-imports) and will not work if this has been disabled.
626626
::
627+
628+
## serverRoutePrefix
629+
630+
- type `string`{lang="ts-type"}
631+
- default: `'/_i18n'`{lang="ts"}
632+
633+
Sets the prefix for the server route used for loading locale messages.

src/bundler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ export function getDefineConfig(
103103

104104
__I18N_ROUTING__: JSON.stringify(nuxt.options.pages.toString() && options.strategy !== 'no_prefix'),
105105
__I18N_STRICT_SEO__: JSON.stringify(!!options.experimental.strictSeo),
106+
__I18N_SERVER_ROUTE__: JSON.stringify([options.serverRoutePrefix, deploymentHash].join('/')),
106107
__I18N_SERVER_REDIRECT__: JSON.stringify(!!options.experimental.nitroContextDetection),
107-
__I18N_HASH__: JSON.stringify(deploymentHash),
108108
}
109109

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

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export const DEFAULT_OPTIONS = {
8383
multiDomainLocales: false,
8484
hmr: true,
8585
autoDeclare: true,
86+
serverRoutePrefix: '/_i18n',
8687
} as const
8788

8889
export const DEFINE_I18N_ROUTE_FN = 'defineI18nRoute'

src/env.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ declare let __I18N_PRELOAD__: boolean
2929
declare let __I18N_ROUTING__: boolean
3030
declare let __I18N_STRICT_SEO__: boolean
3131
declare let __I18N_SERVER_REDIRECT__: boolean
32-
/** Hashed deployment timestamp */
33-
declare let __I18N_HASH__: string
32+
/** Includes hashed deployment timestamp */
33+
declare let __I18N_SERVER_ROUTE__: string

src/nitro.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export { localeDetector }`
105105
addServerPlugin(ctx.resolver.resolve('runtime/server/plugin'))
106106

107107
addServerHandler({
108-
route: `/_i18n/:hash/:locale/messages.json`,
108+
route: `${ctx.options.serverRoutePrefix}/:hash/:locale/messages.json`,
109109
handler: ctx.resolver.resolve('./runtime/server/routes/messages'),
110110
})
111111
}

src/runtime/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export function createNuxtI18nContext(nuxt: NuxtApp, vueI18n: I18n, defaultLocal
9999
const loadMessagesFromServer = async (locale: string) => {
100100
if (locale in localeLoaders === false) { return }
101101
const headers: HeadersInit = getLocaleConfig(locale)?.cacheable ? {} : { 'Cache-Control': 'no-cache' }
102-
const messages = await $fetch(`/_i18n/${__I18N_HASH__}/${locale}/messages.json`, { headers })
102+
const messages = await $fetch(`${__I18N_SERVER_ROUTE__}/${locale}/messages.json`, { headers })
103103
for (const k of Object.keys(messages)) {
104104
i18n.mergeLocaleMessage(k, messages[k])
105105
}

src/runtime/plugins/i18n.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default defineNuxtPlugin({
4343
setupMultiDomainLocales(optionsI18n.defaultLocale)
4444
}
4545

46-
prerenderRoutes(localeCodes.map(locale => `/_i18n/${__I18N_HASH__}/${locale}/messages.json`))
46+
prerenderRoutes(localeCodes.map(locale => `${__I18N_SERVER_ROUTE__}/${locale}/messages.json`))
4747

4848
// create i18n instance
4949
const i18n = createI18n(optionsI18n)

src/runtime/plugins/preload.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default defineNuxtPlugin({
1919
if (import.meta.server) {
2020
for (const locale of localeCodes) {
2121
try {
22-
const messages = await $fetch(`/_i18n/${__I18N_HASH__}/${locale}/messages.json`)
22+
const messages = await $fetch(`${__I18N_SERVER_ROUTE__}/${locale}/messages.json`)
2323
for (const locale of Object.keys(messages)) {
2424
nuxt.$i18n.mergeLocaleMessage(locale, messages[locale])
2525
}

src/runtime/server/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ if (import.meta.dev) {
2323
* @internal
2424
*/
2525
export const fetchMessages = async (locale: string) =>
26-
await $fetch<LocaleMessages<DefineLocaleMessage>>(`/_i18n/${__I18N_HASH__}/${locale}/messages.json`, {
26+
await $fetch<LocaleMessages<DefineLocaleMessage>>(`${__I18N_SERVER_ROUTE__}/${locale}/messages.json`, {
2727
headers,
2828
})
2929

src/runtime/server/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export default defineNitroPlugin(async (nitro) => {
172172
const path = (pathLocale && url.pathname.slice(pathLocale.length + 1)) || url.pathname
173173

174174
// attempt to only run i18n detection for nuxt pages and i18n server routes
175-
if (!url.pathname.includes('/_i18n/') && !isExistingNuxtRoute(path)) {
175+
if (!url.pathname.includes(__I18N_SERVER_ROUTE__) && !isExistingNuxtRoute(path)) {
176176
return
177177
}
178178

0 commit comments

Comments
 (0)