Skip to content

chore: DRY out preload link/header logic#16445

Merged
elliott-with-the-longest-name-on-github merged 2 commits into
version-3from
dry-preload
Jul 20, 2026
Merged

chore: DRY out preload link/header logic#16445
elliott-with-the-longest-name-on-github merged 2 commits into
version-3from
dry-preload

Conversation

@Rich-Harris

Copy link
Copy Markdown
Member

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)

@pkg-svelte-dev

pkg-svelte-dev Bot commented Jul 20, 2026

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from a9ae4af:

pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/a9ae4af91b4ca70cb19d82b4b848c5e9619042d6

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/kit/pr/16445

@changeset-bot

changeset-bot Bot commented Jul 20, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: a9ae4af

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@svelte-docs-bot

Copy link
Copy Markdown

Comment thread packages/kit/src/runtime/server/page/render.js Outdated
…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
elliott-with-the-longest-name-on-github merged commit 877b8d4 into version-3 Jul 20, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants