Repository navigation
Description
- Accepted Date: 2026-11-09
- Reference Discussions: Include scripts and styles only when component used #664, Optimize Static Builds: Tree-shake Unused Components in CMS Page Builder Patterns #1054
- Authors: @y-nk, @moritzlaube
- Champion: @florian-lefebvre
Summary
Astro should support including an Astro component’s scripts and styles only when that component renders. This would help CMS page builders and other runtime component-selection patterns avoid shipping or running assets for components that a page does not use, while preserving Astro’s streaming behavior.
Background & Motivation
Astro builds component scripts and styles ahead of time. When a page imports a set of possible components and selects one at runtime, the page can include assets for candidates that never render. CMS page builders make this pattern common because editors choose page sections from a predefined component library.
Roadmap discussion #664 raised the mismatch between imported components and rendered components, especially for scripts. It also surfaced an important constraint: building assets at runtime is not a suitable direction, and buffering a full response to discover rendered components can affect streaming. Roadmap discussion #1054 focused on reducing unused assets in static CMS builds, while keeping existing streaming behavior for on-demand rendering.
Goals
- Include scripts and styles associated with an Astro component only when that component renders.
- Support pages that select components from CMS data or other runtime values.
- Keep asset generation at build time. Do not require Vite or another bundler to run for each request.
- Preserve response streaming where possible. Avoid requiring a page-wide opt-out from streaming.
- Keep page-level and shared assets, preserve asset ordering, and avoid duplicate asset emission.
Non-goals
- Support asset propagation for framework components.
Example
---
import Hero from '../components/Hero.astro?lazy';
import Cards from '../components/Cards.astro?lazy';
const components = { hero: Hero, cards: Cards };
const blocks = [{ type: 'hero', props: { title: 'Welcome' } }];
---
{blocks.map(({ type, props }) => {
const Component = components[type];
return <Component {...props} />;
})}Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsStage 2: Accepted Proposals, No RFC
Activity
florian-lefebvre commentedon Nov 12, 2026
API ideas from my Spike PR:
Global
treeShakeComponentsflagexperimental.treeShakeComponentsenables Astro to track which components render and remove assets owned only by components that do not render. The flag applies to the whole build. For on-demand pages, Astro must buffer the full response, which disables streaming.Pros
Cons
astro:dynamichelperastro:dynamictakes a loader for each candidate component. The?assetsquery exposes the component’s collected assets.dynamic()resolves the selected loader and uses head propagation to add those assets as the component renders, preserving streaming. The example spreads CMS data onto a genericRendercomponent, so directive-shaped keys in that data need careful handling.Note: props could be moved to a
componentPropsproperty to avoid setting directives by mistake.Pros
Cons
?assetsquery.componentProps).Lazy component wrapper
?lazycould make each imported component a wrapper that loads or renders the underlying Astro component and adds its assets during rendering:Note: props could be moved to a
componentPropsproperty to avoid setting directives by mistake.Pros
Cons
componentProps).y-nk commentedon Nov 13, 2026
imho the flag makes more sense but it's all about how to consider this addition:
dynamicmakes more sense (gives control to the end user)florian-lefebvre commentedon Nov 13, 2026
I'm working on another spike which uses a global flag here withastro/astro#18730
saimonsays199128 commentedon Nov 19, 2026
Data point from production, in case it helps choose between the options.
I run 10 sites on Astro 7 with Wix-managed headless (built on
@astrojs/cloudflare). They all use the pattern this targets: one catch-all route, page sections chosen from CMS data through a type → component map.<style>would shrink. On a Tailwind-only site the same 33.5 KB of inlined CSS is on every page, because the utilities are one global stylesheet, so this change wouldn't touch it. On the sites with scoped<style>blocks (10 of 53 components on the largest), the savings would be real.treeShakeComponentsflag) would raise time to first byte on exactly those misses, which is where our field TTFB is worst. A per-component, streaming-preserving form (?lazyorastro:dynamic) would fit far better. We'd happily wrap the section map ourselves.y-nk commentedon Nov 23, 2026
@saimonsays199128 how's "scripts are already fine"? could you elaborate? because it seems that what @florian-lefebvre is working on is exactly about making them fine, therefore if you already have a working solution maybe it's not a bug to solve?
florian-lefebvre commentedon Nov 25, 2026
@ascorbic you hoped Stage 2 could be use to explore different designs. Do you have any ideas/opinions?