question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamic Lazily imported component doesn't work in preview

See original GitHub issue

Describe 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:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
PlopTheRealcommented, Jul 13, 2022

Ok thanks @f-elix, I had more than one dynamic segment in the path.

0reactions
PlopTheRealcommented, Jul 13, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found