-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Astro components don't work if passed as children to hydrated svelte components #9037
Comments
I'm not sure if this is an Astro issue. The problem is that the script is hoisted as a However, the button script will capture the non-hydrated button first, and then Svelte hydrates and replaces the button instance, causing the reference to be lose. In this case, I think you need to anticipate that buttons could be loaded later by modifying your script, using |
I believe it is an issue mainly because it is not specified in the 2 relevant urls of the documentation:
But the contrary, it encourages you to use this defective pattern of passing astro components inside framework components with a Also, having the developer to take into account this hydration limitation of not passing astro components with client side script to a framework component Luckily my original svelte component was simple enough to be rewritten into an astro component with imperative javascript and avoid this all together, but that made me lost svelte's simplicity and resulted in more lines of code. At the very least the documentation could be updated to disencourage about this pattern and/or provide guidance on dealing with this limitation with |
Yeah I think we can clarify in our docs about the limitations of the examples. They currently don't work well when the HTML elements they're querying appears later. |
Transferring this issue to the docs so they can document those limitations. I'm not sure I 100% understand the situation, if you could clarify @bluwy so that docs team can document accurately that'd be amazing! |
The docs that needs updating is at https://docs.astro.build/en/guides/client-side-scripts. The page could have a section (under To query the elements after hydration or dynamically added to the page, you'd need to use |
Thanks for this report! In docs' hands now! 🫡 |
So if I understand correctly the specific interaction here is:
I’ll admit it feels like a bit of an edge case and the correct fix is probably one of:
Using Bit tricky to document 🤔 I guess technically the advice would be something like:
|
Yeah exactly. I agree that custom elements is probably the most correct way and maybe we can recommend that directly instead. The suggested advice lgtm 👍 I'm not quite sure where we'll put it now though since it's a short sentence and might not worth its own section, but simply a info box? Maybe at https://docs.astro.build/en/guides/client-side-scripts/#handle-onclick-and-other-events but it also has an existing info box. |
I think I'd suggest adding a new subheading to the final section "Common Script Patterns", something like:
Would happily accept a PR for this! |
Astro Info
If this issue only occurs in one browser, which browser is a problem?
No response
Describe the Bug
If you pass an astro component to a svelte component slot and if you hydrate that svelte component the astro component stops working after 1 click or may not work at all.
svelte-astro-error.mp4
What's the expected result?
The astro component should work when passed as a slot to a svelte component
Link to Minimal Reproducible Example
https://stackblitz.com/edit/github-iqe45d?file=src%2Fpages%2Findex.astro
Participation
The text was updated successfully, but these errors were encountered: