fix: prevent viewtransitions.css from loading with server:defer#15577
fix: prevent viewtransitions.css from loading with server:defer#155770xRozier wants to merge 3 commits intowithastro:mainfrom
Conversation
The @astrojs/compiler incorrectly treats `server:defer` like a transition directive, causing viewtransitions.css to be imported on pages that use server:defer even without any ViewTransitions/ClientRouter usage. Strip the viewtransitions.css import from compiled output when the source doesn't contain any transition:name, transition:animate, or transition:persist directives. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 860bfeb 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 issue #15574 where viewtransitions.css is incorrectly loaded on pages using server:defer without any View Transition directives. The root cause lies in @astrojs/compiler, which incorrectly groups server:defer with transition directives when determining whether to import the ViewTransitions CSS. This PR implements a post-compilation workaround in Astro's compile step to strip the unnecessary CSS import.
Changes:
- Added post-compilation logic to strip
viewtransitions.cssimport when no transition directives are present in source - Added unit tests verifying CSS inclusion/exclusion logic for various directive combinations
- Added integration test to verify CSS is not included in rendered HTML for server:defer-only pages
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| packages/astro/src/core/compile/compile.ts | Adds post-compilation check to strip viewtransitions.css import when only server:defer is used without transition directives |
| packages/astro/test/units/compile/viewtransitions-css.test.js | New unit test file with 4 tests covering CSS import behavior across different directive combinations |
| packages/astro/test/server-islands.test.js | Adds integration test verifying viewtransitions CSS is not included in rendered HTML |
| .changeset/fix-viewtransitions-css-server-defer.md | Changeset documenting the patch fix |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Hi @0xRozier 👋🏼 thank you for addressing the issue so fast! I saw that you repair the unnecessary input inside the astro package after the fact. Did you have a chance take a look at the compiler repository? |
…lint error Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Hi @martrapp, thanks for the feedback! I agree that the compiler would be the ideal place for this fix. Unfortunately, I'm not familiar with Go and the compiler internals, so contributing a fix there isn't really something I can take on. This workaround is intentionally scoped and defensive — it only strips the import when the source genuinely has no Let me know if this approach works for you, or if you'd prefer to wait for a compiler-side fix instead! |
|
Hi @0xRozier, thanks for the honest reply — I really appreciate the transparency. That makes sense, and your workaround sounds sensible as a stopgap. I will take a look at the compiler and see if I can make any progress there. |
|
Opened withastro/compiler#1149 |
|
Closing this PR in favour of #1149 |
Summary
Fixes withastro/compiler#1148
@astrojs/compilerincorrectly treatsserver:deferlike atransition:*directive, causingviewtransitions.cssto be imported on pages that useserver:defereven without any ViewTransitions/ClientRouter usage.viewtransitions.cssimport from compiled output when the source doesn't contain anytransition:name,transition:animate, ortransition:persistdirectives.Root cause
In the compiler's
transform.go,server:deferis grouped with transition directives:This sets
doc.Transition = truewhich makes the compiler emitimport "astro/components/viewtransitions.css"even when onlyserver:deferis present.The long-term fix belongs in
@astrojs/compiler, but this provides an Astro-side workaround by stripping the unnecessary CSS import post-compilation.Test plan
server:defertransition:nametransition:animateserver:deferandtransition:name🤖 Generated with Claude Code