Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

HydrateElement (SSR+Hydration for native HTMLElement / Web Components) #926

Closed
thescientist13 opened this issue Apr 1, 2022 Discussed in #548 · 1 comment
Closed
Labels
CLI feature New feature or request SSR
Milestone

Comments

@thescientist13
Copy link
Member

thescientist13 commented Apr 1, 2022

Overview

(Discussed in #548)

Originally posted by thescientist13 April 11, 2021
Have been toying around with the idea of a "hydratable" Web Component base class that could be leveraged by Greenwood to reduce the cost of shipping Web Component JS to the client, as is unfortunate in that WCs do require JS to work.

Something like

class MyCounter extends HydrateElement {
  constructor() {
    super();
    this.count = 0;
    this.attachShadow({ mode: "open" }); // TODO only works when mode is "open"
  }

  async connectedCallback() {
    await this.rehydrate(this, async () => {
      this.count = parseInt(this.querySelector('#count').textContent, 10);
    });
    
    this.shadowRoot.appendChild(template.content.cloneNode(true));
    this.shadowRoot.getElementById("inc").onclick = () => this.inc();
    this.shadowRoot.getElementById("dec").onclick = () => this.dec();
    this.update();
  }

  inc() {
    this.update(++this.count);
  }

  dec() {
    this.update(--this.count);
  }

  update(count) {
    this.shadowRoot.getElementById("count").innerHTML = count || this.count;
  }
}
@thescientist13 thescientist13 added CLI SSR feature New feature or request labels Apr 1, 2022
@thescientist13 thescientist13 added this to the 1.0 milestone Apr 1, 2022
@thescientist13
Copy link
Member Author

thescientist13 commented May 29, 2022

This might work well in conjunction with wcc and with #880

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
CLI feature New feature or request SSR
Projects
Development

No branches or pull requests

1 participant