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

Svelte 5: SSR breaks rendering of Select component when HMR is enabled #12601

Closed
pheuter opened this issue Jul 25, 2024 · 26 comments
Closed

Svelte 5: SSR breaks rendering of Select component when HMR is enabled #12601

pheuter opened this issue Jul 25, 2024 · 26 comments
Assignees
Labels
Milestone

Comments

@pheuter
Copy link

pheuter commented Jul 25, 2024

Describe the bug

image

See the minimal repro I created.

Reproduction

https://github.com/pheuter/hmr-repro

Logs

No response

System Info

System:
    OS: macOS 15.0
    CPU: (16) arm64 Apple M3 Max
    Memory: 2.91 GB / 48.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.16.0 - ~/.nvm/versions/node/v20.16.0/bin/node
    npm: 10.8.1 - ~/.nvm/versions/node/v20.16.0/bin/npm
    bun: 1.1.20 - ~/.bun/bin/bun
  Browsers:
    Chrome: 127.0.6533.73
    Safari: 18.0
  npmPackages:
    svelte: ^5.0.0-next.1 => 5.0.0-next.198

Severity

blocking an upgrade

@trueadm
Copy link
Contributor

trueadm commented Jul 25, 2024

I'm not sure if it's directly related (still digging into this) but I encounter an exception SUPERFORMS_LEGACY is not defined when refreshing the page.

Also this seems unrelated to HMR, but rather seems like a hydration only issue.

@pheuter
Copy link
Author

pheuter commented Jul 25, 2024

Hm, I'm not seeing any exceptions or errors in my browser or node console when refreshing.

Also this seems unrelated to HMR, but rather seems like a hydration only issue.

If I go into svelte.config.js and set hmr: false, the refresh issue goes away, which is why I thought it's related to HMR.

@trueadm
Copy link
Contributor

trueadm commented Jul 25, 2024

Ah you're right, it is related to hmr being enabled, but not during the client – more from the server or vps it seems?

It seems related to the generation of the svelte class. From the server the class is called s-413vZPMk7oiT yet when rendered on the client, it is svelte-82qwg8. The first class doesn't actually reference any CSS selector, so it's missing the styles entirely for the global styles.

@trueadm trueadm added the bug label Jul 25, 2024
@trueadm trueadm added this to the 5.0 milestone Jul 25, 2024
@trueadm trueadm self-assigned this Jul 25, 2024
@dominikg
Copy link
Member

vite-plugin-svelte uses a custom cssHash function during dev to ensure that dom nodes don't get different style hashes every time you edit css in your .svelte file.

This is what you see as s-413vZPMk7oiT ( its a hash of the file path)

@trueadm
Copy link
Contributor

trueadm commented Jul 25, 2024

https://github.com/sveltejs/vite-plugin-svelte/blob/bd915c1317127a716a22cdd8c44d66d766e0f513/packages/vite-plugin-svelte/src/utils/compile.js#L66

is options.emitCss not stable?

It seems stable and i can confirm commenting that code out doesn't fix the issue. What's strange is that the class names don't match any selectors in the injected CSS string.

Update: it was this after all, I was seeing a cached svelte-select doh

@trueadm
Copy link
Contributor

trueadm commented Jul 25, 2024

@trueadm
Copy link
Contributor

trueadm commented Jul 25, 2024

I also get a console error in the terminal when refreshing on the Select page:

SvelteKitError: Not found: /workspaces/1/Select.svelte

Not sure if it's connected to this issue though.

@dominikg
Copy link
Member

@dominikg Is this line meant to be used here?

https://github.com/sveltejs/vite-plugin-svelte/blob/bd915c1317127a716a22cdd8c44d66d766e0f513/packages/vite-plugin-svelte/src/utils/compile.js#L169

It never reaches it with hmr enabled.

the only reason that line isn't reached would be emitCss false (which we ruled out above) or hasCss false, which would mean ssr compile did not produce css.

@pheuter
Copy link
Author

pheuter commented Jul 28, 2024

@dominikg Upgrading @sveltejs/vite-plugin-svelte to 4.0.0-next.5 appears to break SSR with two different error messages now, and on more pages too (even those without the Select component):

