Skip to content

fix(🌐): build the React reconciler on the first render, not at import (2.x backport of #4546) - #4547

Open
wcandillon wants to merge 1 commit into
2.xfrom
fix/lazy-reconciler-server-render-2.x
Open

wcandillon wants to merge 1 commit into
2.xfrom
fix/lazy-reconciler-server-render-2.x

Conversation

@wcandillon

Copy link
Copy Markdown
Owner

Description

Backport of #4546 to 2.x, for #4544 (reported against 2.14.0).

sksg/Reconciler.ts built its React reconciler when the module was evaluated. Building a reconciler hooks it into the React instance for good: createReconciler() wraps ReactSharedInternals.S around the hook already there. The Expo dev server evaluates the whole server bundle again for every web request, against one shared react, so every request left one more reconciler reachable from React, and the whole evaluated bundle behind it.

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

Same change as #4546, applied to the 2.x Reconciler.ts:

  • The reconciler is built on first use (getReconciler()), and injectIntoDevTools() moves along with it.
  • SkiaSGRoot creates its React root in the first render(), not in its constructor: <Canvas> constructs its SkiaSGRoot in a useMemo, during render, which a server does too, and renders into it from a layout effect, which a server never runs.
  • unmount() on a root that never rendered resolves right away, without committing null into an empty root.

Differences from #4546

  • The source change is the same; 2.x passes fewer arguments to createContainer (react-reconciler 0.31.0) and keeps its @ts-expect-error on injectIntoDevTools().
  • CanvasServerRender.spec.tsx resolves SkiaPictureViewNativeComponent to its web module, the name of the native component on 2.x. LazyReconciler.spec.tsx is unchanged.

Local verification

Run in a worktree of 2.x with its own dependencies installed from the 2.x lockfile (React 19.0.0, react-reconciler 0.31.0):

  • yarn tsc, eslint on the changed files and the full yarn jest run in packages/skia pass (108 suites, 845 tests).
  • The server render spec passes with this change, fails right after the import against the 2.x Reconciler.ts it replaces, and fails after the render against a version that builds the reconciler lazily but still creates the root in the constructor.
  • Not run: the reproduction from the issue against a real Expo dev server, and the device e2e suites, which run on CI.

… (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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant