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

Recover component state from DOM #234

Open
eight04 opened this issue Feb 26, 2022 · 1 comment
Open

Recover component state from DOM #234

eight04 opened this issue Feb 26, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@eight04
Copy link
Contributor

eight04 commented Feb 26, 2022

This is a feature request.

For example, I have a link component:

<script>
import {onMount} from "svelte";
export let url, text;

let linkElement;
onMount(() => {
  // some fancy stuff
})
</script>
<a href={url} bind:this={linkElement}>{text}</a>

To hydrate it, we have to stringify url and text property:

<Link hydrate-client={{link: "http://example.com", text: "Test"}} />

Which produces code like this:

{
  linkABC: {
    component: "Link.blabla.js",
    props: {
      url: "http://example.com",
      text: "Test"
    }
  }
}

However, these props are already available in DOM, result in data duplication.

Proposal

Support "server-only props" via the original syntax:

<Link link="example.com" text="Test" hydrate-client={{}} />

link and text will still be rendered in the server.

Support a recover module function in the component:

<script type="module">
export function recover(node) {
  const a = node.children[0];
  return {url: a.href, text: a.textContent};
}
</script>

<!-- ... other component stuff ... -->

When hydrating:

blabla.then(([comp, props]) => {
  if (comp.recover) {
    Object.assign(props, comp.recover(target));
  }
  new comp.default({ ... })
})

I don't know whether this is already supported by svelte. Is svelte component able to recover its state from pre-rendered DOM?

@eight04
Copy link
Contributor Author

eight04 commented Mar 7, 2022

The example usage in this issue - the template is static and we only want to add js code in onMount - is probably covered by #237

@eight04 eight04 added the enhancement New feature or request label Nov 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant