Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork4.6k
fix: improve hydration of altered html#16226
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': patch | ||
--- | ||
fix: improve hydration of altered html |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -3,7 +3,8 @@ import { hydrate_node, hydrating, set_hydrate_node, set_hydrating } from '../hyd | ||
import { create_text, get_first_child, get_next_sibling } from '../operations.js'; | ||
import { block } from '../../reactivity/effects.js'; | ||
import { COMMENT_NODE, HEAD_EFFECT } from '#client/constants'; | ||
import { HYDRATION_END, HYDRATION_ERROR, HYDRATION_START } from '../../../../constants.js'; | ||
import { hydration_mismatch } from '../../warnings.js'; | ||
/** | ||
* @type {Node | undefined} | ||
@@ -58,6 +59,41 @@ export function head(render_fn) { | ||
try { | ||
block(() => render_fn(anchor), HEAD_EFFECT); | ||
if (hydrating) { | ||
if (hydrate_node === null || /** @type {Comment} */ (hydrate_node).data !== HYDRATION_END) { | ||
7nik marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
hydration_mismatch(); | ||
throw HYDRATION_ERROR; | ||
} | ||
} | ||
} catch (error) { | ||
// re-mount only this svelte:head | ||
if (was_hydrating && head_anchor && error === HYDRATION_ERROR) { | ||
// Here head_anchor is the node next after HYDRATION_START | ||
/** @type {Node | null} */ | ||
let prev = head_anchor.previousSibling; | ||
/** @type {Node | null} */ | ||
let next = head_anchor; | ||
// remove nodes that failed to hydrate | ||
while ( | ||
prev !== null && | ||
(prev.nodeType !== COMMENT_NODE || /** @type {Comment} */ (prev).data !== HYDRATION_END) | ||
) { | ||
document.head.removeChild(prev); | ||
prev = next; | ||
next = get_next_sibling(/** @type {Node} */ (next)); | ||
} | ||
if (prev?.parentNode) document.head.removeChild(prev); | ||
if (next !== null) { | ||
// allow the next head block try to hydrate | ||
head_anchor = set_hydrate_node(/** @type {TemplateNode} */ (next)); | ||
} | ||
set_hydrating(false); | ||
anchor = document.head.appendChild(create_text()); | ||
block(() => render_fn(anchor), HEAD_EFFECT); | ||
} else { | ||
throw error; | ||
} | ||
} finally { | ||
if (was_hydrating) { | ||
set_hydrating(true); | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { flushSync } from 'svelte'; | ||
import { test } from '../../test'; | ||
export default test({ | ||
expect_hydration_error: true, | ||
test(assert, target, snapshot, component, window) { | ||
assert.equal(window.document.querySelectorAll('meta').length, 5); | ||
const [button] = target.getElementsByTagName('button'); | ||
button.click(); | ||
flushSync(); | ||
/** @type {NodeList} */ | ||
const metas = window.document.querySelectorAll('meta[name=count]'); | ||
assert.equal(metas.length, 4); | ||
metas.forEach((meta) => assert.equal(/** @type {HTMLMetaElement} */ (meta).content, '2')); | ||
} | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<!--[--><meta name="count" content="1"><!----><!--]--><!--[--><meta name="count" content="1"><!----><!--]--> | ||
<!----><meta name="count" content="1"> <meta name="will-be-missing"> <meta name="count" content="1"> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<!--[--><meta name="count" content="1" /><!----><!--]--><!--[--><meta name="count" content="1" /> | ||
<meta name="count" content="1" /><!----><!--]--><!--[--><meta | ||
name="count" | ||
content="1" | ||
/><!----><!--]--> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<script> | ||
let { children } = $props(); | ||
</script> | ||
<svelte:head> | ||
{@render children()} | ||
</svelte:head> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<script> | ||
import Head from './head.svelte'; | ||
let count = $state(1); | ||
</script> | ||
<Head> | ||
<meta name="count" content={count}> | ||
</Head> | ||
<Head> | ||
<meta name="count" content={count}> | ||
<meta name="will-be-missing"> | ||
<meta name="count" content={count}> | ||
</Head> | ||
<Head> | ||
<meta name="count" content={count}> | ||
</Head> | ||
<button onclick={() => count++}>inc</button> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { test } from '../../test'; | ||
export default test({ | ||
expect_hydration_error: true, | ||
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"> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<!--[--><meta name="description" content="some description" /> | ||
<meta name="foreign" content="alien" /> <meta name="keywords" content="some keywords" /><!--]--> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<script> | ||
let content = "some keywords" | ||
</script> | ||
<svelte:head> | ||
<meta name="description" content="some description" /> | ||
<meta name="keywords" {content} /> | ||
</svelte:head> | ||
<div>Just a dummy page.</div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { test } from '../../test'; | ||
export default test({ | ||
expect_hydration_error: true, | ||
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"> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!--[--><meta name="description" content="some description" /><!--]--> |
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> |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.