fix: strip dead imports for client:only components#1162
Open
kimjune01 wants to merge 4 commits intowithastro:mainfrom
Open
fix: strip dead imports for client:only components#1162kimjune01 wants to merge 4 commits intowithastro:mainfrom
kimjune01 wants to merge 4 commits intowithastro:mainfrom
Conversation
The compiler emits `import Foo from './Foo'` for client:only components
but passes `null` to $$renderComponent — the import value is dead code
in the prerender environment. However, Rollup still resolves the import
and traverses the component's dependency tree (e.g. React), inflating
prerender build time.
Skip hoisted imports whose specifier appears exclusively in
ClientOnlyComponents (not also in HydratedComponents or
ServerComponents), but only when the import has exactly one binding
and is not a type-only or bare side-effect import. This avoids breaking:
- Mixed imports: `import Repl, { getMeta } from './Repl'`
- Side-effect imports: `import './Repl'`
- Type imports: `import type { Props } from './Repl'`
Measured on a 906-page site with 2,055 client:only React islands:
prerender build drops from ~4.3s to ~2.2s (49% reduction).
🦋 Changeset detectedLatest commit: 3cff70d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
The compiler emits `import Foo from './Foo'` for client:only components
but passes `null` to $$renderComponent — the import value is dead code
in the prerender environment. However, Rollup still resolves the import
and traverses the component's dependency tree (e.g. React), inflating
prerender build time.
Strip hoisted imports when every binding they introduce is an
exclusively client:only component identifier that does not appear in
the frontmatter body. This is binding-aware, not specifier-based:
- Mixed imports (`import Comp, { helper } from './X'`) are preserved
because `helper` is not a client:only binding
- Separate imports from the same specifier are evaluated independently
- Frontmatter usage of the binding (`console.log(Comp.name)`) prevents
stripping via a body text scan
- Type-only and bare side-effect imports are never stripped
Measured on a 906-page site with 2,055 client:only React islands:
prerender build drops from ~4.3s to ~2.2s (49% reduction).
- Also exclude ServerComponents from clientOnlyBindings to prevent stripping imports used by plain server-rendered components (e.g. <Carousel /> without any client: directive) - Use word-boundary regex instead of bytes.Contains to prevent false positives from substring matches (e.g. "X" inside "MAX_WIDTH")
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
Strips dead imports for
client:onlycomponents from the compiled.astrooutput. The compiler emitsimport Foo from './Foo'forclient:onlycomponents but passesnullto$$renderComponent— the import value is dead code in the prerender environment. However, Rollup still resolves the import and traverses the component's dependency tree (e.g. React, CodeMirror), inflating prerender build time for island-heavy sites.What this PR does
In
print-to-js.go, before printing hoisted imports, builds a set of specifiers that appear exclusively inClientOnlyComponents(not also inHydratedComponentsorServerComponents). For each hoisted import matching that set, skips it if:import Repl, { getMeta } from './Repl')import './Repl')The
clientOnlyComponentsmetadata in the compiled output is preserved — only the dead import statement is removed.Why this is safe
nullto$$renderComponentforclient:only: the import value is provably unused in the render pathlen(stmt.Imports) == 1)len(stmt.Imports) == 0→ not matched)!stmt.IsType)ClientOnlyComponentsandHydratedComponents) are preserved (specifier removed from the skip set)Known limitation
If frontmatter code references the imported binding for non-component purposes (e.g.
const Alias = Repl), the import would be incorrectly removed. This is an unlikely pattern forclient:onlycomponents (which by definition have no server-side use), but could be addressed with binding usage analysis if needed.Measured impact
906-page static site (Astro 6.1.2), 472 pages with 2,055
client:onlyReact islands:client:only="react"The trivial-pages experiment (same page count, no React imports) confirms ~2,135ms of prerender time is attributable to dead
client:onlyimport graph traversal.Compiled output before/after
Before:
After:
The
clientOnlyComponentsmetadata array is unchanged.Testing
All 6 client:only snapshot tests updated and passing. Full internal test suite passes.
Previously opened (and closed) withastro/astro#16634 with a Vite plugin approach, but moved to the compiler as the cleaner fix point.