-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
SSR dynamic component imports? #9775
Comments
I think the way to do this would probably be to select the proper component and do an |
@benmccann we have load functions in |
This is the sort of advanced use case we want to have a better solution for in a future version of Svelte. For now, you can achieve it by combining a Here's one way you could do it to load only the components that are needed for the blocks you're using: https://stackblitz.com/edit/sveltejs-kit-template-default-nsesrk?file=src%2Flib%2Fblocks%2Fred.svelte,src%2Flib%2Fblocks%2Fgreen.svelte,src%2Flib%2Fblocks%2Fblue.svelte,src%2Froutes%2F%2Bpage.svelte,src%2Froutes%2F%2Bpage.js,src%2Froutes%2F%2Bpage.server.js&terminal=dev |
Ah! Didn't realize both load functions could be used in combination. Thanks for the example, Rich. |
@Rich-Harris Could you share an example with TypeScript? I have trouble with correct types. |
Hi, (a year later!) we've been using a solution along the lines of what @Rich-Harris proposed here for our block-based sveltekit app. (i.e. we're using vite's dynamic import to import svelte files in
Vite will include all the svelte files in this folder in the build, not just the ones that we're importing via the CMS (which are known during server execution). Is there any way to limit this import to a specific set of blocks based on the contents of the blocks array, so we have smaller client-side bundles? |
Describe the problem
We have a SvelteKit app that is attached to a block-based CMS.
In the CMS you define blocks, like text, image, video, etc. Data from our CMS then gets passed to
+page.svelte
, where we loop through the defined blocks and render them to the page:As the number of blocks we support grows, however, the more unused code we will likely ship to the client. A page that contains only
Text
andImage
blocks, for example, would also ship code for theVideo
component.One solution is to dynamically import components...
...but it doesn't seem like SSR works with this approach.
Is there a good way to solve this problem or best practices folks can suggest?
Describe the proposed solution
I know this is footgun territory, but...
...I'm pretty sure this will create more problems than it solves!
Alternatives considered
We've thought about using a preprocessor to generate the body loop, but that requires static analysis to happen before component hydration and is a little obtuse. It also locks our body loop inside of a preprocessor and makes page-level customization a lot harder.
Importance
nice to have
Additional Information
No response
The text was updated successfully, but these errors were encountered: