plentico / pico

Svelte-like reactive UI compiler written in Go
2 stars 0 forks source link

Replace target instead of appending to it #6

Open jimafisk opened 2 years ago

jimafisk commented 2 years ago

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: ```html
My component
``` ...and a page like this: ```html

some content

``` ...the result after mounting would be: ```html

some content

My component
``` I _believe_ it puts the new component [after existing children](https://youtu.be/wYCMnphN5c8?t=1842) 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: ```html
My component
``` ...and a page like this: ```html

some content we want to get rid of

``` ...the result after mounting would be: ```html
My component
```

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.