Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow skip hydration for LWR islands #5016

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

khangsfdc
Copy link

@khangsfdc khangsfdc commented Dec 6, 2024

LWR islands hydration can be optimized by skipping hydration for children of hydrated parent nodes, e.g.:

<!-- parent root component that requires hydration --> 
<my-cmp-island lwr-hydrate="load"> 
  <!-- child that needs hydration --> 
  <my-hydrate-child></my-hydrate-child>
  <!-- child that is SSR only (i.e. static), and can be skipped --> 
  <my-ssr-only-child data-lwc-skip-hydrate></my-ssr-only-child>
</my-cmp-island>

This has the benefit of:

  • Avoiding unnecesary hydration compute on the browser
  • Reducing LWR JS bundle size by not sending the JS for the child component

Details

Does this pull request introduce a breaking change?

  • 😮‍💨 No, it does not introduce a breaking change.

Does this pull request introduce an observable change?

  • 🤞 No, it does not introduce an observable change.

GUS work item

TD-0234873

<!-- parent root component that requires hydration -->
<my-cmp-island lwr-hydrate="load"> 
  <!-- child that needs hydration --> 
  <my-hydrate-child></my-hydrate-child>
  <!-- child that is SSR only (i.e. static), and can be skipped --> 
  <my-ssr-only-child lwr-skip-hydrate></my-ssr-only-child>
</my-cmp-island>
if (elm.hasAttribute('data-lwc-skip-hydrate')) {
return elm;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This skips hydration but I'm not sure what happens if the component is updated afterwards.

For example, if the parent has an update that would normally trigger VDOM diffing which would affect the child (e.g. removing it, passing in a new prop, etc.), LWC is not aware of the child, and rendering/callbacks will not be invoked.

Ideally we should have tests in test-hydration to verify this. It might work as-is (the element is just treated opaquely), but I'm not sure.

The custom element tag name may also not be defined, which could cause potential issues if other code is able to "hijack" it – e.g. someone injects code that does customElements.define('lightning-button', ...). Not sure if this is a legitimate attack vector but it's at least unexpected.

@@ -368,6 +368,10 @@ function hydrateCustomElement(
vnode: VCustomElement,
renderer: RendererAPI
): Node | null {
if (elm.hasAttribute('data-lwc-skip-hydrate')) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (elm.hasAttribute('data-lwc-skip-hydrate')) {
if (renderer.hasAttribute(elm, 'data-lwc-skip-hydrate')) {

This is our convention for abstracting differences between engine-dom and engine-server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants