Repository navigation
Description
Related
Follow-up to #16291 (fixed by #16292). That fix is present in astro@6.3.8, but the bug still reproduces on output: 'static' with no adapter installed — i.e. outside the @astrojs/cloudflare prerender-environment case PR #16292 addressed.
Astro Info
astro 6.3.8 (dist/vite-plugin-head/index.js already tracks ssr + prerender envs, i.e. includes #16292)
@astrojs/starlight 0.39.3
node v22.22.0
output 'static' (default; no adapter installed)
OS Linux
Describe the Bug
Once astro dev has been running for a while (module graph warmed by browsing several pages and/or a few HMR edits), the dev server serves HTML in which the page's entire set of hoisted <style data-vite-dev-id> blocks is emitted inside Starlight's <head> <template id="theme-icons"> (from @astrojs/starlight/components/ThemeProvider.astro).
<template> content is inert per the HTML spec, so every trapped style is dead — the page renders unstyled and data-theme falls back to dark.
Key points:
- Server-state dependent, not browser/navigation dependent. The malformed markup is in the served bytes (confirmed with
curl, no JS involved). Often perceived as "only when navigating to the page" because browsing other routes first warms the dev server into the bad state; a hard refresh of a freshly-started server is fine. - On the same code and machine: a long-running dev server trapped 81 of 82
<style>blocks inside the template; a freshly-started one trapped 0. - It's the
<template>being in<head>that matters, not its contents. Replacing the three<Icon>children with plain raw<svg>(no scoped-CSS component inside) does not help — a warmed server still dumps all the styles right after the<template>'s opening tag. Moving the<template>out of<head>(into<body>) is what fixes it.
Byte evidence from the warmed server:
<template id="theme-icons"> @ offset 5643
<style …×81…> first style @ 5978 ← inside the template
</template> @ offset 245513 ← closes only after all the CSS
Root cause (same mechanism as #16291)
On a warmed dev module graph, maybeRenderHead() flushes the page's collected <style> blocks at a position that lands inside the first <template> present in <head> (here, Starlight's theme-icons template). PR #16292 fixed environment tracking for the Cloudflare prerender environment, but on output: 'static' (no adapter) the virtual:astro:component-metadata can still be stale/incomplete after warm-up, so the head flush still lands inside the <head> template.
What's the expected result?
Hoisted dev <style> blocks should be emitted in <head> outside any <template>, regardless of how warm the dev module graph is — matching the production build (which is unaffected) and a freshly-started dev server.
Workaround
- Restart the dev server (temporary), or
- Don't put a
<template>in<head>at all — render it in<body>instead. (Changing only the template's contents, e.g. raw<svg>instead of<Icon>, does not help.)
Link to Minimal Reproducible Example
It's intermittent (warm-state dependent), which makes a fully deterministic StackBlitz repro tricky. Repro recipe: Starlight project on output: 'static', run astro dev, browse several routes / trigger a few HMR edits, then curl a page and grep for <style> between <template id="theme-icons"> and its </template>. Happy to help build a deterministic repro if maintainers can point at the warm-up trigger.
Participation
- I am willing to submit a pull request for this issue.
Activity
matthewp commentedon Jun 11, 2026
@astrobot-houston can you try again?
astrobot-houston commentedon Jun 11, 2026
<template>out of<head>into<body>. Primarily affects Starlight users onoutput: 'static'.The reported mechanism is plausible: when the dev module graph is "warmed" over extended use,
containsHeadmetadata propagated byvite-plugin-head/index.tscan become stale for page modules. This causesresult._metadata.headInTreeto befalseinrenderPage()(runtime/server/render/page.ts:106), allowingmaybeRenderHead()to fire inside a child component rendered within a<head><template>— trapping all hoisted<style data-vite-dev-id>blocks inside inert template content. However, the exact warm-up sequence that causes the metadata to go stale could not be triggered programmatically.I was unable to determine the cause of this issue. The code analysis points to
getComponentMetadata()invite-plugin-astro-server/metadata.ts(which crawls the live module graph per-request) and thecontainsHeadpropagation invite-plugin-head/index.tsas the relevant code paths. A potential timing/ordering issue may exist where, after certain HMR invalidation patterns,pluginContainer.getModuleInfo()returns stale data missing thecontainsHeadflag. A deterministic reproduction from the reporter (e.g. a specific sequence of file edits and page visits) would significantly help narrow this down.Full Triage Report
Reproduction Attempts
1. Simple fixture (
output: 'static', no adapter)Created a fixture mirroring the Starlight pattern:
Layout.astrowith<head>containing<ThemeIcons />ThemeIcons.astrowith<template id="theme-icons">containing<Icon />componentsIcon.astrowith scoped<style>/,/page-1/,/page-2/,/page-3/)Tested:
2. Real Starlight project (v0.39.3)
@astrojs/starlight@0.39.3withastro@6.3.8ThemeProvider.astroinnode_modules→ PassPage.astroandPageFrame.astro(which contain<head>) → Pass3. Code Analysis
Relevant code paths:
packages/astro/src/vite-plugin-head/index.ts— PropagatescontainsHeadandpropagationmetadata up the module graphpackages/astro/src/vite-plugin-astro-server/metadata.ts—getComponentMetadata()crawls the live module graph per-request (used byRunnablePipelineforoutput: 'static')packages/astro/src/runtime/server/render/page.ts:106— SetsheadInTreefromcontainsHeadmetadatapackages/astro/src/core/head-propagation/policy.ts:25—shouldRenderMaybeHeadInstruction()checks!headInTreeKey observation: The
vite-plugin-headtransformhook hasenforce: 'pre', meaning it runs BEFOREvite-plugin-astro's transform setscontainsHead. On the first transform, propagation can't happen in thetransformhook. Instead, it relies onresolveId(when other modules import this one) to propagate. After HMR re-transforms, previous metadata is available, so propagation should still work. However, there may be a window during complex invalidation chains where metadata is transiently stale.Changes since 6.3.8: Only
#16888(minorextraHeadrefactor using.join()) touched the head rendering code — no functional changes to propagation logic.This report was made by an LLM. The analysis may be wrong, and the potential fix might not work, but is intended as a starting point for exploring the issue.
matthewp commentedon Jun 26, 2026
If you're able to come up with a reproduction, please file a new issue. Thank you.
fix(dev): restore Starlight ThemeProvider override to prevent dev-mod…