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

Replace target instead of appending to it #6

Open
jimafisk opened this issue Nov 28, 2021 · 0 comments
Open

Replace target instead of appending to it #6

jimafisk opened this issue Nov 28, 2021 · 0 comments

Comments

@jimafisk
Copy link
Member

When Svelte hydrates, it appends a child component to its parent. In other words, it nests the new component inside the existing component.

How it currently works

So if you have a component like this:

<div>My component</div>

...and a page like this:

<body>
  <main id='target'>
    <p>some content</p>
  <main>
</body>

...the result after mounting would be:

<body>
  <main id='target'>
    <p>some content</p>
    <div>My component</div>
  <main>
</body>

I believe it puts the new component after existing children elements.

There have been several discussions about replacing the parent element instead of appending to it:

Some of the worry around replacing elements is that it's expensive and destructive to other child elements that potentially exist already in the dom:

Destroying existing child element

So if you have a component like this:

<div>My component</div>

...and a page like this:

<body>
  <main id='target'>
    <p>some content we want to get rid of</p>
  <main>
</body>

...the result after mounting would be:

<body>
  <div>My component</div>
</body>

You could potentially check that the elements that are being replaced are the same as the hydrated elements, but that could be a lot of work and potentially slow down performance. For Plenti we're just creating HTML fallbacks that should exactly mirror the hydrated components at their initial state, so it shouldn't be destructive in any way.

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

No branches or pull requests

1 participant