Skip to content

How to handle Error with lazy Route: Failed to fetch dynamically imported module #10333

Closed Answered by brophdawg11
lasseklovstad asked this question in Q&A
Discussion options

You must be logged in to vote

Errors from lazy should 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:

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(".…

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@User6531
Comment options

@lasseklovstad
Comment options

@brophdawg11
Comment options

@User6531
Comment options

@brophdawg11
Comment options

Answer selected by brophdawg11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants