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

Ouverte
#178 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
1-2 jours
Accessibilité débutants
35/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
typescript
Domaine
frontend

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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

Langage dominant
TypeScript
Étoiles
581
Forks
28
Merge moyen
13 h 48 min
PR mergées (30 j)
45

Préparer son environnement

Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de TanStack/redact

Toutes les issues de TanStack/redact

Issues similaires

Plus d'issues TypeScript

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.