-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
inject font preload links #4963
Conversation
🦋 Changeset detectedLatest commit: 20c6df1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
I'm surprised this was all that was needed. When I had looked at this awhile ago the font info wasn't in the build and the only way to get it was setting Anyway, in terms of the option, I think you might want something more than a boolean. Like a function that takes the page URL and a list of the font paths and allows you to filter to decide which to preload. In that case it'd have to be done via |
Vite plans to add options for disabling, filtering, and transform of preload URLs after v3. I wonder if we should just keep this on the backburner for awhile and see what v4 brings in terms of allowing the user to configure preload URLs so that we don't duplicate Vite's options. |
v4 could be a long ways off, no? |
I think the idea is to do it around the end of this year. They want to do a new version for Rollup 3 support |
But actually, Vite 4 is probably irrelevant because adding a new option is backwards compatible. The actual update I saw this morning on Vite Discord was "we'll work on this after v3 release" and my mind somehow filled that in with Vite 4 |
What would it even look like though, given that we're not using |
We would need to be ultimately responsible for rendering the preload links. We could potentially use Vite's options though for filtering the links. I have no objection to adding this PR as is. I think the trickier thing would be whether we want to add an API for filtering preload links when Vite will add its own. |
Here's a PR for Vite's new option: vitejs/vite#9938 |
@benmccann that PR was since merged. What does this mean for the state of this PR? How would we use those APIs? |
I haven't looked into it very much yet, so don't know a whole lot beyond what's in the docs: https://vitejs.dev/config/build-options.html#build-modulepreload. It looks to be called via Vite's preload helper when there's a dynamic import, so we should probably use it to ensure dynamic imports are handled. I think there'd be two options:
|
Updated this to master. I think I prefer the "enhance export function handle({ event, resolve }) {
return resolve(event, { preload: ({ type, url }) => {
// this would be the default; or we decide to also not preload fonts;
// or we decide to add fonts to assets and remove that special type
if (type !== 'asset') { // type: 'font' | 'css' | 'js' | 'asset'
return true;
}
}); Routing every asset through there could be done later in a non-breaking way if we either treat asset as a special type with which for now the method is never called; or if we decide "not font type, just an asset type" then we can still make clear in the docs that this will potentially be called with more asset types in the future and you should use a regex on the url to narrow it to what you want. |
Yeah, I think that API makes sense, and I'd probably be in favour of defaulting to preloading everything (we'd need to see what impact it has on different sites before committing). I think |
After reading a bit I think it would be a mistake to preload all assets by default. In fact, things like Next's imagine component do the opposite and load images on demand only. Preloading all this by default would probably hurt core web vitals for the average page. I think the best way forward is to
|
fixes #4039.
I think we need to figure out some details before merging this though:
resolve
option?resolve
is more granular, though possibly unnecessarily soPlease don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. All changesets should bepatch
until SvelteKit 1.0