-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Dynamic loading of cdn-hosted Svelte 5 component #13186
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
Comments
Unless you disable SSR, you need two builds: Likewise you would need to change the import to get the correct file accordingly. |
@brunnerh Thank you for pointing that out. The sveltekit app is a SPA and has no SSR. /routes/+page.ts contains Unfortunately, the error persists. |
SSR is controlled by the |
So i've explored this a bit...the error that you see is caused by this function
being undefined. The reason because it's undefined is because this get's initialised inside this function which is called during hydrate or mount. However by bundling the svelte component as a library you are bundling and minifying those functions too...so while in the actual runtime (of the sveltekit application) I'm not sure if there's a way to bundle your component so that it uses the importer svelte runtime (i guess you need to externalize svelte) so either this is something we definitely need to document or we might want to fix something...this is if we actually want the ability to bundle a svelte component as standalone library and give the users the ability to import it like so. Consider that by doing this you are basically freezing in time the compiled output to that of the version you compiled it with. |
Hi @paoloricciuti , Thanks for taking the time! I think you are right in the need of externalizing svelte. I have tried to do that now and I feel like I am a little closer. My goal was to bundle all svelte dependencies together such that I can create an import map which will be used by the dynamically imported widgets. I see the svelte-bundle.js is successfully compiled in the server but when the counter.js file is loaded, it gives an error that "template" is not a function. This indicates to me that the import map is correctly resolving to the svelte-bundle.js but maybe it is not bundled correct because something is missing. Do you have any ideas what I might be missing? Vite config for bundling the component:
Output
Vite config in sveltekit app
app.html in sveltekit
|
Uh i actually never used manual chunks so i'd have to explore that but my first guess would be that the built chunk is minified so |
@paoloricciuti, do you think the solution mentioned here is still possible for svelte 5? It seems that the compiler emits an error when someone tries to import |
Yeah It does error on purpose...accessing internals should not be done because are not stable (we could change those In a minor). I'm not sure that solution is what you are looking for |
Hi, I have a usecase which also is affected by this. In short, I'm writing a CMS that should allow for user defined input components when editing documents, and the best solution I can think to achieve that is retrieving a pre compiled component and using it at runtime, which in my research on if this is even possible lead me to here. |
Also having this issue, additionally if I try to compile my Svelte 5 lib into
Any solutions to this? |
reading through this again i can't tell if this is something that svelte isn't designed for or if this is just an edge case. it just seems like component initialization expects things to be done a certain way and there isn't a clear way to pull it apart. i don't know enough about build options and svelte's internals to have any idea about what needs to be done. |
I was wondering if there is any update to the issue? |
@TheKili Unfortunately not. |
@stephanboersma to make sure i understand (because this thread's a bit hard for me to follow), where are you getting stuck? |
Hi @murl-digital, In my latest attempt, I tried to use vite module federation since this seems easier than a webpack config. However, I end up at the same error as described in the issue. ![]() See this repo: https://github.com/stephanboersma/svelte-cdn The remote exposes App.Svelte and the Host tries load it. See the module federation config in I suspect that this isn't possible to solve using vite or webpack but that it is about the javascript that the svelte compiler ultimately outputs. |
I'm looking for the same thing. My reason being me not wanting to do a full site deploy and just update the component that's on the cdn. |
In my experience this happens when you're trying to mount a precompiled Svelte component. Module federation and single-spa fall under this case. For Svelte 3 and Svelte 4:As these are
You can pass props as you instantiate the component, but it looks like you can't pass slots from the consumer app to the remotely (dynamic) loaded component. For Svelte 5:It's not a solution, but if you enable compatibility with Svelte 4 in your producer app's bundler config you can instantiate the component exactly as you did with Svelte 4
Related: |
Just tried the same using esbuild to build the component, same result obviously. As Svelte 5 has been out for a while it would be nice to get that working somehow, not having to downgrade to Svelte 4. |
I would like to provide a solution that may not relate to the code, but the server. Important TLDR: Disable CDN/Server Cache So this problem occurred to me today out of nowhere. It caused all the svelte components (layout, page, etc.) disappear from the dom after the JS is loaded, only The weird thing is, the problem doesn't always appear. I tried to revert my changes, reinstall dependencies, disable browser cache and switch browser. Nothing works, it went away and came back to stay. Not until I started a new svelte-kit project and the problem is still there. Now I realize the problem is from the server. In my case, I do all my development on my dev server. If you're also using Cloudflare, you can go to your domain and turn on |
Describe the bug
Hi team,
I am not sure whether this is a bug, not supported or me not having proper configuration. I hope that you or someone is able to help me with clarification.
The requirement I am trying to satisfy is externally compiled and hosted Svelte widgets that can be loaded into my SvelteKit app but when I try to dynamically import and load the Svelte widgets, I get the following error.
I can see that the dynamically loaded component taps into the svelte runtime within the sveltekit project, so I got that going for me :)
Looking forward to hear from someone.
Reproduction
1st Repo with Component
src/lib/Counter.svelte
.npm run build
http-server dist --cors
Counter.svelte
vite.config.js
2nd Repo with barebone SvelteKit setup
src/lib/DynamicComponent.svelte
<DynamicComponent componentUrl="http://localhost:8080/counter.js" />
npm run dev
DynamicaComponent.svelte
Both repos have svelte 5 installed.
Logs
No response
System Info
Severity
blocking all usage of svelte
The text was updated successfully, but these errors were encountered: