Skip to content
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

fix: provide more hydration mismatch coverage #12755

Merged
merged 15 commits into from
Aug 10, 2024
Merged
5 changes: 5 additions & 0 deletions .changeset/stupid-rivers-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: provide more hydration mismatch coverage
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,8 @@ export function RegularElement(node, context) {
// set the value of `hydrate_node` to `node.content`
if (node.name === 'template') {
needs_reset = true;

child_state.init.push(b.stmt(b.call('$.hydrate_template', arg)));
arg = b.member(arg, b.id('content'));
child_state.init.push(b.stmt(b.call('$.reset', arg)));
}

process_children(trimmed, () => b.call('$.child', arg), true, {
Expand Down
27 changes: 22 additions & 5 deletions packages/svelte/src/internal/client/dom/hydration.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,38 @@ export let hydrate_node;

/** @param {TemplateNode} node */
export function set_hydrate_node(node) {
if (node === null) {
w.hydration_mismatch();
throw HYDRATION_ERROR;
}

return (hydrate_node = node);
}

export function hydrate_next() {
if (hydrate_node === null) {
return set_hydrate_node(/** @type {TemplateNode} */ (hydrate_node.nextSibling));
}

/** @param {TemplateNode} node */
export function reset(node) {
if (!hydrating) return;

// If the node has remaining siblings, something has gone wrong
if (hydrate_node.nextSibling !== null) {
w.hydration_mismatch();
throw HYDRATION_ERROR;
}
return (hydrate_node = /** @type {TemplateNode} */ (hydrate_node.nextSibling));

hydrate_node = node;
}

/** @param {TemplateNode} node */
export function reset(node) {
/**
* @param {HTMLTemplateElement} template
*/
export function hydrate_template(template) {
if (hydrating) {
hydrate_node = node;
// @ts-expect-error TemplateNode doesn't include DocumentFragment, but it's actually fine
hydrate_node = template.content;
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export {
bind_focused
} from './dom/elements/bindings/universal.js';
export { bind_window_scroll, bind_window_size } from './dom/elements/bindings/window.js';
export { next, reset } from './dom/hydration.js';
export { hydrate_template, next, reset } from './dom/hydration.js';
export {
once,
preventDefault,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
expect_hydration_error: true
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>call +636-555-3226 now</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!--[--><h1>call <a href="tel:+636-555-3226">+636-555-3226</a> now</h1><!--]-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
const message = `call +636-555-3226 now`;
</script>

<h1>{message}</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!-- unrelated comment -->
<!--[--><!--[-->hello<!--]--><!--]-->
13 changes: 7 additions & 6 deletions packages/svelte/tests/hydration/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,16 @@ const { test, run } = suite<HydrationTest>(async (config, cwd) => {
throw new Error(`Unexpected errors: ${errors.join('\n')}`);
}

if (!override) {
const expected = read(`${cwd}/_expected.html`) ?? rendered.html;
flushSync();
assert.equal(target.innerHTML.trim(), expected.trim());
}
flushSync();

const normalize = (string: string) => string.trim().replace(/\r\n/g, '\n');

const expected = read(`${cwd}/_expected.html`) ?? rendered.html;
assert.equal(normalize(target.innerHTML), normalize(expected));

if (rendered.head) {
const expected = read(`${cwd}/_expected_head.html`) ?? rendered.head;
assert.equal(head.innerHTML.trim(), expected.trim());
assert.equal(normalize(head.innerHTML), normalize(expected));
}

if (config.snapshot) {
Expand Down
4 changes: 3 additions & 1 deletion playgrounds/demo/ssr-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ polka()

const html = transformed_template
.replace(`<!--ssr-head-->`, head)
.replace(`<!--ssr-body-->`, body);
.replace(`<!--ssr-body-->`, body)
// check that Safari doesn't break hydration
.replaceAll('+636-555-3226', '<a href="tel:+636-555-3226">+636-555-3226</a>');

res.writeHead(200, { 'Content-Type': 'text/html' }).end(html);
})
Expand Down
Loading