Unhandled Promise Rejection: HierarchyRequestError: The operation would yield an incorrect node tree.
	in PageWithSidePanel.svelte
	in Page.svelte
	in +page.svelte
	in +layout.svelte
	in pane.svelte
	in pane-group.svelte
	in res...

and

Unhandled Promise Rejection: TypeError: null is not an object (evaluating 'node.nodeType')
	in InfoPanel.svelte
	in PageColumns.svelte
	in Page.svelte
	in +page.svelte
	in pane.svelte
	in pane-group.svelte
	in resizable-pane-gro...

@trueadm
Copy link
Contributor

trueadm commented Jul 28, 2024

@pheuter Are you using the latest Svelte too?

@pheuter
Copy link
Author

pheuter commented Jul 28, 2024

I tried with both 199 and 200, no difference in terms of the errors above. Think I'm able to repro the new null is not an object (evaluating 'node.nodeType') issue, will update the repo shortly, it's a weird one...

@pheuter
Copy link
Author

pheuter commented Jul 28, 2024

I've updated the repro here to showcase the new Unhandled Promise Rejection: TypeError: null is not an object (evaluating 'node.nodeType') issue.

This is a weird one and I'm not sure how to best explain it other than to note that it happens when you SSR on the page and the {:else if} branch happens to be the active one:

<div class="flex items-center gap-2">
	<LucideMapPin class="h-4 w-4" />
	{#if $formData.role === 'editor'}
		{$formData.name} (Editor)
	{:else if $formData.role === 'admin'}
		{$formData.name} (Admin)
	{:else}
		{$formData.name}
	{/if}
</div>

Note that this only starts to happen after the upgrade to vite-plugin-svelte 4.0.0-next.5.

/cc @dominikg @trueadm

@trueadm
Copy link
Contributor

trueadm commented Jul 28, 2024

@pheuter I assume this is with HMR enabled too? What happens when you disable HMR?

@pheuter
Copy link
Author

pheuter commented Jul 28, 2024

@trueadm Correct, the SSR rendering works just fine with hmr: false.

@dominikg
Copy link
Member

the error comes from

and it looks like the loop ran out of siblings before encountering an end marker.

If it was while(node) instead of while(true) that would not happen, however i assume something else borked the end marker before and that loop rightfully expected it to exist?

removing this line https://github.com/sveltejs/vite-plugin-svelte/blob/2670424fc7f66cef6797286bee4e0be1aa03b4a8/packages/vite-plugin-svelte/src/utils/esbuild.js#L78 in vite-plugin-svelte fixes it, so it seems like hmr code in prebundled dependencies is somehow required for their hydration?

@trueadm
Copy link
Contributor

trueadm commented Jul 28, 2024

@dominikg This seems like an esbuild bug then?

@trueadm
Copy link
Contributor

trueadm commented Aug 7, 2024

@pheuter Can you please try with the latest version of vite-plugin-svelte?

@vyconm
Copy link

vyconm commented Aug 7, 2024

@trueadm the issue persists for me with vite-plugin-svelte 4.0.0-next.6, if that's the newest

@dominikg
Copy link
Member

dominikg commented Aug 7, 2024

just to be sure can you try again with pnpm dev --force ? i'm at a loss here what might cause it

@vyconm
Copy link

vyconm commented Aug 7, 2024

hm, after forcing re-op with --force I cannot seem to recreate it in the reproduction repo linked from the author anymore, having ssr and hmr enabled.

The same error persists when using a third party library though, and the author referenced it as a svelte issue:
huntabyte/bits-ui#620

That is recreatable even with vite-plugin-svelte-next6, but it might be a library issue, not related to svelte anymore: https://stackblitz.com/edit/github-lahshv?file=src%2Froutes%2F%2Bpage.svelte

@pheuter
Copy link
Author

pheuter commented Aug 7, 2024

@trueadm The issues I was encountering appear to be resolved with 4.0.0-next.6 👍

@vyconm
Copy link

vyconm commented Aug 9, 2024

the issue is now also fixed on the libraries side :

huntabyte/shadcn-svelte@3497b07

I think the issue can be closed! :)

@dominikg
Copy link
Member

dominikg commented Aug 9, 2024

looks like everything works, thanks for confirming

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants