Skip to content

chore: always return hydrate_start from template functions #11760

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 1 commit into from
May 23, 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
11 changes: 4 additions & 7 deletions packages/svelte/src/internal/client/dom/operations.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hydrate_anchor, hydrating } from './hydration.js';
import { hydrate_anchor, hydrate_start, hydrating } from './hydration.js';
import { DEV } from 'esm-env';
import { init_array_prototype_warnings } from '../dev/equality.js';
import { current_effect } from '../runtime.js';
Expand Down Expand Up @@ -96,24 +96,21 @@ export function first_child(fragment, is_text) {
return /** @type {DocumentFragment} */ (fragment).firstChild;
}

// when we _are_ hydrating, `fragment` is an array of nodes
var first_node = /** @type {import('#client').TemplateNode[]} */ (fragment)[0];

// if an {expression} is empty during SSR, there might be no
// text node to hydrate — we must therefore create one
if (is_text && first_node?.nodeType !== 3) {
if (is_text && hydrate_start?.nodeType !== 3) {
var text = empty();
var dom = /** @type {import('#client').TemplateNode[]} */ (
/** @type {import('#client').Effect} */ (current_effect).dom
);

dom.unshift(text);
first_node?.before(text);
hydrate_start?.before(text);

return text;
}

return hydrate_anchor(first_node);
return hydrate_anchor(hydrate_start);
}

/**
Expand Down
16 changes: 9 additions & 7 deletions packages/svelte/src/internal/client/dom/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ import { effect } from '../reactivity/effects.js';
/**
* @template {import("#client").TemplateNode | import("#client").TemplateNode[]} T
* @param {T} dom
* @returns {T}
*/
function push_template_node(dom) {
var effect = /** @type {import('#client').Effect} */ (current_effect);

if (effect.dom === null) {
effect.dom = dom;
}

return dom;
}

/**
Expand All @@ -35,7 +32,8 @@ export function template(content, flags) {

return () => {
if (hydrating) {
return push_template_node(is_fragment ? hydrate_nodes : hydrate_start);
push_template_node(is_fragment ? hydrate_nodes : hydrate_start);
return hydrate_start;
}

if (!node) {
Expand Down Expand Up @@ -87,7 +85,8 @@ export function ns_template(content, flags, ns = 'svg') {

return () => {
if (hydrating) {
return push_template_node(is_fragment ? hydrate_nodes : hydrate_start);
push_template_node(is_fragment ? hydrate_nodes : hydrate_start);
return hydrate_start;
}

if (!node) {
Expand Down Expand Up @@ -188,14 +187,17 @@ export function text(anchor) {
anchor.before((node = empty()));
}

return push_template_node(node);
push_template_node(node);
return node;
}

export function comment() {
// we're not delegating to `template` here for performance reasons
if (hydrating) {
return push_template_node(hydrate_nodes);
push_template_node(hydrate_nodes);
return hydrate_start;
}

var frag = document.createDocumentFragment();
var anchor = empty();
frag.append(anchor);
Expand Down
Loading