Skip to content

Commit

Permalink
fix: bail-out of hydrating head if no anchor is found (#12541)
Browse files Browse the repository at this point in the history
* fix: bail-out of hydrating head if no anchor is found

* add failing test

* fix

* fix comment

---------

Co-authored-by: Rich Harris <[email protected]>
  • Loading branch information
trueadm and Rich-Harris authored Jul 22, 2024
1 parent 5253c8f commit 346cf96
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-boxes-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: bail-out of hydrating head if no anchor is found
19 changes: 14 additions & 5 deletions packages/svelte/src/internal/client/dom/blocks/svelte-head.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @import { TemplateNode } from '#client' */
import { hydrate_node, hydrating, set_hydrate_node } from '../hydration.js';
import { hydrate_node, hydrating, set_hydrate_node, set_hydrating } from '../hydration.js';
import { empty } from '../operations.js';
import { block } from '../../reactivity/effects.js';
import { HEAD_EFFECT } from '../../constants.js';
Expand Down Expand Up @@ -36,21 +36,30 @@ export function head(render_fn) {
}

while (
head_anchor.nodeType !== 8 ||
/** @type {Comment} */ (head_anchor).data !== HYDRATION_START
head_anchor !== null &&
(head_anchor.nodeType !== 8 || /** @type {Comment} */ (head_anchor).data !== HYDRATION_START)
) {
head_anchor = /** @type {TemplateNode} */ (head_anchor.nextSibling);
}

head_anchor = set_hydrate_node(/** @type {TemplateNode} */ (head_anchor.nextSibling));
} else {
// If we can't find an opening hydration marker, skip hydration (this can happen
// if a framework rendered body but not head content)
if (head_anchor === null) {
set_hydrating(false);
} else {
head_anchor = set_hydrate_node(/** @type {TemplateNode} */ (head_anchor.nextSibling));
}
}

if (!hydrating) {
anchor = document.head.appendChild(empty());
}

try {
block(() => render_fn(anchor), HEAD_EFFECT);
} finally {
if (was_hydrating) {
set_hydrating(true);
head_anchor = hydrate_node; // so that next head block starts from the correct node
set_hydrate_node(/** @type {TemplateNode} */ (previous_hydrate_node));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from '../../test';

export default test({
test(assert, target, snapshot, component, window) {
assert.equal(window.document.querySelectorAll('meta').length, 2);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<meta name="description" content="some description"> <meta name="keywords" content="some keywords">
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<svelte:head>
<meta name="description" content="some description" />
<meta name="keywords" content="some keywords" />
</svelte:head>

<div>Just a dummy page.</div>
3 changes: 2 additions & 1 deletion packages/svelte/tests/hydration/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ const { test, run } = suite<HydrationTest>(async (config, cwd) => {
});

const override = read(`${cwd}/_override.html`);
const override_head = read(`${cwd}/_override_head.html`);

fs.writeFileSync(`${cwd}/_output/body.html`, rendered.html + '\n');
target.innerHTML = override ?? rendered.html;

if (rendered.head) {
fs.writeFileSync(`${cwd}/_output/head.html`, rendered.head + '\n');
head.innerHTML = rendered.head;
head.innerHTML = override_head ?? rendered.head;
}

config.before_test?.();
Expand Down

0 comments on commit 346cf96

Please sign in to comment.