Skip to content

Commit d50fa36

Browse files
authored
fix: prevent duplicate message requests on hydration (#3852)
1 parent bf200a6 commit d50fa36

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

specs/lazy_load/basic_lazy_load.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ describe('basic lazy loading', async () => {
2929
expect(await page.locator('#dynamic-time').innerText()).to.not.equal(dynamicTime)
3030
})
3131

32+
test('(#3773) messages are loaded once on page load', async () => {
33+
const { requests } = await renderPage('/')
34+
expect(requests.filter(x => x.includes('/en/messages.json'))).toHaveLength(1)
35+
})
36+
3237
test('locales are fetched on demand', async () => {
3338
const home = url('/')
3439
const { page } = await renderPage(home)

src/runtime/context.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export function createNuxtI18nContext(nuxt: NuxtApp, vueI18n: I18n, defaultLocal
7575
const detectConfig = useI18nDetection(nuxt)
7676
const serverLocaleConfigs = useLocaleConfigs()
7777
const localeCookie = useI18nCookie(detectConfig)
78+
const loadMap = new Set<string>()
7879

7980
/** Get computed config for locale */
8081
const getLocaleConfig = (locale: string) => serverLocaleConfigs.value![locale]
@@ -159,12 +160,17 @@ export function createNuxtI18nContext(nuxt: NuxtApp, vueI18n: I18n, defaultLocal
159160
return joinURL(baseUrl(), nuxt.$config.app.baseURL)
160161
},
161162
loadMessages: async (locale: string) => {
163+
// prevent multiple loads during hydration
164+
if (nuxt.isHydrating && loadMap.has(locale)) { return }
165+
162166
try {
163167
return ctx.dynamicResourcesSSG || import.meta.dev
164168
? await loadMessagesFromClient(locale)
165169
: await loadMessagesFromServer(locale)
166170
} catch (e) {
167171
console.warn(`Failed to load messages for locale "${locale}"`, e)
172+
} finally {
173+
loadMap.add(locale)
168174
}
169175
},
170176
composableCtx: undefined!,

0 commit comments

Comments
 (0)