Skip to content

Commit 60d7904

Browse files
committed
fix: don't regenerate fallbacks for already-transformed declarations
1 parent df3903e commit 60d7904

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

‎src/module.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ export default defineNuxtModule<ModuleOptions>({
315315
selectFontsToPreload: resolveFontsToPreload,
316316
exposeUsage: devtools?.exposeUsage,
317317
async resolveFontFace(fontFamily, fallbackOptions) {
318+
// Declarations in bundled stylesheets may already have been transformed.
319+
if (fallbackOptions?.fallbacks.some(fallback => fallback.startsWith(`${fontFamily} Fallback: `))) {
320+
return
321+
}
322+
318323
resolveFontFaceWithOverride ||= await resolvePromise
319324
const result = await resolveFontFaceWithOverride(fontFamily, undefined, fallbackOptions)
320325

‎test/basic.test.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { readFile } from 'node:fs/promises'
12
import { fileURLToPath } from 'node:url'
23
import { describe, it, expect } from 'vitest'
3-
import { setup, $fetch, fetch } from '@nuxt/test-utils'
4+
import { setup, $fetch, fetch, useTestContext } from '@nuxt/test-utils'
5+
import { glob } from 'tinyglobby'
46

57
import { mockAdobeFetch } from './fixtures/adobe/index.ts'
68
import { extractFontFaces, extractPreloadLinks } from './utils.ts'
@@ -187,6 +189,13 @@ describe('features', () => {
187189
expect(html).toContain('font-family:Anton,Anton Fallback\\: Segoe UI')
188190
})
189191

192+
it('should generate fallback metrics for global families once in bundled stylesheets', async () => {
193+
const [file] = await glob('public/_nuxt/global.*.css', { cwd: useTestContext().nuxt!.options.nitro.output!.dir, absolute: true })
194+
const css = await readFile(file!, 'utf8')
195+
expect(css).not.toContain('Anton Fallback: Anton Fallback')
196+
expect(extractFontFaces('Anton Fallback: Arial', css)).toHaveLength(1)
197+
})
198+
190199
it('should not duplicate the font face or preload hints of a global family at its usage site', async () => {
191200
const html = await $fetch<string>('/global')
192201
expect(extractFontFaces('Anton', html)).toMatchInlineSnapshot(`

0 commit comments

Comments
 (0)