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

未关闭
#8,827 2 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@theRizwan 已经在做这个了。

开始于 2026年10月28日。

  • #8830 来自 @theRizwan —— 未关闭

评估

难度
4/5
预计耗时
1-2 天
新手友好度
54/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
react, typescript
领域
frontend, web-dev

调研方向

Start by reading the lazyRouteComponent implementation and the router plugin’s automatic code splitting path that imports it; the issue does not name file paths or tests. Reproduce with a slowly served route chunk in Firefox or Safari, then trace how a cancelled import triggers the reload during navigation away. Done means the browser follows the requested full navigation, with a regression test covering the cancellation case.

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
TypeScript
星标
15.3k
派生
1.9k
平均合并
1 天 5 小时
30 天内合并 PR
46

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

TanStack/router 的其他 Issue

查看 TanStack/router 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。