-
-
Notifications
You must be signed in to change notification settings - Fork 432
Initial rendering remove all nodes from server side rendering and then render them again #911
Comments
Is this a bug with Sapper or a problem with Svelte's hydration? |
Why is this a problem at all? It's documented behavior. |
That should not an expected behaviour because removing all DOM nodes inside your APP node on the client after SSR should not happen. What else would be the advantage of SSR? It is as if you were using CSR. Hydration is okay. |
The main reason for doing SSR is that the time for the first meaningful paint is lower and that it's easier for search engines to crawl and index your website. Recreating the DOM client-side doesn't impact this at all. |
If this is happening, then it is definitely a bug. Removing everything and recreating it is wasteful and not how SSR is supposed to work. There are benefits to delivering static-content even without hydration but it is this hydration process that makes it a powerful pattern, despite the problems it poses. |
How is it a bug? It's right there in the documentation:
|
Because Svelte 'claims' nodes as long as the structures of the static version and dynamic version match, if these two versions of the site do not match then it will indeed bail and destroy them. Here is the relevant runtime code. This distinction is mentioned in the docs because without hydration Svelte appends (ignoring other DOM), with hydration Svelte forces the contents to match whatever is inside you Svelte app. This is discussed in the docs to prevent duplicate DOM when building a static version and to prevent accidental removal when hydrating. |
When re-hydrating a component, Svelte "claims" all of the child nodes that are still needed/wanted; removes all of the leftover unwanted nodes; and then re-appends the needed nodes to ensure they're in the correct order. The flickering is because when breaking on DOM modifications, The other aspect is that if you have a component X with children [A, B, C], because the component is mounted by re-appending the children, you'll visually see the nodes cycle around:
This is a consequence of using the same generic I don't know enough about the browser paint cycle to know if this could ever actually be visible to a normal user (i.e., outside of debugging). But "fixing" it would probably require changing the Svelte compiler to emit a call like |
In my case the javascript engages about a second after site load, if a user enters something into the auto focused input field in that second, it is cleared by this and looses focus. |
@pngwn can I ask you why the I think this is really a Has anyone found a way to get around in the meantime? |
This is particularly a problem when you have animations (even just css) on the elements as soon as they are rendered. |
There are a number of issues tracking this over in the Svelte repo with a list of some of them in sveltejs/svelte#4975. I agree this could be improved, but it has to be addressed in Svelte, so I'm going to close this one in favor of the Svelte issues |
Describe the bug
When load the page in the first time or click the "refresh" button in browser, the client side js is going to remove all html nodes rendered in server side, and then render them again.
To Reproduce
npx degit "sveltejs/sapper-template#rollup" my-app
)Expected behavior
Sapper shouldn't remove those html nodes in first time rendering.
Information about your Sapper Installation:
Your browser and the version: Chrome 79
Your operating system: macOS 10.15
Your hosting environment: Local
Sapper version: 0.27.9
Svelte version: 3.12.1
If it is an exported (npm run export) or dynamic application: dynamic
Whether your application uses Webpack or Rollup: Rollup
The text was updated successfully, but these errors were encountered: