Skip to content

Commit c5465e7

Browse files
author
Antoine Monnet
committed
feat(i18n): split manifests per locales if i18n.splitManifest is set
1 parent 2f63166 commit c5465e7

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

src/utils/config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,12 @@ function createManifestTransform(
141141
if (latestEntry)
142142
latestEntry.revision = revision
143143
else
144-
entries.push({ url: latest, revision, size: data.size })
144+
entries.push({ url: base+latest, revision, size: data.size })
145145
}
146146
else {
147147
entries = entries.filter(e => e.url !== latest)
148148
}
149149
}
150-
151150
return { manifest: entries, warnings: [] }
152151
}
153152
}

src/utils/i18n.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
import { useNuxt, loadNuxtModuleInstance } from '@nuxt/kit'
3+
import { resolve } from 'pathe'
4+
import type { NuxtI18nOptions } from '@nuxtjs/i18n'
5+
import type { NuxtModule } from 'nuxt/schema'
6+
7+
export async function webManifests(manifestDir) {
8+
9+
const i18nOptions = await getNuxtModuleOptions('@nuxtjs/i18n') as NuxtI18nOptions
10+
11+
return i18nOptions.locales.map(({code})=>{
12+
const localePath = getLocalePath(i18nOptions, code)
13+
return {
14+
localDir: resolve(manifestDir, localePath),
15+
optionsI18n: { manifest: {
16+
start_url: `/${localePath}`,
17+
scope: `/${localePath}`,
18+
lang: code,
19+
}}
20+
}
21+
})
22+
}
23+
24+
//from https://github.com/nuxt-modules/sitemap/blob/main/src/util/kit.ts
25+
async function getNuxtModuleOptions(module: string | NuxtModule, nuxt: Nuxt = useNuxt()) {
26+
const moduleMeta = (typeof module === 'string' ? { name: module } : await module.getMeta?.()) || {}
27+
const { nuxtModule } = (await loadNuxtModuleInstance(module, nuxt))
28+
29+
let moduleEntry: [string | NuxtModule, Record<string, any>] | undefined
30+
for (const m of nuxt.options.modules) {
31+
if (Array.isArray(m) && m.length >= 2) {
32+
const _module = m[0]
33+
const _moduleEntryName = typeof _module === 'string'
34+
? _module
35+
: (await (_module as any as NuxtModule).getMeta?.())?.name || ''
36+
if (_moduleEntryName === moduleMeta.name)
37+
moduleEntry = m as [string | NuxtModule, Record<string, any>]
38+
}
39+
}
40+
41+
let inlineOptions = {}
42+
if (moduleEntry)
43+
inlineOptions = moduleEntry[1]
44+
if (nuxtModule.getOptions)
45+
return nuxtModule.getOptions(inlineOptions, nuxt)
46+
return inlineOptions
47+
}
48+
49+
function getLocalePath(options, localeCode) {
50+
switch (options.strategy) {
51+
case 'prefix_except_default':
52+
case 'prefix_and_default':
53+
return localeCode === options.defaultLocale ? "" : localeCode
54+
case 'prefix':
55+
return localeCode
56+
default:
57+
throw "Strategy not implemented"
58+
}
59+
}

src/utils/utils.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,18 @@ export async function writeWebManifest(dir: string, options: PwaModuleOptions, a
2323
if (pwaAssetsGenerator)
2424
pwaAssetsGenerator.injectManifestIcons()
2525
}
26-
const manifest = api.generateBundle({})?.[path]
27-
await _writeWebManifest(dir, path, manifest)
26+
27+
if (options.i18n?.splitManifest == true) {
28+
const i18n = await import('./i18n')
29+
const manifests = await i18n.webManifests(dir)
30+
await Promise.all(manifests.map(async ({localDir, optionsI18n})=>{
31+
const manifest = api.generateBundle({}, optionsI18n)?.[path]
32+
await _writeWebManifest(localDir, path, manifest)
33+
}))
34+
} else {
35+
const manifest = api.generateBundle({})?.[path]
36+
await _writeWebManifest(dir, path, manifest)
37+
}
2838
}
2939

3040
async function _writeWebManifest(dir, path, manifest) {

0 commit comments

Comments
 (0)