fix: preserve inline styles and font preloads during ClientRouter head swap#15514
Conversation
…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 detectedLatest 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 |
There was a problem hiding this comment.
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.
martrapp
left a comment
There was a problem hiding this comment.
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.
|
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. |
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>
|
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>
|
Great! Thank you very much for adding the test! |
|
Sure thanks !!
…________________________________
From: Martin Trapp ***@***.***>
Sent: Friday, February 20, 2026 1:54:40 AM
To: withastro/astro ***@***.***>
Cc: Varun Chawla ***@***.***>; Author ***@***.***>
Subject: Re: [withastro/astro] fix: preserve inline styles and font preloads during ClientRouter head swap (PR #15514)
[https://avatars.githubusercontent.com/u/94928215?s=20&v=4]martrapp left a comment (withastro/astro#15514)<#15514 (comment)>
Great! Thank you very much for adding the test!
I will take a look and will probably make the test a bit more concise.
—
Reply to this email directly, view it on GitHub<#15514 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AIE72BACI5CH7IF4EETI73D4M3KWBAVCNFSM6AAAAACVD5AHQSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTSMZSG43DAOJXHA>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Fixes #15465
What
During client-side navigation with
<ClientRouter />, theswapHeadElements()function removes all head elements that don't match one of two conditions: havingdata-astro-transition-persist, or being a<link rel="stylesheet">with a matchinghref.This means inline
<style>tags -- including the@font-facedeclarations injected by the experimental<Font>component -- get removed from the DOM and re-appended on every navigation. The browser then re-evaluates the@font-facerules, which triggersfont-display: swapand produces a visible font flash even though the font files are already cached.Fix
Extended
persistedHeadElement()to also preserve:<style>elements -- matched by comparingtextContent. If the exact same style content exists in both the old and new head, the existing node is kept in place.<link rel="preload" as="font">) -- matched byhref, 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.jssuite -- both tests pass.