Skip to content

fix: always run if block code the first time #14597

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

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rare-cheetahs-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: always run `if` block code the first time
8 changes: 4 additions & 4 deletions packages/svelte/src/internal/client/dom/blocks/if.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
set_hydrating
} from '../hydration.js';
import { block, branch, pause_effect, resume_effect } from '../../reactivity/effects.js';
import { HYDRATION_START_ELSE } from '../../../../constants.js';
import { HYDRATION_START_ELSE, UNINITIALIZED } from '../../../../constants.js';

/**
* @param {TemplateNode} node
Expand All @@ -30,8 +30,8 @@ export function if_block(node, fn, elseif = false) {
/** @type {Effect | null} */
var alternate_effect = null;

/** @type {boolean | null} */
var condition = null;
/** @type {UNINITIALIZED | boolean | null} */
var condition = UNINITIALIZED;

var flags = elseif ? EFFECT_TRANSPARENT : 0;

Expand All @@ -54,7 +54,7 @@ export function if_block(node, fn, elseif = false) {
if (hydrating) {
const is_else = /** @type {Comment} */ (anchor).data === HYDRATION_START_ELSE;

if (condition === is_else) {
if (!!condition === is_else) {
// Hydration mismatch: remove everything inside the anchor and start fresh.
// This could happen with `{#if browser}...{/if}`, for example
anchor = remove_nodes();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { test } from '../../test';

// even {#if true} or {#if false} should be kept as an if block, because it could be {#if browser} originally,
// which is then different between client and server.
export default test({
server_props: {
condition: true
},

props: {
condition: false
},

trim_whitespace: false
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!--[--><!--[--><!--]--> <div><!----><!----></div> hello<!--]-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
let { condition } = $props();
</script>

{#if condition}
<slot />
{/if}

<div>
<slot />
</div>

hello
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!--[--><!--[!--><p>foo</p><!--]--><!--]-->
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
let { condition } = $props();
</script>

{#if true}
{#if condition}
<p>foo</p>
{:else}
<p>bar</p>
Expand Down
Loading