Skip to content

chore: improve code generation for bind:this in SSR mode #10524

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 2 commits into from
Feb 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/brave-points-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

chore: improve code generation for `bind:this` in SSR mode
4 changes: 3 additions & 1 deletion packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,9 @@ const common_visitors = {
parent.type === 'SvelteComponent' ||
parent.type === 'SvelteSelf'
) {
context.state.analysis.uses_component_bindings = true;
if (node.name !== 'this') {
context.state.analysis.uses_component_bindings = true;
}
break;
} else if (is_element_node(parent)) {
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ function serialize_inline_component(node, component_name, context) {

const value = serialize_attribute_value(attribute.value, context, false, true);
push_prop(b.prop('init', b.key(attribute.name), value));
} else if (attribute.type === 'BindDirective') {
} else if (attribute.type === 'BindDirective' && attribute.name !== 'this') {
// TODO this needs to turn the whole thing into a while loop because the binding could be mutated eagerly in the child
push_prop(
b.get(attribute.name, [
Expand Down
3 changes: 3 additions & 0 deletions packages/svelte/tests/snapshot/samples/bind-this/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { test } from '../../test';

export default test({});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// index.svelte (Svelte VERSION)
// Note: compiler output will change before 5.0 is released!
import "svelte/internal/disclose-version";
import * as $ from "svelte/internal";

export default function Bind_this($$anchor, $$props) {
$.push($$props, false);
$.init();

/* Init */
var fragment = $.comment($$anchor);
var node = $.child_frag(fragment);

$.bind_this(Foo(node, {}), ($$value) => foo = $$value, foo);
$.close_frag($$anchor, fragment);
$.pop();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// index.svelte (Svelte VERSION)
// Note: compiler output will change before 5.0 is released!
import * as $ from "svelte/internal/server";

export default function Bind_this($$payload, $$props) {
$.push(false);

const anchor = $.create_anchor($$payload);

$$payload.out += `${anchor}`;
Foo($$payload, {});
$$payload.out += `${anchor}`;
$.pop();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<Foo bind:this={foo} />