Skip to content

Commit

Permalink
chore: always return hydrate_start from template functions (#11760)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed May 23, 2024
1 parent c239cdf commit 4a708dd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
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

0 comments on commit 4a708dd

Please sign in to comment.