diff --git a/.changeset/witty-shirts-confess.md b/.changeset/witty-shirts-confess.md new file mode 100644 index 0000000000..371e30bfba --- /dev/null +++ b/.changeset/witty-shirts-confess.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: reset `reset_element` in `render` to prevent runtime error diff --git a/packages/svelte/src/internal/server/dev.js b/packages/svelte/src/internal/server/dev.js index ffa5c31aa0..61c7c6d5e8 100644 --- a/packages/svelte/src/internal/server/dev.js +++ b/packages/svelte/src/internal/server/dev.js @@ -53,7 +53,11 @@ function print_error(payload, parent, child) { } export function reset_elements() { + let old_parent = parent; parent = null; + return () => { + parent = old_parent; + }; } /** diff --git a/packages/svelte/src/internal/server/index.js b/packages/svelte/src/internal/server/index.js index c1cbe32a60..9b5e4ac3e2 100644 --- a/packages/svelte/src/internal/server/index.js +++ b/packages/svelte/src/internal/server/index.js @@ -101,9 +101,11 @@ export function render(component, options = {}) { on_destroy = []; payload.out += BLOCK_OPEN; + let reset_reset_element; + if (DEV) { // prevent parent/child element state being corrupted by a bad render - reset_elements(); + reset_reset_element = reset_elements(); } if (options.context) { @@ -118,6 +120,10 @@ export function render(component, options = {}) { pop(); } + if (reset_reset_element) { + reset_reset_element(); + } + payload.out += BLOCK_CLOSE; for (const cleanup of on_destroy) cleanup(); on_destroy = prev_on_destroy; diff --git a/packages/svelte/tests/runtime-runes/samples/snippet-raw-component-ssr-dev/Child.svelte b/packages/svelte/tests/runtime-runes/samples/snippet-raw-component-ssr-dev/Child.svelte new file mode 100644 index 0000000000..d61a532607 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/snippet-raw-component-ssr-dev/Child.svelte @@ -0,0 +1,7 @@ + + + diff --git a/packages/svelte/tests/runtime-runes/samples/snippet-raw-component-ssr-dev/_config.js b/packages/svelte/tests/runtime-runes/samples/snippet-raw-component-ssr-dev/_config.js new file mode 100644 index 0000000000..8e0c11dbc8 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/snippet-raw-component-ssr-dev/_config.js @@ -0,0 +1,23 @@ +import { flushSync } from 'svelte'; +import { test } from '../../test'; + +export default test({ + props: { + browser: true + }, + + server_props: { + browser: false + }, + compileOptions: { + dev: true + }, + html: `
`, + + test({ target, assert }) { + const button = target.querySelector('button'); + + flushSync(() => button?.click()); + assert.htmlEqual(target.innerHTML, `
`); + } +}); diff --git a/packages/svelte/tests/runtime-runes/samples/snippet-raw-component-ssr-dev/main.svelte b/packages/svelte/tests/runtime-runes/samples/snippet-raw-component-ssr-dev/main.svelte new file mode 100644 index 0000000000..2b1c939e1c --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/snippet-raw-component-ssr-dev/main.svelte @@ -0,0 +1,22 @@ + + +
+ {@render hello()} +