Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork4.6k
Migrating Remotely Bundled Component setup from Svelte 4 to 5#14298
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Describe the bugI have a server on port 8000 that bundles and hosts svelte components, which are imported into a sveltekit app running on port 5173. This setup used to work fine on svelte 4, but the migration isnt as straight forward as i'd hoped. The server had a esbuild bundler, which had its outputFiles hosted on a http get request.
This was consumed by the client in two steps.
The referenced Component:
The component thats gettings built by the server is currently an empty demo.
this setup worked like a CHARM! given, its a bit much, but once i had figured it out, it never had any hickups. but as you can see, it relied on instantiating the components as now ive updated the build svelte dependency to 5.1.9 which is the same my main sveltekit app uses. replace but nothing works.
any help would be welcome. i am very stuck and have no clue what levers to try next. Reproduction
LogsNo response System InfoSystem: OS: macOS 14.5 CPU: (8) arm64 Apple M1 Pro Memory: 161.06 MB / 16.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 20.17.0 -~/.nvm/versions/node/v20.17.0/bin/node Yarn: 4.5.0 -~/.nvm/versions/node/v20.17.0/bin/yarn npm: 10.8.2 -~/.nvm/versions/node/v20.17.0/bin/npm pnpm: 9.11.0 -~/Library/pnpm/pnpm Browsers: Chrome: 130.0.6723.117 Severityblocking an upgrade |
BetaWas this translation helpful?Give feedback.
All reactions
done. solution is in the repo.
Replies: 9 comments 11 replies
-
Please provide a reproduction repository |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
of the system that worked or the one that doesnt? |
BetaWas this translation helpful?Give feedback.
All reactions
-
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
This the version that doesnt work. I am pretty sure the problem is somewhere on the client with how the component is instantiated and mounted. only once the component tries to attach to the dom, some problem arrises. also the bundling is successfull, and from what i can tell, looks exactly like before. |
BetaWas this translation helpful?Give feedback.
All reactions
-
The problem is that your setup does not deduplicate the Svelte runtime, it exists multiple times: once for your main app, once for your each of your bundles. If you can externalize them somehow that would be the ideal outcome, since it means you also save on bundle size. If that's not possible for some reason, you have to make sure to call the I quickly tried that like this <scriptmodule>importTestfrom'./Test.svelte'import {mountas_mount }from"svelte";exportfunctionmount(target,props) {return_mount(Test, { target, props}) }</script><script>let {...data }=$props();console.log("Hello World from Test.svelte");console.log("Check these amazing props", data);</script><h1class="text-palette-white">My Test Heading</h1> (and adjusting the place where the component is instantiated to use the exported mount method) ...but it still produces two versions of the Svelte runtime inside the Converting this to a discussion because this is not a bug in Svelte. |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
for reference:https://stackoverflow.com/questions/65484019/how-can-i-manually-compile-a-svelte-component-down-to-the-final-javascript-and-c the author adresses the issue of duplicate runtimes headon towards the end:
Is there any way to maintain this approach with svelte5? |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
@dummdidumm I am chewing my teeth out on this. I can easily remove the second svelte runtime from the component bundle and get the component down to:
by adding the
the author of the stackoverflow above cites this issue in his explanation for why the second svelte instance is needed. the problems seems to be that we changed from classes to functions. ie, i can no longer instantiate a svelte component with |
BetaWas this translation helpful?Give feedback.
All reactions
-
i might have it. |
BetaWas this translation helpful?Give feedback.
All reactions
-
done. solution is in the repo. |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1🎉 1🚀 1
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
glad you found a fix! Curious why you did separate out the mount into a different file? Did mounting within the same file via |
BetaWas this translation helpful?Give feedback.
All reactions
-
it worked fine. purely a matter of taste. this pattern fits my overall application architecture way better. |
BetaWas this translation helpful?Give feedback.
All reactions
👍 2
-
i'll leave the repo up. the stackoverflow thread was fairly popular and people will want to know at some point. |
BetaWas this translation helpful?Give feedback.
All reactions
👍 2❤️ 2
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
I too looking for a way to import components lazy remotely (like an external components plugin way), the solution definitely seems to be working (tested the repo, thanks for that example! ❤️ ) - too bad there is no built-in way 😅 Did you maybe figured out how to prevent the additional svelte runtime in those remote component bundles? The current example component bundle is about 24kbs big - sure might not be that huge, but with a couple of remote components this might add up a bit 😬 maybe it would be possible to replace the Alternative: Otherwise I might need to look into building the Svelte as WebComponents and then import/render them instead (but that would have the full svelte runtime in it as well... so probably not an alternative at all xD) |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
-
@dummdidumm something popped up.
and If i add any idea how to handle this? |
BetaWas this translation helpful?Give feedback.
All reactions
-
Did this work before or are you only adding this import now? |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Its a new import. I've not yet used the How do i make sure that SvelteKit looks at code that imported via of course the Plan B would be to just pass the info as but howcould i do it? do you have any idea? |
BetaWas this translation helpful?Give feedback.
All reactions
-
I think if you want to keep it such that you can dynamically import widgets which have no idea about in which environment they're loaded specifically then youhave to pass it at runtime via props or context somehow. |
BetaWas this translation helpful?Give feedback.
All reactions
-
the environment is allways sveltekit. but yes, ill just pass required props. still something to look out for. would be interesting to know what a possible solution looks like. |
BetaWas this translation helpful?Give feedback.
All reactions
-
there isn't really another solution other than passing it through via context or props, because what |
BetaWas this translation helpful?Give feedback.
All reactions
-
I've been wanting to solve this problem for over two years and I finally got to the bottom of it 🥳 There are plenty of gotchas one has to overcome to get this realized. I now agree, I threw up an end-to-end working demo athttps://github.com/mateothegreat/svelte-dynamic-component-engine. Hope it helps everybody!
|
BetaWas this translation helpful?Give feedback.
All reactions
This discussion was converted from issue #14293 on November 14, 2024 12:32.