-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Inconsistent behavior of let
directive in combination with snippets
#12087
Comments
I think snippets and |
If that's the case I'll refrain from trying to use the Regardless, would it be good to make the behavior consistent between the server and client, to prevent confusion? Interoperability between v5 and older version could be favorable, so perhaps it would be good if it is still possible to use the |
I agree we should probably have some runtime warn about this. |
Perhaps the easiest fix would be this: export default function App($$anchor) {
MyComponent($$anchor, {
- children: ($$anchor, $$slotProps) => {
+ children: ($$anchor, $$args) => {
+ if ($$args) e.invalid_default_snippet_argument(); // this would need to be created
- const myProp = $.derived_safe_equal(() => $$slotProps.myProp);
var p = root_1();
var text = $.child(p);
$.reset(p);
- $.template_effect(() => $.set_text(text, $.get(myProp) ?? '...'));
+ $.template_effect(() => $.set_text(text, myProp ?? '...'));
$.append($$anchor, p);
},
$$slots: { default: true },
$$legacy: true
});
} |
It seems https://github.com/sveltejs/svelte/pull/12400/files#diff-e6527b55735d1694bca3cd7e3eef522b936ddd63022f038c67759321200dc39bR21 is triggering the error in certain cases, where I guess it shouldn't: This was the simplest reproduction I could think of. My suspicion is that |
Describe the bug
Snippets are my favourite new feature in Svelte 5, and I trying to use the let directive in combination with snippets to see if it was possible, and ran into some odd behavior. Consider the following scenario (don't mind the console.log in the template, it's just to portray the issue):
During SSR the server logs prints
"Hello"
, as I was hoping would happen. In the browser however, it will logundefined
. This made me wonder if this is supposed to work at all. Either way I assume it should either work for both server and client, or neither.I've included a REPL to demonstrate the client-side behavior in this issue, let me know if you'd also like a reproduction for the server-side behavior.
Did a little poking around, and looking at the compiler output in the REPL it seems to be caused by the generated JS, the generated snippet when using the
let
directive is as follows:When updating the REPL to use a snippet instead the following JS is generated:
In the case of the
let
directive, the prop is referenced as$$slotProps.myProp
, in the case of a snippet the prop is referenced as$$args0?.().myProp
. So if$$slotProps
is actually a function, like$$args0
, then$$slotsProps.myProp
would always evaluate toundefined
.I haven't been able to dive deeper into this yet, but I might find some time to dig a little deeper, and create a PR for whatever the desired functionality is.
Reproduction
https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE42PQUvFMBCE_8qyCG2h9N1jfShevAjvbj1Iu9XAJhs2eQ9KyH-XWihFLx5nmG-YyThbpojmLaP_cIQGn0LAFtMSVhFvxImwxShXHVenj6PakM6DB7AuiCZ4XZ7FBfHkE8wqDqrudPC6raS6H3x_2unB90eOKRm3XFTCT3MexUdh6lg-681vysofmDO26GSys6UJTdIrlXZ_ccj99w1Tggzjl-VJyUOBB7gLKiHWze_p-VHJT6R7us6wrTRQvRCzVFCa8nfge_kGCpyuQ3IBAAA=
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: