A context reader inside a suspended boundary keeps the old value after its provider commits (regression from #133)

未关闭
#191 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
react, typescript
领域
frontend

调研方向

Start with tests/suspended-context-reader-retry.test.tsx, which exercises the locale transition and suspense-boundary retry described in the issue. Run that test, then inspect the context-propagation and suspense retry code affected by #133; the relevant implementation file is not identified in the issue. The work appears already implemented in commit 8a04ab7, so confirm that commit and run size:check on CI rather than starting a new patch.

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

描述

On our website, the locale switcher on documentation pages updates the locale in a transition (setLocale inside startTransition, read through context), then TanStack Router commits the new route through its stores. With 0.3.1 the page often kept the previous locale until a second click. That one is #130, fixed on main by #133 but not released yet.

To check the fix, I reproduced the same flow against main. The route's store update now goes through, but a component that reads the locale through context inside the page's <Suspense> keeps the old value once the boundary resolves, and nothing retries it. No error, nothing in the console. It works with React, and it works with 0.3.1.

Repro
const Locale = createContext('en')
let setLocale!: (locale: string) => void
function Provider({ children }: { children: ReactNode }) {
  const [locale, set] = useState('en')
  setLocale = set
  return <Locale.Provider value={locale}>{children}</Locale.Provider>
}

// load(key) returns one pending promise per key (like an i18n cache)
function Body() {
  const locale = useContext(Locale)
  return <span>{locale === 'en' ? 'body-en' : use(load(`body-${locale}`))}</span>
}
function Page() {
  const locale = useSyncExternalStore(route.subscribe, route.get) // the router's store
  return <p>{locale === 'en' ? 'page-en' : use(load(`page-${locale}`))}</p>
}
const Outlet = memo(() => <Page />) // the router's Match / Outlet are memo

root.render(
  <Provider>
    <Suspense fallback={<i>loading</i>}>
      <Body />
      <Outlet />
    </Suspense>
  </Provider>
)

startTransition(() => setLocale('fr')) // held: Body suspends
route.set('fr')                        // Page suspends: the boundary shows its fallback
// then resolve both loads

// React 19.3.1 / redact 0.3.1: body-fr page-fr
// redact main:                 body-en page-fr

Cause

The route update suspends outside the transition, so the boundary shows its fallback. In the same flush the held transition renders again: the provider changes, Body reads fr and suspends, but the boundary already has a fallback, so renderSuspense rewinds the attempt instead of throwing and the transition commits. The rewind gives Body back its remembered context (en). When the boundary retries, Body's JSX is unchanged, so the stable-JSX bailout added in #133 skips it: ownContextChanged only reports a change while a provider is rendering (changingProviders), and the provider has already committed. The boundary resolves with body-en and nothing schedules Body again. d9ac1c3, right before #133, renders body-fr.

Fix

After that rewind, flag the readers under the boundary whose remembered context differs from the changing provider as pending (fiber.g), so the retry renders them. React keeps the propagated context work on the fiber in the same situation. Regression test included: tests/suspended-context-reader-retry.test.tsx.

The fix costs about 30 B gzip; client total (default Vite) ends 5 B under budget. I measured with Node 22 because I don't have 24.22 locally, so size:check still needs a run on CI.

Fix: https://github.com/aymericzip/redact/commit/8a04ab7
Related issues: #130, #133

Since #133 also fixes #130, which we hit in production on 0.3.1, a patch release with both would help.

主要语言
TypeScript
星标
640
派生
32
平均合并
1 小时 15 分钟
30 天内合并 PR
7

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

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

TanStack/redact 的其他 Issue

查看 TanStack/redact 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

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