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.

Catching dynamic import errors

See original GitHub issue

Feature request

Is your feature request related to a problem? Please describe.

It would be handy to be able to catch any errors that may arise for dynamic imports, like a component not found, so we could handle the error and show a custom message for example. I haven’t seen a documented way of doing such a thing so that’s why I’m requesting it.

Describe the solution you’d like

An additional option to which a function can be passed to handle errors. Maybe that you can also return a element that will be shown then?

An rough example of what I had in mind:

import dynamic from 'next/dynamic'

const DynamicComponent = dynamic(
  () => import('../components/hello'),
  {
    loading     : () => <p>...</p>,
    errorHandler: (err) => {
      console.error('An issue has occured while loading a component', err)

      if (err.code === 'MODULE_NOT_FOUND') {
        return <p>Unknown item</p>
      }

      return <p>Unknown error..</p>
    }
  }
)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
ijjkcommented, Jun 1, 2019

Hi, one way you could currently handle this is adding a .catch on the import e.g.

import dynamic from 'next/dynamic'

const Hello = dynamic(
  () => import('../components/hello').catch(err => {
    return () => <p>oops... this failed to load</p>
  }
}
1reaction
malithmcrcommented, Mar 4, 2022

Yes, I’m having the same issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Catching dynamic import errors · Issue #7480 · vercel/next.js
Hi, failing to resolve the import during build is different than failing to load it during runtime. During runtime .catch() can be used...
Read more >
Dynamic imports - Unable to catch error due to compile error
I'm not sure if I'm misunderstanding the concept of import(), but I expect the import not to take place if the module doesn't...
Read more >
How to Handle Dynamic Import Errors in Vuejs
Today, I'm going to talk about dynamic import error handling. ... In Vuejs, we can set import methods return value to component key...
Read more >
Dynamic Import, Code Splitting, Lazy Loading, and Error ...
This article is a detailed guide on how to use dynamic import, which enables code splitting and lazy loading. It also describes how...
Read more >
Dynamic imports - The Modern JavaScript Tutorial
Export and import statements that we covered in previous chapters are called “static”. The syntax is very simple and strict.
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