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

オープン
#191 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
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分
マージ済み PR(30日)
7

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

TanStack/redact のほかの issue

TanStack/redact の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。