chore: DRY out preload link/header logic#16445
Merged
Merged
Conversation
|
Install the latest version of pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/a9ae4af91b4ca70cb19d82b4b848c5e9619042d6Open in |
|
…er font due to a redundant nested `if` check in the fonts loop of `render_response`.
This commit fixes the issue reported at packages/kit/src/runtime/server/page/render.js:344
## Bug
In the fonts loop of `render_response` (`packages/kit/src/runtime/server/page/render.js`), the code contained a redundant nested check:
```js
if (resolve_opts.preload({ type: 'font', path })) {
const ext = dep.slice(dep.lastIndexOf('.') + 1);
if (resolve_opts.preload({ type: 'font', path })) {
add_preload(path, ['rel="preload"', 'as="font"', `type="font/${ext}"`, 'crossorigin']);
}
}
```
`resolve_opts.preload` is a **user-provided hook** (via the `preload` option of `resolve` in `hooks.server.js`). It is not guaranteed to be pure or idempotent — a user hook may increment counters, log, or track which assets were preloaded. Calling it twice per font is unintended behavior.
### Trigger
Any request rendering a page/route that includes font dependencies, when the app defines a `resolve(..., { preload })` hook. The hook is executed a second time for every font whose first `preload` call returned truthy. A hook with side effects (e.g. counting preloaded fonts) would observe double invocation.
## Fix
Removed the redundant inner `if`, calling `add_preload` directly after the single outer `resolve_opts.preload` check. This matches the pattern used for the modulepreloads loop, which calls `add_preload` directly after one hook check, ensuring the hook is invoked exactly once per font.
Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
Co-authored-by: Rich-Harris <hello@rich-harris.dev>
elliott-with-the-longest-name-on-github
approved these changes
Jul 20, 2026
elliott-with-the-longest-name-on-github
merged commit Jul 20, 2026
877b8d4
into
version-3
21 checks passed
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
extracts the drive-by improvements in #16443 to reduce the size of that diff (am weighing a slightly different approach, but this change unambiguously makes sense)