Dynamic Lazily imported component doesn't work in preview
See original GitHub issueDescribe the bug
I’m adding components dynamically based on IntersectionObserver ie when the component is in the browser view.
Lazy
component will load when necessary the component:
<Lazy
componentName="./Thing"
let:intersecting
let:component
>
{#if intersecting}
<svelte:component this={component} />
{/if}
</Lazy>
// the interesting part from <Lazy>
...
export let componentPath = "";
const initialize = () => {
observer = new IntersectionObserver(
(entries) => {
entries.forEach(async (_entry) => {
entry = _entry;
if (_entry.isIntersecting) {
component = (await import(`${componentPath}.svelte`)).default;
}
intersecting = _entry.isIntersecting;
});
},
{ root, rootMargin, threshold }
);
};
...
In dev mode all is fine and components loads but not in preview (it loads an HTML instead of the component js).
I’m suspecting the path to the component isn’t correct in preview mode.
Perhaps it’s a matter of specifying a static path that will work in both dev and preview?
Unfortunatly $lib
doesn’t work.
Reproduction
import a component via a function that takes the component path as a parameter such as
export async function loadComp(path) {
return (await import(`${path}.svelte`)).default;
}
and run it in preview mode.
Logs
No response
System Info
@sveltejs/kit 1.0.0-next.370
@sveltejs/vite-plugin-svelte 1.0.0-next.49
vite 2.9.13
node v18.4.0
Severity
blocking prod usage of SvelteKit
Additional Information
No response
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Dynamically Importing Components with React.lazy
With React.lazy, you can dynamically import components at run time to ... But not all of them are needed immediately; some components only ......
Read more >Advanced Features: Dynamic Import - Next.js
Dynamically import JavaScript modules and React Components and split your code into manageable chunks.
Read more >Lazy loading react components with dynamic imports and ...
Lazy loading is a way by which we can load content only when they are needed. This is achieved by code-splitting, where we...
Read more >Lazy loading React components - LogRocket Blog
React.lazy() is a function that enables you to render a dynamic import as a regular component. Dynamic imports are a way of code-splitting, ......
Read more >Code splitting with dynamic imports in Next.js - web.dev
If you have large components in your app, it's a great idea to split them into separate chunks. That way, any large components...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ok thanks @f-elix, I had more than
one dynamic segment
in the path.Thanks for the quick feedback. My problem is that I haven’t found a way to make this work in preview. Any recommendation would be greatly appreciated.