Repository navigation
fix(🌐): build the React reconciler on the first render, not at import - #4546
Open
wcandillon wants to merge 1 commit into
Open
wcandillon wants to merge 1 commit into
wcandillon wants to merge 1 commit into
Conversation
Building a reconciler hooks it into the React instance for the lifetime of the process: createReconciler() wraps ReactSharedInternals.S around the hook already there. sksg/Reconciler.ts built one when the module was evaluated, so a server that evaluates the bundle once per request against a single React instance, as the Expo dev server does for web, kept one reconciler per request, and the whole bundle behind it. The reconciler is now built by the first render, along with its DevTools registration. The React root of a SkiaSGRoot is created by the first render as well, not by the constructor: a Canvas constructs its root while it renders, which a server does too, and renders into it from a layout effect, which a server never runs. Unmounting a root that never rendered resolves without a commit.
wcandillon
added a commit
that referenced
this pull request
Nov 26, 2026
… (2.x backport of #4546) Backport of #4546 to 2.x. Building a reconciler hooks it into the React instance for the lifetime of the process: createReconciler() wraps ReactSharedInternals.S around the hook already there. sksg/Reconciler.ts built one when the module was evaluated, so a server that evaluates the bundle once per request against a single React instance, as the Expo dev server does for web, kept one reconciler per request, and the whole bundle behind it. The reconciler is now built by the first render, along with its DevTools registration. The React root of a SkiaSGRoot is created by the first render as well, not by the constructor: a Canvas constructs its root while it renders, which a server does too, and renders into it from a layout effect, which a server never runs. Unmounting a root that never rendered resolves without a commit.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #4544
sksg/Reconciler.tsbuilt its React reconciler when the module was evaluated. Building a reconciler hooks it into the React instance for good:createReconciler()wrapsReactSharedInternals.Saround the hook already there. The Expo dev server evaluates the whole server bundle again for every web request, against one sharedreact, so every request left one more reconciler reachable from React, and the whole evaluated bundle behind it. The dev server ran out of memory after a dozen page loads.The root cause is on the Expo side (expo/expo#54308); this is a mitigation on ours. Nothing changes for a client: the reconciler is built by the first render into a canvas instead of at import.
Agent-written content
Summary
getReconciler()), andinjectIntoDevTools()moves along with it.SkiaSGRootcreates its React root in the firstrender(), not in its constructor. This part is needed on top of what the issue proposes:<Canvas>constructs itsSkiaSGRootin auseMemo, during render, and a<Canvas>does render on a server without CanvasKit. It renders into the root from a layout effect, which a server never runs.unmount()on a root that never rendered resolves right away, without committingnullinto an empty root.<Canvas>never does that: its layout effect renders before its unmount effect exists.Tests
renderer/__tests__/CanvasServerRender.spec.tsx: imports the library with the web modules and no CanvasKit, runsrenderToStringon a<Canvas>, and checks thatReactSharedInternals.Sis the same before the import, after it, and after the render.sksg/__tests__/LazyReconciler.spec.tsx: constructing and unmounting a root that never rendered leaves React untouched, the first render installs the hook, and later roots share the one reconciler.The server render spec was also run against two other versions of
Reconciler.tsto check that it tells them apart:Reconciler.tsmain(reconciler built at import)Local verification
yarn tsc,eslinton the changed files and the fullyarn jestrun inpackages/skiapass (130 suites, 985 tests).