You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I tried to edit a child component that uses getContext where the context key is imported from the parent svelte component's context="module". When I edit the child component and save, HMR is triggered and I get an error in the console. I have to refresh the page to be able to keep working
It's just getContext returning undefined. But I do setContext in the parent and it works until the HMR happens.
Edit the child component that's currently being displayed and save
Watch the console for errors
Logs
One.svelte?t=1685287937140:91 Uncaught (in promise) TypeError: Cannot destructure property 'func' of 'getContext(...)' as it is undefined.
at instance (One.svelte?t=1685287937140:91:10)
at init (chunk-UVEMIUTC.js?v=45af3e07:1890:23)
at new One (One.svelte?t=1685287937140:105:3)
at createProxiedComponent (svelte-hooks.js?v=45af3e07:341:9)
at new ProxyComponent (proxy.js?v=45af3e07:242:7)
at new Proxy<One> (proxy.js?v=45af3e07:349:11)
at Array.create_if_block (Child.svelte?t=1685287888134:114:8)
at Object.update [as p] (Child.svelte?t=1685287888134:198:98)
at update (chunk-UVEMIUTC.js?v=45af3e07:1140:32)
at flush (chunk-UVEMIUTC.js?v=45af3e07:1106:9)
You're pushing the HMR engine to its limits with this kind of cyclical dependencies.
I am not sure this can be fixed in a reasonable way on the Svelte side. On Vite side, it is debatable whether this could / should be fixed, or if it is like it is. The only thing I'm sure for now is that we're in a pretty gray area 😅
Currently, you can work around this limitation either by explicitly breaking the import loop, by extracting the context=module part to its own module:
Parent.js
exportconstCONTEXT={}
This is probably the cleanest and more solid approach.
Alternatively, you can make your precise use case work with a sprinkle of HMR incantations:
Understood but I wasn't aware it was an HMR limitation since the repro was so minimal. Thanks for the example by the way, I think it would be worth putting in the docs for getContext/setContext
After further investigation, it turns out it is a known bug in Vite, and it is in the process of being fixed in this PR. So I think we just need to wait it out.
Describe the bug
I tried to edit a child component that uses
getContext
where the context key is imported from the parent svelte component'scontext="module"
. When I edit the child component and save, HMR is triggered and I get an error in the console. I have to refresh the page to be able to keep workingIt's just
getContext
returningundefined
. But I dosetContext
in the parent and it works until the HMR happens.Reproduction
https://github.com/space-nuko/svelte-context-hmr-issue
Logs
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: