Skip to content

Commit 7d76283

Browse files
authored
perf: skip client locale chunks when using messages endpoint (#4067)
1 parent 329db4e commit 7d76283

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { readdirSync, readFileSync } from 'node:fs'
2+
import { join } from 'node:path'
3+
import { fileURLToPath } from 'node:url'
4+
import { describe, expect, test } from 'vitest'
5+
6+
import { renderPage, waitForLocaleNetwork } from '../helper'
7+
import { setup, useTestContext } from '../utils'
8+
9+
await setup({
10+
rootDir: fileURLToPath(new URL(`../fixtures/lazy`, import.meta.url)),
11+
browser: true
12+
})
13+
14+
describe('client locale chunks stripped in endpoint mode', () => {
15+
test('client assets contain no bundled locale messages', async () => {
16+
// ssr builds always load messages from the endpoint, so client locale chunks are not emitted
17+
const assetsDir = join(useTestContext().nuxt!.options.nitro.output!.dir!, 'public/_nuxt')
18+
const chunks = readdirSync(assetsDir).filter(x => x.endsWith('.js'))
19+
expect(chunks.length).toBeGreaterThan(0)
20+
for (const chunk of chunks) {
21+
expect(readFileSync(join(assetsDir, chunk), 'utf8')).not.toContain('Homepage')
22+
}
23+
})
24+
25+
test('client-side locale switch fetches messages from the endpoint', async () => {
26+
const { page } = await renderPage('/')
27+
28+
await Promise.all([waitForLocaleNetwork(page, 'fr', 'response'), page.click('#nuxt-locale-link-fr')])
29+
expect(await page.locator('#home-header').innerText()).toEqual('Accueil')
30+
})
31+
})

src/template.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,18 @@ export function generateTemplateNuxtI18nOptions(
7575
'}',
7676
].join('\n\n')
7777

78+
// the production client only imports locale files when `dynamicResourcesSSG` (runtime/context.ts)
79+
// resolves to true - in endpoint mode the loaders are unreachable, stubbing them behind
80+
// `import.meta.client` (folded per graph) skips compiling and emitting client locale chunks
81+
const stripClientLoaders = !server
82+
&& !nuxt.options.dev
83+
&& nuxt.options.ssr
84+
&& (ctx.fullStatic || !nuxt.options.nitro.static)
85+
const clientLoad = (load: string) => (stripClientLoaders ? `import.meta.client ? () => Promise.resolve({}) : ${load}` : load)
86+
7887
const localeLoaderEntries: Record<string, { key: string, load: string, cache: boolean }[]> = {}
7988
for (const locale in opts.localeLoaders) {
80-
localeLoaderEntries[locale] = opts.localeLoaders[locale]!.map(({ key, load, loadServer, cache }) => ({ key, load: server ? loadServer : load, cache }))
89+
localeLoaderEntries[locale] = opts.localeLoaders[locale]!.map(({ key, load, loadServer, cache }) => ({ key, load: server ? loadServer : clientLoad(load), cache }))
8190
}
8291

8392
return `// @ts-nocheck

0 commit comments

Comments
 (0)