Optimize Static Builds: Tree-shake Unused Components in CMS Page Builder Patterns #1054
Replies: 3 comments
-
We would like to see that in dynamic SSR rendering as well and would even waive the streaming option since every page is cached through our cdn for an hour anyway. |
Beta Was this translation helpful? Give feedback.
-
Definitely interested in this, today I had a video component in my articles page, which added 240kb to the bundle because it used mux-player library, even if the article didn't have one. Changed this to dynamic imports with intersection observer, which solved it, but almost slipped past me. |
Beta Was this translation helpful? Give feedback.
-
Hi @moritzlaube, I don't think I quite understand what you are asking for. In particular:
What do you mean here? What is a "page bundle"? |
Beta Was this translation helpful? Give feedback.
-
Body
Summary
When building static sites with a CMS page builder approach, where pages are dynamically composed of a library of components, Astro currently includes all potential components in every page bundle, even when only a subset is actually used. This RFC proposes build optimizations for static sites to only include components that are actually used in the final bundle, either through static imports with tree-shaking or optimized dynamic imports.
Background & Motivation
Modern headless CMSs like Strapi, Prismic, Directus or even WordPress with the Gutenberg Blocks offer page builder capabilities where content editors can freely compose pages from a predefined set of components. For example, they can build a landing page by combining a hero section, feature list, testimonials, and a contact form – all through the CMS interface.
When building such sites with Astro (
output: 'static'
), the current behavior includes all available components in every page bundle, even though:This leads to unnecessarily bloated bundles in static sites, where pages might only use 3–4 components but include scripts and styles from, let's say, all 20-30 available components. While the current behavior makes sense for SSR where streaming requires knowing all possible scripts upfront, it seems unnecessary for static builds.
This issue was previously discussed in #4863 but was closed due to perceived technical constraints related to streaming. However, since streaming isn't relevant for static builds, this limitation should be revisitable.
Goals
Example
In my humble opinion, there are two possible approaches to implement this optimization, using either static or dynamic imports. Both approaches would solve the same problem of unnecessary component loading.
Current behavior can be seen in this Stackblitz example – although using an outdated Astro version, the issue still applies –, where even unused components are included in the final bundle.
Static Import Approach (Preferred)
Dynamic Import Approach (Alternative)
With either approach, Astro should:
The static import approach offers several advantages:
Both approaches would achieve the same goal: ensuring only used components are included in the final static build. The static import approach might be easier to implement since it leverages existing static analysis capabilities of bundlers.
This feature would be crucial for anyone building CMS-powered static sites where pages are dynamically composed through a page builder interface (which is really common when working with clients).
While my understanding of Astro's internal architecture is quite limited, I hope this proposal can spark a discussion about this important feature. And thank you for creating such an amazing tool!
Beta Was this translation helpful? Give feedback.
All reactions