Catching dynamic import errors
See original GitHub issueFeature 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:
- Created 4 years ago
- Comments:8 (3 by maintainers)
Top 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 >
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

Hi, one way you could currently handle this is adding a
.catchon theimporte.g.Yes, I’m having the same issue.