Skip to content

chore: remove redundant hydration code #9698

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 6 commits into from
Nov 30, 2023
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/sour-forks-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

chore: remove redundant hydration code
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might not be an accurate description anymore now that it's adding rather than removing code. I don't care so much about the changeset, but just thought it was funny 😆

Screenshot from 2023-11-29 20-19-53

12 changes: 9 additions & 3 deletions packages/svelte/src/internal/client/hydration.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Handle hydration

import { schedule_task } from './runtime';

/** @type {null | Array<Text | Comment | Element>} */
export let current_hydration_fragment = null;

Expand Down Expand Up @@ -68,12 +70,16 @@ export function hydrate_block_anchor(anchor_node, is_controlled) {
let fragment = target_node.$$fragment;
if (fragment === undefined) {
fragment = get_hydration_fragment(target_node);
// @ts-ignore remove to prevent memory leaks
target_node.$$fragment = undefined;
} else {
schedule_task(() => {
// @ts-expect-error clean up memory
target_node.$$fragment = undefined;
});
}
set_current_hydration_fragment(fragment);
} else {
set_current_hydration_fragment([/** @type {Element} */ (target_node.firstChild)]);
const first_child = /** @type {Element | null} */ (target_node.firstChild);
set_current_hydration_fragment(first_child === null ? [] : [first_child]);
}
}
}
13 changes: 6 additions & 7 deletions packages/svelte/src/internal/client/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ import {
push,
current_component_context,
pop,
schedule_task,
managed_render_effect
schedule_task
} from './runtime.js';
import {
current_hydration_fragment,
Expand Down Expand Up @@ -211,8 +210,10 @@ function close_template(dom, is_fragment, anchor) {
? dom
: /** @type {import('./types.js').TemplateNode[]} */ (Array.from(dom.childNodes))
: dom;
if (anchor !== null && current_hydration_fragment === null) {
insert(current, null, anchor);
if (anchor !== null) {
if (current_hydration_fragment === null) {
insert(current, null, anchor);
}
}
block.d = current;
}
Expand Down Expand Up @@ -1396,9 +1397,7 @@ function if_block(anchor_node, condition_fn, consequent_fn, alternate_fn) {
} else if (current_hydration_fragment !== null) {
const comment_text = /** @type {Comment} */ (current_hydration_fragment?.[0])?.data;
if (
(!comment_text &&
// Can happen when a svelte:element that is turned into a void element has an if block inside
current_hydration_fragment[0] !== null) ||
!comment_text ||
(comment_text === 'ssr:if:true' && !result) ||
(comment_text === 'ssr:if:false' && result)
) {
Expand Down