Skip to content

Commit 0fa042c

Browse files
authored
fix: include hash in locale message server route (#3808)
1 parent a9dda56 commit 0fa042c

12 files changed

Lines changed: 2049 additions & 256 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
"magic-string": "^0.30.17",
101101
"mlly": "^1.7.4",
102102
"nuxt-define": "^1.0.0",
103+
"ohash": "^2.0.11",
103104
"oxc-parser": "^0.81.0",
104105
"oxc-transform": "^0.81.0",
105106
"oxc-walker": "^0.4.0",

pnpm-lock.yaml

Lines changed: 2024 additions & 244 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

specs/helper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,9 @@ export async function startServerWithRuntimeConfig(env: Record<string, unknown>,
259259
*/
260260
export function waitForLocaleNetwork(page: Page, locale: string, type: 'request' | 'response') {
261261
if (type === 'request') {
262-
return page.waitForRequest(new RegExp(`/_i18n/${locale}/messages.json`))
262+
return page.waitForRequest(new RegExp(`/_i18n/.*/${locale}/messages.json`))
263263
}
264-
return page.waitForResponse(new RegExp(`/_i18n/${locale}/messages.json`))
264+
return page.waitForResponse(new RegExp(`/_i18n/.*/${locale}/messages.json`))
265265
}
266266

267267
/**

src/bundler.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ export async function extendBundler(ctx: I18nNuxtContext, nuxt: Nuxt) {
6565
await addDefinePlugin(defineConfig)
6666
}
6767

68-
export function getDefineConfig({ options, fullStatic }: I18nNuxtContext, server = false, nuxt = useNuxt()) {
68+
export function getDefineConfig(
69+
{ options, fullStatic, deploymentHash }: I18nNuxtContext,
70+
server = false,
71+
nuxt = useNuxt()
72+
) {
6973
const cacheLifetime = options.experimental.cacheLifetime ?? (fullStatic ? FULL_STATIC_LIFETIME : -1)
7074
const isCacheEnabled = cacheLifetime >= 0 && (!nuxt.options.dev || !!options.experimental.devCache)
7175

@@ -99,7 +103,8 @@ export function getDefineConfig({ options, fullStatic }: I18nNuxtContext, server
99103
// eslint-disable-next-line @typescript-eslint/no-base-to-string
100104
__I18N_ROUTING__: JSON.stringify(nuxt.options.pages.toString() && options.strategy !== 'no_prefix'),
101105
__I18N_STRICT_SEO__: JSON.stringify(!!options.experimental.strictSeo),
102-
__I18N_SERVER_REDIRECT__: JSON.stringify(!!options.experimental.nitroContextDetection)
106+
__I18N_SERVER_REDIRECT__: JSON.stringify(!!options.experimental.nitroContextDetection),
107+
__I18N_HASH__: JSON.stringify(deploymentHash)
103108
}
104109

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

src/context.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createResolver } from '@nuxt/kit'
22
import { fileURLToPath } from 'node:url'
33
import { dirname } from 'pathe'
4+
import { hash } from 'ohash'
45
import type { Resolver } from '@nuxt/kit'
56
import type { FileMeta, LocaleInfo, LocaleObject, NuxtI18nOptions } from './types'
67

@@ -15,6 +16,7 @@ export interface I18nNuxtContext {
1516
distDir: string
1617
runtimeDir: string
1718
fullStatic: boolean
19+
deploymentHash: string
1820
}
1921

2022
const resolver = createResolver(import.meta.url)
@@ -34,6 +36,7 @@ export function createContext(userOptions: NuxtI18nOptions): I18nNuxtContext {
3436
localeCodes: undefined!,
3537
normalizedLocales: undefined!,
3638
vueI18nConfigPaths: undefined!,
37-
fullStatic: undefined!
39+
fullStatic: undefined!,
40+
deploymentHash: hash(Date.now()).slice(0, 8)
3841
}
3942
}

src/env.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +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

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/:locale/messages.json`,
108+
route: `/_i18n/: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
@@ -98,7 +98,7 @@ export function createNuxtI18nContext(nuxt: NuxtApp, vueI18n: I18n, defaultLocal
9898
const loadMessagesFromServer = async (locale: string) => {
9999
if (locale in localeLoaders === false) return
100100
const headers: HeadersInit = getLocaleConfig(locale)?.cacheable ? {} : { 'Cache-Control': 'no-cache' }
101-
const messages = await $fetch(`/_i18n/${locale}/messages.json`, { headers })
101+
const messages = await $fetch(`/_i18n/${__I18N_HASH__}/${locale}/messages.json`, { headers })
102102
for (const k of Object.keys(messages)) {
103103
i18n.mergeLocaleMessage(k, messages[k])
104104
}

src/runtime/plugins/i18n.ts

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

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

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

src/runtime/plugins/preload.ts

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

0 commit comments

Comments
 (0)