Skip to content

Commit 3da6cef

Browse files
authored
chore: improve code generation for bind:this in SSR mode (#10524)
* chore: improve code generation for `bind:this` in SSR mode * test --------- Co-authored-by: Rich Harris <[email protected]>
1 parent b5628af commit 3da6cef

File tree

7 files changed

+44
-2
lines changed

7 files changed

+44
-2
lines changed

.changeset/brave-points-sleep.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: improve code generation for `bind:this` in SSR mode

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,9 @@ const common_visitors = {
10601060
parent.type === 'SvelteComponent' ||
10611061
parent.type === 'SvelteSelf'
10621062
) {
1063-
context.state.analysis.uses_component_bindings = true;
1063+
if (node.name !== 'this') {
1064+
context.state.analysis.uses_component_bindings = true;
1065+
}
10641066
break;
10651067
} else if (is_element_node(parent)) {
10661068
break;

packages/svelte/src/compiler/phases/3-transform/server/transform-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ function serialize_inline_component(node, component_name, context) {
853853

854854
const value = serialize_attribute_value(attribute.value, context, false, true);
855855
push_prop(b.prop('init', b.key(attribute.name), value));
856-
} else if (attribute.type === 'BindDirective') {
856+
} else if (attribute.type === 'BindDirective' && attribute.name !== 'this') {
857857
// TODO this needs to turn the whole thing into a while loop because the binding could be mutated eagerly in the child
858858
push_prop(
859859
b.get(attribute.name, [
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from '../../test';
2+
3+
export default test({});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// index.svelte (Svelte VERSION)
2+
// Note: compiler output will change before 5.0 is released!
3+
import "svelte/internal/disclose-version";
4+
import * as $ from "svelte/internal";
5+
6+
export default function Bind_this($$anchor, $$props) {
7+
$.push($$props, false);
8+
$.init();
9+
10+
/* Init */
11+
var fragment = $.comment($$anchor);
12+
var node = $.child_frag(fragment);
13+
14+
$.bind_this(Foo(node, {}), ($$value) => foo = $$value, foo);
15+
$.close_frag($$anchor, fragment);
16+
$.pop();
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// index.svelte (Svelte VERSION)
2+
// Note: compiler output will change before 5.0 is released!
3+
import * as $ from "svelte/internal/server";
4+
5+
export default function Bind_this($$payload, $$props) {
6+
$.push(false);
7+
8+
const anchor = $.create_anchor($$payload);
9+
10+
$$payload.out += `${anchor}`;
11+
Foo($$payload, {});
12+
$$payload.out += `${anchor}`;
13+
$.pop();
14+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<Foo bind:this={foo} />

0 commit comments

Comments
 (0)