Skip to content

partial hydration (side effect free custom elements, e.g. tree shaking) #11

Closed

Description

@thescientist13

Type of Change

  • New Feature Request

Summary

To build off of #3 , the next level of granularity to try and achieve in hydration strategies is partial hydration, which is

Use knowledge of the client vs server to only ship code / serialize data needed in the browser.

Details

Which I basically interpret to mean that given a component like this

class Header extends HTMLElement {
  constructor() {
    super();

    if (this.shadowRoot) {
      const button = this.shadowRoot.querySelector('button');

      button.addEventListener('click', this.toggle);
    }
  }

  connectedCallback() {
    if (!this.shadowRoot) {
      this.attachShadow({ mode: 'open' });
      this.shadowRoot.innerHTML = this.render();
    }
  }

  toggle() {
    alert('this.toggle clicked!');
  }

  render() {
    return `
      <style>
        .header {
          background-color: #192a27;
          min-height: 30px;
          padding: 10px;
          font-size: 1.2rem;
        }

        ...
      </style>

      <header class="header">
        ...
      </header>
    `;
  }
}

export { Header };
customElements.define('wcc-header', Header);

Since there are no props or state, we should be able to infer

  • Only one render is needed, which can be handled via declarative shadow dom
  • We still need the event handler

So in an ideal world, this is what would actually be shipped to the browser

class Header extends HTMLElement {
  constructor() {
    super();

    if (this.shadowRoot) {
      const button = this.shadowRoot.querySelector('button');

      button.addEventListener('click', this.toggle);
    }
  }

  toggle() {
    alert('this.toggle clicked!');
  }
}

export { Header };
customElements.define('wcc-header', Header);

Activity

  1. added this to the 1.0 milestone on May 1, 2022
  2. added
    featureNew feature or request
    and removed
    featureNew feature or request
    on May 27, 2022
  3. locked and limited conversation to collaborators on Oct 2, 2024
  4. converted this issue into a discussion #169 on Oct 2, 2024
  5. moved this from 📋 Backlog to ✅ Done in [WCC] General Activitieson Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions