Skip to content

Commit 3c3594c

Browse files
committed
fix: handle duplicate preloaded fonts correctly
1 parent c18da6d commit 3c3594c

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/module.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,15 @@ export default defineNuxtModule<ModuleOptions>({
283283
viteEntry = relative(ctx.config.root || nuxt.options.srcDir, (ctx as any).entry)
284284
})
285285
nuxt.hook('build:manifest', (manifest) => {
286-
const unprocessedPreloads = new Set([...fontMap.values()].flatMap(v => [...v]))
287-
function addPreloadLinks(chunk: ResourceMeta, urls: Set<string>) {
286+
const unprocessedPreloads = new Set([...fontMap.keys()])
287+
function addPreloadLinks(chunk: ResourceMeta, urls: Set<string>, id?: string) {
288288
chunk.assets ||= []
289289
for (const url of urls) {
290290
if (!chunk.assets.includes(url)) {
291291
chunk.assets.push(url)
292-
unprocessedPreloads.delete(url)
292+
if (id) {
293+
unprocessedPreloads.delete(id)
294+
}
293295
}
294296
if (!manifest[url]) {
295297
manifest[url] = {
@@ -311,7 +313,7 @@ export default defineNuxtModule<ModuleOptions>({
311313
for (const css of chunk.css) {
312314
const assetName = withoutLeadingSlash(join(nuxt.options.app.buildAssetsDir, css))
313315
if (fontMap.has(assetName)) {
314-
addPreloadLinks(chunk, fontMap.get(assetName)!)
316+
addPreloadLinks(chunk, fontMap.get(assetName)!, assetName)
315317
}
316318
}
317319
}
@@ -321,11 +323,11 @@ export default defineNuxtModule<ModuleOptions>({
321323
const chunk = manifest[relative(nuxt.options.srcDir, id)]
322324
if (!chunk) continue
323325

324-
addPreloadLinks(chunk, urls)
326+
addPreloadLinks(chunk, urls, id)
325327
}
326328

327329
if (entry) {
328-
addPreloadLinks(entry, unprocessedPreloads)
330+
addPreloadLinks(entry, new Set([...unprocessedPreloads].flatMap(v => [...fontMap.get(v) || []])))
329331
}
330332
})
331333

src/plugins/transform.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ export const FontFamilyInjectionPlugin = (options: FontFamilyInjectionPluginOpti
200200
for (const file of chunk.moduleIds) {
201201
if (options.fontsToPreload.has(file)) {
202202
options.fontsToPreload.set(chunk.facadeModuleId, options.fontsToPreload.get(file)!)
203+
if (chunk.facadeModuleId !== file) {
204+
options.fontsToPreload.delete(file)
205+
}
203206
}
204207
}
205208
}

0 commit comments

Comments
 (0)