Skip to content

Hydration breaks with conditional browser rendering and slot fallback #7653

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

Open
plmrry opened this issue Jul 1, 2022 · 1 comment
Open

Comments

@plmrry
Copy link
Contributor

plmrry commented Jul 1, 2022

Describe the bug

I'm running into a pretty mysterious bug involving the use browser to conditionally render and (in a separate component!) a slot with fallback content.

Here's my setup:

An array is used to render one of two components:
routes/index.svelte:

<script>
  import BreakHydration from '$lib/BreakHydration.svelte';
  import Text from '$lib/Text.svelte';

  const blocks = [
    { type: `text`, text: `text one` },
    { type: `breakhydration`, text: `this breaks hydration` },
    { type: `text`, text: `text two` },
    { type: `text`, text: `text three` }
  ];
</script>

{#each blocks as { type, text }, index (index)}
  <div>index: {index}</div>
  <div>should be: {text}</div>
  {#if type === `breakhydration`}
    <BreakHydration {text} />
  {:else}
    <Text {text} />
  {/if}
  <div style:height="2rem" />
{/each}

Text.svelte uses a slot with fallback content:

<script>
  export let text;
</script>

<p style:border="1px dashed black">
  <slot>
    {@html text}
  </slot>
</p>

BreakHydration.svelte renders different things depending on browser:

<script>
  import { browser } from '$app/env';
  export let text;
</script>

{#if browser}
  <div style:border="1px dashed black">{text}</div>
{:else}
  <ul>
    <li>{text}</li>
  </ul>
{/if}

And the result:
Screen Shot 2022-07-01 at 10 49 38 AM

Reproduction

StackBlitz

Logs

No response

System Info

StackBlitz

Severity

serious, but I can work around it

Additional Information

No response

@benmccann benmccann transferred this issue from sveltejs/kit Jul 2, 2022
@plmrry
Copy link
Contributor Author

plmrry commented Jul 5, 2022

I've created another reproduction of this bug without using SvelteKit

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

No branches or pull requests

1 participant