-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
" "
(space) in text nodes replaced with ""
(null string) in each blocks (svelte@5.0.0-next.97+)
#11932
Comments
" "
(space) in text nodes with ""
(null string) in svelte@5.0.0-next.97+
I don't think it's HMR i think is simply hydration...the reproduction works in the REPL too! |
Ah, good to know! I originally couldn't reproduce it in the REPL, but didn't try again once I figured out what was happening. |
@dummdidumm This is the intended behavior, isn't it? Isn't this just the new |
This case doesn't seem to fall into the listed cases there:
And the behavior on full reload versus dynamic reload differ, which presumably is not intended in any case. |
Btw i don't even think this is hydration...is probably the each loop thing with a bug |
It does look like However, I did find this behavior to be surprising and, more importantly, very difficult to pin down. Searching for related terms in GitHub Issues and more generally didn't yield any answers. Maybe that'll be improved once the Svelte 5 docs are more discoverable, though the docs don't actually indicate that this behavior should happen. |
Crap, I take it back. For clarity, my use case is that I have a "spellcheck" component that splits a string up into labeled parts (words, template variables, other). It then loops through those parts and either renders them directly with {#each parsed as part (part.start)}
{#if part.isVariable}
<code>{part.substr}</code>
{:else if !part.isWord && part.substr === '\n'}
<br />
{:else if !part.isWord || part.valid}
{part.substr}
{:else if part.isWord}
<span
role="button"
class="spelling-error"
onclick={async (event) => {
if (event.ctrlKey) await glossary.addTerm(part.substr);
}}
>
{part.substr}
</span>
{/if}
{/each} |
When If, after you've split the string, you've lost the information about which elements should have whitespace displayed between them, then there's not anything Svelte is going to be able to do. If you want to conditionally display whitespace, and you know what that condition is, then you could do something like |
The information is not lost. I'm still rendering the whitespace characters via You can see in this REPL that the split text includes entries that are just whitespace strings, and these are not being excluded by the loop. You can also see each entry in the text nodes of the dom, but that their data is set to |
Gotcha, okay, yeah, it looks like the core of the bug, then is something like: Apologies, I had missed that the original REPL had |
No worries! |
And yep, your minimal case is exactly right: REPL |
" "
(space) in text nodes with ""
(null string) in svelte@5.0.0-next.97+" "
(space) in text nodes with ""
(null string) when looping in svelte@5.0.0-next.97+
" "
(space) in text nodes with ""
(null string) when looping in svelte@5.0.0-next.97+" "
(space) in text nodes replaced with ""
(null string) in each blocks (svelte@5.0.0-next.97+)
I should have a fix for this ...PRing now |
Describe the bug
If you have a component that creates a bunch of text nodes via an
{#each}
loop, on HMR the spaces between those nodes disappear (see video). On page refresh the spaces come back, so this appears to be an HMR problem.svelte-hmr-issue.mp4
I tested Svelte versions and found that this issue started in
svelte@5.0.0-next.97
. Summary of tested versions (✅ means it worked as expected, ❌ means the issue was observed):It appears that on HMR the text nodes are being replaced such that:
💡 Any text node whose
data
was" "
gets replaced with a nullstring""
Reproduction
You can observe this behavior in the Svelte 5 REPL, but there you can only see it with the spaces removed. For a full reproduction, do the following:
npm create svelte@latest hmr-whitespace
)package.json
contents with the following to ensure the same dependency versions:npm install
)hmr-spaces/src/routes/+page.svelte
with:npm run dev
)[$('p')]
to inspect the element. Observe that itschildNodes
include Text nodes whosedata
value is a space (" "
)string
variable to trigger HMR[$('p')]
to inspect the element. Observe that the Text nodes in itschildNodes
that used to be spaces (" "
) are now null strings (""
).The video above shows what you should observe.
Repeating this test with any Svelte version above
5.0.0-next.97
yields the same outcome. All versions less than5.0.0-next.97
do NOT yield this outcome and work as expected.Logs
Similarly, in the Node console:
System Info
See the
package.json
content listed above for project dependency versions.Additional system info:
Severity
This behavior completely breaks my application
The text was updated successfully, but these errors were encountered: