Skip to content

fix: strip internal properties from rest props during SSR #13492

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 4 commits into from
Oct 3, 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/sour-poems-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: strip internal properties from rest props during SSR
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ export function VariableDeclaration(node, context) {
}

if (rune === '$props') {
let has_rest = false;
// remove $bindable() from props declaration
const id = walk(declarator.id, null, {
let id = walk(declarator.id, null, {
RestElement(node, context) {
if (context.path.at(-1) === declarator.id) {
has_rest = true;
}
},
AssignmentPattern(node) {
if (
node.right.type === 'CallExpression' &&
Expand All @@ -39,6 +45,24 @@ export function VariableDeclaration(node, context) {
}
}
});
if (id.type === 'ObjectPattern' && has_rest) {
// If a rest pattern is used within an object pattern, we need to ensure we don't expose $$slots or $$events
id.properties.splice(
id.properties.length - 1,
0,
// @ts-ignore
b.prop('init', b.id('$$slots'), b.id('$$slots')),
b.prop('init', b.id('$$events'), b.id('$$events'))
);
} else if (id.type === 'Identifier') {
// If $props is referenced as an identifier, we need to ensure we don't expose $$slots or $$events as properties
// on the identifier reference
id = b.object_pattern([
b.prop('init', b.id('$$slots'), b.id('$$slots')),
b.prop('init', b.id('$$events'), b.id('$$events')),
b.rest(b.id(id.name))
]);
}
declarations.push(
b.declarator(/** @type {Pattern} */ (context.visit(id)), b.id('$$props'))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
let { time_16 } = $derived({ time_16: count * 16 })
</script>

{count} / {doubled} / {quadrupled} / {time_8} / {time_16}
{count} / {doubled} / {quadrupled} / {time_8} / {time_16}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
<!-- reassign to migrate to state -->
<button onclick={()=> count++}></button>

{count} / {doubled} / {quadrupled} / {time_8} / {time_16}
{count} / {doubled} / {quadrupled} / {time_8} / {time_16}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
let rest = $props();
</script>
<ul>
{#each Object.getOwnPropertyNames(rest) as n}
<li>{n}</li>
{/each}
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
html: '<ul><li>name</li><li>title</li><li>children</li></ul>'
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import Component from "./Component.svelte";
</script>

<Component name="n" title="t">Foo</Component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
let {
children,
...rest
} = $props();
</script>
{@render children?.()}
<ul>
{#each Object.getOwnPropertyNames(rest) as n}
<li>{n}</li>
{/each}
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
html: 'Foo\n<ul><li>name</li><li>title</li></ul>'
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import Component from "./Component.svelte";
</script>

<Component name="n" title="t">Foo</Component>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as $ from "svelte/internal/server";
export default function Props_identifier($$payload, $$props) {
$.push();

let props = $$props;
let { $$slots, $$events, ...props } = $$props;

props.a;
props[a];
Expand Down