Skip to content

lazyRouteComponent reloads over a navigation when Firefox/Safari cancel a pending route chunk on page leave #8827

Open

Description

@alexskatell

Which project does this relate to?

Router

Describe the bug

When a code-split route's chunk is still loading and the person leaves the page (clicks a link to another document, types an address, reloads), Firefox and Safari cancel the pending dynamic import. They reject it with the same messages as a missing chunk: "error loading dynamically imported module" in Firefox, "Importing a module script failed" in Safari.

lazyRouteComponent takes that as a stale deploy. It sets the tanstack_router_reload:<message> key and calls window.location.reload(). That reload of the old address replaces the navigation the person just started, and they land back on the page they were leaving.

There's no option to turn this off. The reload is hardcoded, the router plugin's automatic code splitting imports lazyRouteComponent by name, and the branch runs in the lazy component's render, before any errorComponent or onError sees the error.

Related, but a different trigger: #8377 and #8331.

Your Example Website or App

Any app with automatic code splitting. A small repro follows.

Steps to Reproduce the Bug or Issue

  1. Build an app with automatic code splitting. Serve its chunks slowly, for example with a proxy that delays .js responses by a few seconds.
  2. Open the app in Firefox or Safari.
  3. Click a link to a route whose chunk hasn't loaded yet.
  4. While that chunk is still loading, start a full navigation away: type another URL (same origin or not) and press Enter, or follow a plain <a href> to another document.

Expected behavior

The browser goes to the address the person asked for.

Actual behavior

The import is cancelled with a missing module's message, lazyRouteComponent calls window.location.reload(), and the page reloads at the old address instead of following the navigation. In end-to-end tests this shows up as Playwright's page.goto in Firefox ending on the previous URL.

Platform

  • Router version: 1.170.46 (the code is the same in 1.170.45)
  • Browsers: Firefox and Safari (WebKit). Chromium doesn't cancel the import with this message.

Additional context

We work around it with a local patch. A beforeunload listener sets a module-level leaving flag. While the flag is set, Lazy suspends instead of reloading, since the page is going away anyway:

let leaving = false
let staying = []
if (typeof window !== 'undefined') {
  const stay = () => {
    leaving = false
    const waiting = staying
    staying = []
    for (const resolve of waiting) resolve()
  }
  window.addEventListener('beforeunload', () => { leaving = true })
  // Some browsers fire beforeunload for mailto:/tel: links without leaving, so the
  // first sign the page is still in use ends the suspension and the route reloads once as before.
  window.addEventListener('pageshow', stay)
  window.addEventListener('pointerdown', stay, true)
  window.addEventListener('keydown', stay, true)
}

// in Lazy, before the sessionStorage guard:
if (leaving) throw new Promise((resolve) => staying.push(resolve))

We'd be glad to drop the patch for an upstream fix: either this behavior, or an option to opt out of the automatic reload or handle it ourselves.

Activity

  1. gsisso commented on Nov 11, 2026

    @gsisso

    Another trigger for the same hardcoded reload: being offline.

    In a PWA whose service worker leaves some route chunks out of its precache, an in-app navigation to one of those routes while offline fails the import with the same "Failed to fetch dynamically imported module" message. lazyRouteComponent reloads, which can't fetch anything. The service worker then serves its offline fallback, or, before a worker is installed, the browser shows its own network error page and the app is gone until a manual refresh. A second offline click in the same tab doesn't reload (the key is set), and the error reaches the route's error component, which can show an offline view and recover on reconnect.

    Seen on @tanstack/react-router 1.170.46, Chromium.

    An option to turn the reload off, or to veto it (for example skipping it while navigator.onLine is false, or a callback that decides), would cover both this and the cancelled-import case above.

  2. ntilwalli commented on Nov 12, 2026

    @ntilwalli

    +1 for an opt-out. We hit the offline case @gsisso describes. Our app watches connectivity, and if someone navigates while the connection is down, the chunk import fails and the reload lands them on the browser's offline page. Their in-progress work goes with it.

    #8830 handles the page-unload case, but every other cause still reloads unconditionally. And because autoCodeSplitting imports lazyRouteComponent by name, an app can't swap in its own wrapper. We're carrying a small patch for now.

    Would a router option along these lines be OK?

    const router = createRouter({
      routeTree,
      // Return false to skip the reload; the error goes to the route's errorComponent instead.
      shouldReloadOnModuleNotFound: (error) => navigator.onLine,
    })

    The default would keep the current behaviour. If the shape works for you, I'm happy to send a PR covering React, Solid and Vue.

    (Drafted with AI assistance.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions