Skip to content

Starlight dev: page styles trapped inside <head> <template id="theme-icons"> on output:'static' (no adapter) — #16291 still reproduces #16994

Closed

Description

@vvlladd28

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

  1. added
    triage: needs triage
    on Jun 8, 2026
  2. matthewp commented on Jun 11, 2026

    @matthewp
    Contributor

    @astrobot-houston can you try again?

  3. astrobot-houston commented on Jun 11, 2026

    @astrobot-houston
    Contributor
    • Reproduced: No — tried multiple strategies (simple fixture, real Starlight project, HMR cycles, rapid file changes) but could not trigger the bug deterministically.
    • Exploration: Partial — analyzed the head propagation pipeline but could not identify a concrete code path that fails.
    • Unit Test: No — without a reproducible trigger, a regression test would not be meaningful.
    • Priority: P2: has workaround. This is a dev-only issue (production builds are unaffected) that requires extended dev server usage to trigger. It has two workarounds: restarting the dev server, or moving <template> out of <head> into <body>. Primarily affects Starlight users on output: 'static'.

    The reported mechanism is plausible: when the dev module graph is "warmed" over extended use, containsHead metadata propagated by vite-plugin-head/index.ts can become stale for page modules. This causes result._metadata.headInTree to be false in renderPage() (runtime/server/render/page.ts:106), allowing maybeRenderHead() 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() in vite-plugin-astro-server/metadata.ts (which crawls the live module graph per-request) and the containsHead propagation in vite-plugin-head/index.ts as the relevant code paths. A potential timing/ordering issue may exist where, after certain HMR invalidation patterns, pluginContainer.getModuleInfo() returns stale data missing the containsHead flag. 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.astro with <head> containing <ThemeIcons />
    • ThemeIcons.astro with <template id="theme-icons"> containing <Icon /> components
    • Icon.astro with scoped <style>
    • Multiple pages (/, /page-1/, /page-2/, /page-3/)

    Tested:

    • Cold start fetch → Pass
    • Visiting all pages multiple times → Pass
    • HMR via file touches (Icon, ThemeIcons, Layout) → Pass
    • 10 rapid HMR cycles with interleaved fetches → Pass
    • Rotating file touch order across cycles → Pass

    2. Real Starlight project (v0.39.3)

    • Installed @astrojs/starlight@0.39.3 with astro@6.3.8
    • Created 3 content pages
    • Started dev server, browsed all pages multiple rounds → Pass
    • Touched content files to trigger HMR → Pass
    • Touched ThemeProvider.astro in node_modules → Pass
    • Touched Page.astro and PageFrame.astro (which contain <head>) → Pass

    3. Code Analysis

    Relevant code paths:

    • packages/astro/src/vite-plugin-head/index.ts — Propagates containsHead and propagation metadata up the module graph
    • packages/astro/src/vite-plugin-astro-server/metadata.ts — getComponentMetadata() crawls the live module graph per-request (used by RunnablePipeline for output: 'static')
    • packages/astro/src/runtime/server/render/page.ts:106 — Sets headInTree from containsHead metadata
    • packages/astro/src/core/head-propagation/policy.ts:25 — shouldRenderMaybeHeadInstruction() checks !headInTree

    Key observation: The vite-plugin-head transform hook has enforce: 'pre', meaning it runs BEFORE vite-plugin-astro's transform sets containsHead. On the first transform, propagation can't happen in the transform hook. Instead, it relies on resolveId (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 (minor extraHead refactor 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.

  4. matthewp commented on Jun 26, 2026

    @matthewp
    Contributor

    If you're able to come up with a reproduction, please file a new issue. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triage: needs triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions