How to handle Error with lazy Route: Failed to fetch dynamically imported module #10333
-
| 
         Hi! I have started using the lazy Route functionality and reduced my build index file by 50% 😄 and thats nice. But I have started seeing some errors that lazy loaded chunk files are not found. This often happens when the app is redeployed as described here. I would love it if anyone have any recomandations of how to handle this correctly. I have tried this, but this creates a typeerror, but i guess would potentially work?: const router = createBrowserRouter([
  {
    path: "/",
    lazy: () => import("./routes/Demo/Demo").catch(() => window.location.reload()),
  },
]); | 
  
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
| 
         Errors from  const router = createBrowserRouter([
  {
    path: "/",
    ErrorBoundary() {
      return (
        <>
          <h1>Uh oh!</h1>
          <p>Something went wrong!<p>
          <button onClick={() => window.location.reload()}>Click here to reload the page</button>
        </>
      );
    },   
    lazy: () => import("./routes/Demo/Demo").catch(() => window.location.reload()),
  },
]);The type error is likely just that lazy expects to resolve with a route module, so this might fix it: lazy: () => import("./routes/Demo/Demo").catch(() => {
  window.location.reload();
  return {};
}), | 
  
Beta Was this translation helpful? Give feedback.
Errors from
lazyshould bubble to route error boundaries, so you can either force a hard refresh like you're doing above or you could provide a UI to the user with a button to reload:The type error is likely just that lazy expects to resolve with a route module, so this might fix it: