Skip to content

fix: preserve inline styles and font preloads during ClientRouter head swap#15514

Merged
martrapp merged 10 commits into
withastro:mainfrom
veeceey:fix/issue-15465-font-head-swap
Feb 22, 2026
Merged

fix: preserve inline styles and font preloads during ClientRouter head swap#15514
martrapp merged 10 commits into
withastro:mainfrom
veeceey:fix/issue-15465-font-head-swap

Conversation

@veeceey
Copy link
Copy Markdown
Contributor

@veeceey veeceey commented Feb 14, 2026

Fixes #15465

What

During client-side navigation with <ClientRouter />, the swapHeadElements() function removes all head elements that don't match one of two conditions: having data-astro-transition-persist, or being a <link rel="stylesheet"> with a matching href.

This means inline <style> tags -- including the @font-face declarations injected by the experimental <Font> component -- get removed from the DOM and re-appended on every navigation. The browser then re-evaluates the @font-face rules, which triggers font-display: swap and produces a visible font flash even though the font files are already cached.

Fix

Extended persistedHeadElement() to also preserve:

  1. Inline <style> elements -- matched by comparing textContent. If the exact same style content exists in both the old and new head, the existing node is kept in place.
  2. Font preload links (<link rel="preload" as="font">) -- matched by href, similar to how stylesheet links are already handled.

This is a small, targeted change to a single function. The existing behavior for all other head elements is unchanged.

Testing

Ran the existing view-transitions.test.js suite -- both tests pass.

…d swap

During client-side navigation with ClientRouter, swapHeadElements() only
preserved elements with data-astro-transition-persist and stylesheet links.
Inline <style> tags (including @font-face declarations from <Font>) were
removed and re-appended on every navigation, forcing the browser to
re-evaluate them and triggering a visible font flash (FOUT).

This extends persistedHeadElement() to also match:
- Inline <style> elements by their text content
- Font preload links (link[rel=preload][as=font]) by href

When an identical element exists in both the old and new document head,
it stays in the DOM instead of being removed and re-inserted.

Fixes withastro#15465
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Feb 14, 2026

🦋 Changeset detected

Latest commit: 5767c7b

The changes in this PR will be included in the next version bump.

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

@github-actions github-actions Bot added the pkg: astro Related to the core `astro` package (scope) label Feb 14, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Feb 15, 2026

Merging this PR will not alter performance

✅ 9 untouched benchmarks


Comparing veeceey:fix/issue-15465-font-head-swap (5767c7b) with main (b30a2c4)

Open in CodSpeed

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes font flashing (FOUT) during client-side navigation with <ClientRouter /> by preserving inline <style> elements and font preload links during head element swaps. Previously, inline styles containing @font-face declarations from the experimental <Font> component were removed and re-inserted on every navigation, causing the browser to re-evaluate them and trigger visible font flashes.

Changes:

  • Extended the persistedHeadElement() function to preserve inline <style> elements by matching their textContent
  • Added preservation logic for <link rel="preload" as="font"> elements by matching their href attribute
  • Added appropriate changeset documenting the fix

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
packages/astro/src/transitions/swap-functions.ts Extended persistedHeadElement() to preserve inline styles and font preload links during head swaps
.changeset/fix-font-head-swap.md Documented the patch-level change fixing font flash during ClientRouter navigation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Member

@martrapp martrapp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for opening this PR. The change looks good to me.

I was wondering whether we should also match the attributes of the style element, alongside the content? I did not find an example with practical relevance.

It would be great if you could add a new e2e test to show that identical elements survive the swap untouched, but it is not a problem if you decide to leave it out.

@veeceey
Copy link
Copy Markdown
Contributor Author

veeceey commented Feb 16, 2026

Thanks for the review! Good point about matching style element attributes too — I had the same thought but couldn't come up with a case where it would matter in practice either, since the inline styles generated by Font only differ in content. Happy to add that if a real use case comes up down the line.

As for the e2e test, I'll look into adding one that verifies the style and preload elements persist through a navigation swap. Will update the PR if I get something solid together.

veeceey and others added 2 commits February 18, 2026 21:25
Add a test that verifies inline <style> elements (e.g. @font-face
declarations) and font preload links survive head swaps during
client-side navigation, preventing FOUT (Flash of Unstyled Text).

The test navigates between two pages sharing identical inline styles
and font preloads, and asserts that the original DOM nodes persist
rather than being removed and re-inserted.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@veeceey
Copy link
Copy Markdown
Contributor Author

veeceey commented Feb 20, 2026

Added the e2e test as requested!

Remove unused variables (styleIdBefore, preloadIdBefore) to fix biome
lint errors. Replace fragile exact style count comparison with
greaterThan(0) checks, since Astro may inject additional style elements
during client-side navigation. Use data-attribute selectors to directly
query the marked DOM nodes instead of relying on querySelector order.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@martrapp
Copy link
Copy Markdown
Member

Great! Thank you very much for adding the test!
I will take a look and will probably make the test a bit more concise.

@veeceey
Copy link
Copy Markdown
Contributor Author

veeceey commented Feb 20, 2026 via email

Copy link
Copy Markdown
Member

@martrapp martrapp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@martrapp martrapp merged commit 999a7dd into withastro:main Feb 22, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: astro Related to the core `astro` package (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

<Font> component's inline styles are not preserved during ClientRouter head swap, causing font flash on navigation

3 participants