Skip to content

Commit 518d985

Browse files
trueadmRich-Harris
andauthored
chore: add warning for invalid render function of createRawSnippet (#12535)
* chore: add warning for invalid render function of createRawSnippet * add test * warnings * Update packages/svelte/messages/client-warnings/warnings.md Co-authored-by: Rich Harris <[email protected]> * build * build * build --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 2698716 commit 518d985

File tree

6 files changed

+50
-0
lines changed

6 files changed

+50
-0
lines changed

.changeset/nervous-dolphins-allow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
chore: add warning for invalid render function of createRawSnippet

packages/svelte/messages/client-warnings/warnings.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
2121
> Hydration failed because the initial UI does not match what was rendered on the server. The error occurred near %location%
2222
23+
## invalid_raw_snippet_render
24+
25+
> The `render` function passed to `createRawSnippet` should return HTML for a single element
26+
2327
## lifecycle_double_unmount
2428

2529
> Tried to unmount a component that was not mounted

packages/svelte/src/internal/client/dom/blocks/snippet.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
import { hydrate_next, hydrate_node, hydrating } from '../hydration.js';
1212
import { create_fragment_from_html } from '../reconciler.js';
1313
import { assign_nodes } from '../template.js';
14+
import * as w from '../../warnings.js';
15+
import { DEV } from 'esm-env';
1416

1517
/**
1618
* @template {(node: TemplateNode, ...args: any[]) => void} SnippetFn
@@ -89,6 +91,9 @@ export function createRawSnippet(fn) {
8991
var html = snippet.render().trim();
9092
var fragment = create_fragment_from_html(html);
9193
element = /** @type {Element} */ (fragment.firstChild);
94+
if (DEV && (element.nextSibling !== null || element.nodeType !== 3)) {
95+
w.invalid_raw_snippet_render();
96+
}
9297
anchor.before(element);
9398
}
9499

packages/svelte/src/internal/client/warnings.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ export function hydration_mismatch(location) {
6060
}
6161
}
6262

63+
/**
64+
* The `render` function passed to `createRawSnippet` should return HTML for a single element
65+
*/
66+
export function invalid_raw_snippet_render() {
67+
if (DEV) {
68+
console.warn(`%c[svelte] invalid_raw_snippet_render\n%cThe \`render\` function passed to \`createRawSnippet\` should return HTML for a single element`, bold, normal);
69+
} else {
70+
// TODO print a link to the documentation
71+
console.warn("invalid_raw_snippet_render");
72+
}
73+
}
74+
6375
/**
6476
* Tried to unmount a component that was not mounted
6577
*/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
compileOptions: {
5+
dev: true
6+
},
7+
8+
skip_mode: ['hydrate'],
9+
10+
warnings: [
11+
'The `render` function passed to `createRawSnippet` should return HTML for a single element'
12+
]
13+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
import { createRawSnippet } from 'svelte';
3+
4+
const snippet = createRawSnippet(() => ({
5+
render: () => `
6+
<!-- --><div>123</div>
7+
`
8+
}));
9+
</script>
10+
11+
{@render snippet()}

0 commit comments

Comments
 (0)