unplugin-fonts icon indicating copy to clipboard operation
unplugin-fonts copied to clipboard

[Bug]: preload: false does not work for custom font

Open ChristopherJohnson25 opened this issue 1 year ago • 1 comments

Demo URL

No response

What happened?

setting preload: false does not work for custom font

Reproduction steps

1. add preload: false to options
2. observe that fonts are still preloaded

Relevant log output

No response

What browsers are you seeing the problem on?

Chrome

What is your operating system?

MacOS

ChristopherJohnson25 avatar Jul 11 '24 17:07 ChristopherJohnson25

While reading through the source code, I noticed, that generating tags is unconditional for all generated link tags. Thus whenever a bundled asset matches the filename pattern for a font file, there will be a tag for it. Kinda unintuitively the settings under options.custom.prefetch and options.custom.preload are applied for all fonts, including Google Fonts, Font Source, etc.

Unfortunately, there's a bit of code duplication for the actual link tag generation, which makes the fix for this a bit spread all around the code base.

The outline for this is basically:

  • Find all places where getHeadLinkTags is called.
  • Look at the for (const file of files) loop directly below
  • The tags.push() there needs to be conditional on options.custom.prefetch || options.custom.preload

While at it and looking in the context of #10, after that for loop is also the ideal place to introduce a new callback options.custom.linkFilter hook, that in the simplest case just receives the tags array and could then do:

  • return true -> return tags unaltered
  • return false -> return no tags
  • return a modified tag array -> forward the modified choice of headers to generate

BenBE avatar Sep 05 '24 17:09 BenBE