fix(ssr): massive compiled outputs when nested slots are present#5379
Merged
fix(ssr): massive compiled outputs when nested slots are present#5379
Conversation
divmain
commented
May 28, 2025
packages/@lwc/ssr-compiler/src/compile-template/transformers/component/slotted-content.ts
Show resolved
Hide resolved
divmain
commented
May 28, 2025
packages/@lwc/ssr-compiler/src/compile-template/transformers/component/slotted-content.ts
Show resolved
Hide resolved
wjhsf
approved these changes
Jun 2, 2025
packages/@lwc/ssr-compiler/src/compile-template/transformers/component/slotted-content.ts
Show resolved
Hide resolved
packages/@lwc/ssr-compiler/src/compile-template/transformers/component/slotted-content.ts
Show resolved
Hide resolved
Co-authored-by: Will Harney <62956339+wjhsf@users.noreply.github.com>
Co-authored-by: Will Harney <62956339+wjhsf@users.noreply.github.com>
025b1de to
af2815f
Compare
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.
Details
The problem: It was observed that certain templates, when compiled in SSRv2, resulted in massive compiled outputs. There was no functional issue - the generated code worked properly - but sometimes the SSRv2 bundles would be 100x the size of SSRv1 bundles.
It was determined that this related to how we generate code when nesting many layers of slots. After arriving at a reduced repro (like the example template provided below), it became clear that the interplay of shadow-slot functions and light-slot functions results in a O(2^n) space complexity, where each new layer of nesting doubles the number of functions that are generated related to those slots.
The solution: There was not an easy way to fix this; this PR contains my third attempt. The semantics of LWC shadow and light slots simply make the problem very difficult to ameliorate. However, while the approach in this PR does add some new complexity, it successfully avoids the massive compiled outputs that we were seeing before.
The short explanation is that the shadow-slot-content functions require less contextual information compared to the light-slot-content functions. Therefore, it is possible to hoist these shadow-slot functions to the top of the template function, defining them once rather than defining them repeatedly at each layer of nesting. This short-circuits the exponential output sizes, resulting in roughly linear growth rather than exponential.
Example template
Output sizes, before and after
The following table demonstrates the compiled output sizes, both before (on
masterbranch) and after (on this branch) as a function of levels of nesting. The example provided above has eight levels of nesting; for the rows in this table representing fewer levels of nesting, one or more levels of nesting were commented out from the template before compiling, e.g.<x-nascent>and</x-nascent>were commented out for 7 layers of nesting.The difference becomes quite dramatic as increased nesting occurs. As one might expect based on the O(2^n) size complexity described from the original implementation, the size of the SSR-compiled-outputs on
masterbranch roughly doubles (plus change) with every additional layer of nesting added.Does this pull request introduce a breaking change?
Does this pull request introduce an observable change?
GUS work item
W-18501805