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 <rich.harris@vercel.com>

* build

* build

* build

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
pull/12540/head
Dominic Gannaway 4 months ago committed by GitHub
parent 2698716bdf
commit 518d9854f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
chore: add warning for invalid render function of createRawSnippet

@ -20,6 +20,10 @@
> Hydration failed because the initial UI does not match what was rendered on the server. The error occurred near %location%
## invalid_raw_snippet_render
> The `render` function passed to `createRawSnippet` should return HTML for a single element
## lifecycle_double_unmount
> Tried to unmount a component that was not mounted

@ -11,6 +11,8 @@ import {
import { hydrate_next, hydrate_node, hydrating } from '../hydration.js';
import { create_fragment_from_html } from '../reconciler.js';
import { assign_nodes } from '../template.js';
import * as w from '../../warnings.js';
import { DEV } from 'esm-env';
/**
* @template {(node: TemplateNode, ...args: any[]) => void} SnippetFn
@ -89,6 +91,9 @@ export function createRawSnippet(fn) {
var html = snippet.render().trim();
var fragment = create_fragment_from_html(html);
element = /** @type {Element} */ (fragment.firstChild);
if (DEV && (element.nextSibling !== null || element.nodeType !== 3)) {
w.invalid_raw_snippet_render();
}
anchor.before(element);
}

@ -60,6 +60,18 @@ export function hydration_mismatch(location) {
}
}
/**
* The `render` function passed to `createRawSnippet` should return HTML for a single element
*/
export function invalid_raw_snippet_render() {
if (DEV) {
console.warn(`%c[svelte] invalid_raw_snippet_render\n%cThe \`render\` function passed to \`createRawSnippet\` should return HTML for a single element`, bold, normal);
} else {
// TODO print a link to the documentation
console.warn("invalid_raw_snippet_render");
}
}
/**
* Tried to unmount a component that was not mounted
*/

@ -0,0 +1,13 @@
import { test } from '../../test';
export default test({
compileOptions: {
dev: true
},
skip_mode: ['hydrate'],
warnings: [
'The `render` function passed to `createRawSnippet` should return HTML for a single element'
]
});

@ -0,0 +1,11 @@
<script>
import { createRawSnippet } from 'svelte';
const snippet = createRawSnippet(() => ({
render: () => `
<!-- --><div>123</div>
`
}));
</script>
{@render snippet()}
Loading…
Cancel
Save