Repository navigation
Description
What happens
In an Astro project, accepting a live variant that carries CSS makes the dev server fail a style request right after the agent finishes the carbonize cleanup:
[ERROR] [vite] Internal server error: No Astro CSS at index 1
at LoadPluginContext.handler (node_modules/astro/dist/vite-plugin-astro/index.js:125:7)
[ERROR] [vite] [console.error] [vite] Failed to reload /src/pages/index.astro?astro&type=style&index=1&lang.css. This could be due to syntax errors or importing non-existent modules.
and in the browser console:
Failed to load resource: the server responded with a status of 500 (Internal Server Error) [/src/pages/index.astro?t=...&astro&type=style&index=1&lang.css]
[vite] Failed to reload /src/pages/index.astro?astro&type=style&index=1&lang.css.
The live-e2e astro-vite7 fixture fails its clean-console check on this, which is why its dependency was pinned to astro@7.3.5 to unblock a release.
Affected versions
- Astro 7.3.6 and later (checked on 7.3.6 and 7.3.8, the newest today): the 500 and the two console errors above. The page then does a full reload and ends up correct: the accepted markup is there and the page's own styles apply. Nothing stays broken; the damage is a red error in the terminal and the console on every accept that carries CSS.
- Astro 7.3.5 and earlier: no error, but the result is worse and went unnoticed. In a minimal reproduction of the same three file states, the temporary carbonize CSS never applied (so the accepted variant lost its look between accept and cleanup), and after the cleanup the page's own
<style>block was served with stale content, so the page's styles were wrong until the next edit of that file or a dev-server restart. The e2e suite has no computed-style assertion on that fixture, so it passed. - Engine 0.1.11 and 0.1.12 behave the same. Only
.astrosources are affected.
Root cause
Two things meet here.
Ours. While variants are on screen, the session CSS sits in <style is:inline data-impeccable-css="ID">, which Astro's compiler leaves alone (that is what styleMode: astro-global-prefixed prescribes). On accept, build_carbonize_replacement in crates/live/src/live_accept.rs rewrites the block and emits a bare <style data-impeccable-css="ID"> for every non-JSX file, .astro included. Astro compiles a bare <style> into a ?astro&type=style&index=N module, numbered in source order. The carbonize block sits in the markup, ahead of the page's own trailing <style>, so:
| step | file state | Astro style modules |
|---|---|---|
| variants on screen | is:inline session block + page <style> |
index=0 (page) |
after live-accept |
bare session block + page <style> |
index=0 (session), index=1 (page) |
| after carbonize cleanup | page <style> only |
index=0 (page) |
Accept adds a compiled style module and renumbers the page's own; the cleanup removes one again in the same save that changes markup.
Astro's. withastro/astro#18076 ("Fixes stale scoped styles during HMR when both markup and <style> are changed in a single save"), released in 7.3.6, added one line at the end of handleHotUpdate in vite-plugin-astro/hmr.js:
astroFileToCompileMetadata.delete(ctx.file);It is the only change to vite-plugin-astro between 7.3.5 and 7.3.6. Before it, a save that changed markup kept the old compile metadata, so the style modules were answered from the previous compile (hence the stale styles on 7.3.5). After it, the load hook recompiles from disk, which is correct, but Vite still asks for every style module the page had before the save, including the index that no longer exists, and the load hook throws No Astro CSS at index N for it.
That part reproduces with no impeccable involved. A page with two <style> blocks, one save that deletes the first block and changes a word of markup, Astro 7.3.8:
HTTP 500 /src/pages/index.astro?t=...&astro&type=style&index=1&lang.css
[vite] Failed to reload /src/pages/index.astro?astro&type=style&index=1&lang.css.
(full reload, page correct)
On 7.3.5 the same edit logs nothing and leaves the heading without its color, which is the bug #18076 fixed. So the new error is an upstream rough edge (a removed style block should not be a 500), and the reason live mode walks into it on every accept is ours: accept changes how Astro treats the session block.
Ruled out
- A style block index that never existed, or a malformed intermediate file: each of the three file states compiles fine by itself.
- A non-atomic write or HMR racing the cleanup: the standalone reproduction writes each state with one
writeFileSyncand waits 2.5 s between states, and it fails the same way every time.
Fix
Keep the carbonize block's style tag is:inline on .astro, so the number and order of compiled style modules never changes during a live session. With that, the same three states produce no failed request on 7.3.8, and on 7.3.5 the temporary CSS applies and the page's own styles survive. Users get it with the next engine release.
Not done here: reporting the 500 for a removed style block to Astro. I found no existing report (the closest, withastro/astro#16234, is an older closed issue with the same message at index 0).
Investigated and written by an AI agent (Claude Code) working under pbakaus's instructions.
🤖 Generated with Claude Code
Activity
live: keep the Astro carbonize style block is:inline (#993)