chore: always return hydrate_start from template functions (#11760)

pull/11761/head
Rich Harris 4 months ago committed by GitHub
parent c239cdf3af
commit 4a708dd005
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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 { DEV } from 'esm-env';
import { init_array_prototype_warnings } from '../dev/equality.js'; import { init_array_prototype_warnings } from '../dev/equality.js';
import { current_effect } from '../runtime.js'; import { current_effect } from '../runtime.js';
@ -96,24 +96,21 @@ export function first_child(fragment, is_text) {
return /** @type {DocumentFragment} */ (fragment).firstChild; 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 // if an {expression} is empty during SSR, there might be no
// text node to hydrate — we must therefore create one // 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 text = empty();
var dom = /** @type {import('#client').TemplateNode[]} */ ( var dom = /** @type {import('#client').TemplateNode[]} */ (
/** @type {import('#client').Effect} */ (current_effect).dom /** @type {import('#client').Effect} */ (current_effect).dom
); );
dom.unshift(text); dom.unshift(text);
first_node?.before(text); hydrate_start?.before(text);
return text; return text;
} }
return hydrate_anchor(first_node); return hydrate_anchor(hydrate_start);
} }
/** /**

@ -8,7 +8,6 @@ import { effect } from '../reactivity/effects.js';
/** /**
* @template {import("#client").TemplateNode | import("#client").TemplateNode[]} T * @template {import("#client").TemplateNode | import("#client").TemplateNode[]} T
* @param {T} dom * @param {T} dom
* @returns {T}
*/ */
function push_template_node(dom) { function push_template_node(dom) {
var effect = /** @type {import('#client').Effect} */ (current_effect); var effect = /** @type {import('#client').Effect} */ (current_effect);
@ -16,8 +15,6 @@ function push_template_node(dom) {
if (effect.dom === null) { if (effect.dom === null) {
effect.dom = dom; effect.dom = dom;
} }
return dom;
} }
/** /**
@ -35,7 +32,8 @@ export function template(content, flags) {
return () => { return () => {
if (hydrating) { 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) { if (!node) {
@ -87,7 +85,8 @@ export function ns_template(content, flags, ns = 'svg') {
return () => { return () => {
if (hydrating) { 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) { if (!node) {
@ -188,14 +187,17 @@ export function text(anchor) {
anchor.before((node = empty())); anchor.before((node = empty()));
} }
return push_template_node(node); push_template_node(node);
return node;
} }
export function comment() { export function comment() {
// we're not delegating to `template` here for performance reasons // we're not delegating to `template` here for performance reasons
if (hydrating) { if (hydrating) {
return push_template_node(hydrate_nodes); push_template_node(hydrate_nodes);
return hydrate_start;
} }
var frag = document.createDocumentFragment(); var frag = document.createDocumentFragment();
var anchor = empty(); var anchor = empty();
frag.append(anchor); frag.append(anchor);

Loading…
Cancel
Save