Components that suspend inside a transition load one after another instead of in parallel

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

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

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

評価

難易度
4/5
見積もり時間
1〜2日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
typescript
領域
frontend

調査の方向性

Start by reading the transition runner in dom/transitions.ts and tracing how handleSuspended handles a promise during a transition render. Use the provided Suspense reproduction to observe when each load starts. Done means sibling loads start in the same pass while the transition still waits for the promises and commits the same result. The issue links a fix commit, so check its status before starting.

索引モデルが issue の本文から書いたものです。

説明

On our website, each component loads its translations lazily: the first render for a locale starts a dynamic import() and throws the promise. Switching locale is wrapped in startTransition.

With React, the switch takes a single round trip. With redact enabled, the old page stays on screen for several seconds, and the network tab shows the chunks fetched strictly one after another (14 chunks, around 5s in production).

Repro
const cache = new Map<string, Promise<string>>()
const load = (key: string) => {
  if (!cache.has(key)) {
    console.log('start', key)
    cache.set(key, fetchText(key))
  }
  return cache.get(key)!
}

const Text = ({ name, locale }) => <span>{use(load(`${name}-${locale}`))}</span>

const Page = () => {
  const [locale, setLocale] = useState('en')
  return (
    <>
      <button onClick={() => startTransition(() => setLocale('fr'))}>fr</button>
      <Text name="a" locale={locale} />
      <Text name="b" locale={locale} />
      <Text name="c" locale={locale} />
    </>
  )
}

// click "fr"
// React:  start a-fr, start b-fr, start c-fr
// redact: start a-fr ... (a-fr resolves) ... start b-fr ... (b-fr resolves) ... start c-fr

Cause

During a transition render, handleSuspended throws the promise straight up to the transition runner (t in dom/transitions.ts), which rewinds and retries once that one promise settles. The siblings after the suspended component never render in that pass, so their own loads only start on the next retry. React keeps rendering the siblings of a suspended component (prewarming), so every load starts in the same pass.

Fix

When a held transition suspends on a new promise, render the same fibers once more without throwing (the suspended component renders nothing and its siblings keep going), then rewind that pass. It only starts the work, what the transition waits for and what gets committed don't change.

Fix: https://github.com/aymericzip/redact/commit/18464d7
Related issue: https://github.com/TanStack/redact/issues/137

主要言語
TypeScript
スター
581
フォーク
28
平均マージ
13時間 48分
マージ済み PR(30日)
45

環境構築

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

はじめの一歩

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

TanStack/redact のほかの issue

TanStack/redact の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

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

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