Skip to content

fix: reset reset_element in render to prevent runtime error #13669

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

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/witty-shirts-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: reset `reset_element` in `render` to prevent runtime error
4 changes: 4 additions & 0 deletions packages/svelte/src/internal/server/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ function print_error(payload, parent, child) {
}

export function reset_elements() {
let old_parent = parent;
parent = null;
return () => {
parent = old_parent;
};
}

/**
Expand Down
8 changes: 7 additions & 1 deletion packages/svelte/src/internal/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
let count = $state(0);
</script>

<button onclick={() => (count += 1)}>
clicks: {count}
</button>
Original file line number Diff line number Diff line change
@@ -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: `<div><div><button>clicks: 0</button></div></div>`,

test({ target, assert }) {
const button = target.querySelector('button');

flushSync(() => button?.click());
assert.htmlEqual(target.innerHTML, `<div><div><button>clicks: 1</button></div></div>`);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script>
import { createRawSnippet, hydrate } from 'svelte';
import { render } from 'svelte/server';
import Child from './Child.svelte';

let { browser } = $props();

let count = $state(0);

const hello = createRawSnippet((count) => ({
render: () => `
<div>${browser ? '' : render(Child).body}</div>
`,
setup(target) {
hydrate(Child, { target })
}
}));
</script>

<div>
{@render hello()}
</div>