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] {#if} block with several reactive children does not re-render properly #12230

Closed
GauBen opened this issue Jun 30, 2024 · 12 comments · Fixed by #12242, #12245 or #12261
Closed

[Svelte 5] {#if} block with several reactive children does not re-render properly #12230

GauBen opened this issue Jun 30, 2024 · 12 comments · Fixed by #12242, #12245 or #12261
Assignees
Labels
Milestone

Comments

@GauBen
Copy link
Contributor

GauBen commented Jun 30, 2024

Describe the bug

Given the following +page.js:

export const load = ({ url }) => {
  return url.searchParams.get("event")
    ? {
        event: {
          author: "John Doe",
          body: "Body",
        },
      }
    : {};
};

And a +page.svelte:

<script lang="ts">
  const { data } = $props();
  const { event } = $derived(data);
</script>

{#if event}
  <h1>{event.author}</h1>
  <p>{event.body}</p>
{/if}

<a href="?event=1">Open event</a>
<a href="?">Close</a>

Clicking Open event will show a title and a body, clicking Close will close it, clicking Open event again will do nothing.

Very small changes can make it work:

  • Removing <p>{event.body}</p> will fix the issue
  • Adding $inspect(event) will fix the issue

(This is likely to be a quatum physics issue given that observing the behavior changes it 😂)

Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-mhzb7t?file=src%2Froutes%2F%2Bpage.js

Logs

No response

System Info

5.0.0-next.167

Severity

blocking an upgrade

@GauBen
Copy link
Contributor Author

GauBen commented Jul 1, 2024

Hey @dummdidumm thanks for working on this, I just bumped to 168 and the bug is still happening

@dummdidumm dummdidumm reopened this Jul 1, 2024
@dummdidumm dummdidumm added this to the 5.0 milestone Jul 1, 2024
@dummdidumm dummdidumm added the bug label Jul 1, 2024
@dummdidumm
Copy link
Member

dummdidumm commented Jul 1, 2024

(me referencing that issue was a mistake, this bug seems unrelated)

REPL reproduction

@GauBen
Copy link
Contributor Author

GauBen commented Jul 1, 2024

You managed to reproduce! I failed doing so without Kit

@GauBen
Copy link
Contributor Author

GauBen commented Jul 1, 2024

It turns out the bug is still there, but instead of not displaying the content of the {#if} after the first toggle, it happens after the second one:

Recording.2024-07-01.210516.mp4

I can try creating a repro in the REPL if it helps. The exact code is the following:

https://github.com/GauBen/timeline/blob/2f0e1bd1c9b723158e25d8833b98e4dd5d0285fd/packages/app/src/routes/(authentified)/(app)/%5B%5Busername%3Dusername%5D%5D/%5B...date%3Dplaindate%5D/%2Bpage.svelte#L62-L74

@trueadm
Copy link
Contributor

trueadm commented Jul 1, 2024

@GauBen Have you tried the latest version? A REPL link would be preferable too :)

@GauBen
Copy link
Contributor Author

GauBen commented Jul 1, 2024

Yes, I bumped to 5.0.0-next.169 and instead of not showing after the first toggle, it fails after the second toggle

@trueadm
Copy link
Contributor

trueadm commented Jul 1, 2024

@GauBen Is it the same in dev and prod?

@GauBen
Copy link
Contributor Author

GauBen commented Jul 1, 2024

Just tried, it behaves the same in dev and prod

I don't know how to reproduce in REPL yet

@GauBen
Copy link
Contributor Author

GauBen commented Jul 1, 2024

If that helps:

  • event.id is a bigint
  • event.date is a Date object, not a SvelteDate one

Update:

  • Adding a top-level $inspect(event); fixes the issue
  • {#if event}
      <h1>{event.author.displayName}</h1>
      <p>{event.body}</p>
      <a href={$page.url.pathname}>Close</a> <!-- Removing this will fix the issue -->
    {/if}

@GauBen
Copy link
Contributor Author

GauBen commented Jul 1, 2024

@trueadm trueadm reopened this Jul 1, 2024
@henrikvilhelmberglund
Copy link

Also works when replacing {#if event} with {#if data && event}.

@GauBen
Copy link
Contributor Author

GauBen commented Jul 2, 2024

Thanks for all the hard work Svelte team!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment