From 9396f4998ad93acde5687e44a24f8188aa9e9d7c Mon Sep 17 00:00:00 2001 From: William Candillon Date: Thu, 26 Nov 2026 18:03:30 +0100 Subject: [PATCH] =?UTF-8?q?fix(=F0=9F=8C=90):=20build=20the=20React=20reco?= =?UTF-8?q?nciler=20on=20the=20first=20render,=20not=20at=20import=20(2.x?= =?UTF-8?q?=20backport=20of=20#4546)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../__tests__/CanvasServerRender.spec.tsx | 55 +++++++++++++++ packages/skia/src/sksg/Reconciler.ts | 70 ++++++++++++++----- .../sksg/__tests__/LazyReconciler.spec.tsx | 46 ++++++++++++ 3 files changed, 154 insertions(+), 17 deletions(-) create mode 100644 packages/skia/src/renderer/__tests__/CanvasServerRender.spec.tsx create mode 100644 packages/skia/src/sksg/__tests__/LazyReconciler.spec.tsx diff --git a/packages/skia/src/renderer/__tests__/CanvasServerRender.spec.tsx b/packages/skia/src/renderer/__tests__/CanvasServerRender.spec.tsx new file mode 100644 index 0000000000..85a9d8cb6c --- /dev/null +++ b/packages/skia/src/renderer/__tests__/CanvasServerRender.spec.tsx @@ -0,0 +1,55 @@ +import React from "react"; +import { renderToString } from "react-dom/server"; + +import type { Canvas as CanvasComponent } from "../Canvas"; +import type { Group as GroupComponent } from "../components"; + +// Resolve the platform modules to their web implementation, as a web bundler +// would. CanvasKit is not loaded, as on a server. +jest.mock("../../Platform", () => require("../../Platform/Platform.web")); +jest.mock("../../specs/SkiaPictureViewNativeComponent", () => + require("../../specs/SkiaPictureViewNativeComponent.web") +); +jest.mock("../../specs/NativeSkiaModule", () => + require("../../specs/NativeSkiaModule.web") +); +jest.mock("../../skia/Skia", () => require("../../skia/Skia.web")); +jest.mock("../../sksg/Container", () => require("../../sksg/Container.web")); + +// What a reconciler hooks itself into when it is built: it wraps +// onStartTransitionFinish (S) around the one already there. +const internals = ( + React as unknown as { + __CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE: { + S: unknown; + }; + } +).__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE; + +// A server that evaluates the bundle once per request against a single React +// instance (the Expo dev server, for web) keeps whatever an evaluation hooks +// into React, and the whole bundle behind it, for the lifetime of the process +// (#4544). Importing the library and rendering a Canvas to HTML must leave +// React as they found it: the reconciler is built by the first render into a +// Canvas, from a layout effect, which a server never runs. +describe("Canvas server render", () => { + it("imports and renders to HTML without building the reconciler", () => { + const untouched = internals.S; + // Installs global.SkiaViewApi + require("../../specs/NativeSkiaModule.web"); + const { Canvas } = require("../Canvas") as { + Canvas: typeof CanvasComponent; + }; + const { Group } = require("../components") as { + Group: typeof GroupComponent; + }; + expect(internals.S).toBe(untouched); + const html = renderToString( + + + + ); + expect(html).toContain(" { + const reconciler = ReactReconciler(sksgHostConfig); + // @ts-expect-error DefinitelyTyped is not up to date + reconciler.injectIntoDevTools(); + return reconciler; +}; -// @ts-expect-error DefinitelyTyped is not up to date -skiaReconciler.injectIntoDevTools(); +let skiaReconciler: ReturnType | null = null; + +/** + * The reconciler is built by the first render, not when this module is + * evaluated: building one hooks it into the React instance for the lifetime + * of the process (ReactSharedInternals.S, the DevTools hook). A server that + * evaluates the bundle once per request against a single React instance, as + * the Expo dev server does for web, would otherwise keep one reconciler, and + * the whole bundle behind it, per request. + */ +const getReconciler = () => { + if (skiaReconciler === null) { + skiaReconciler = createReconciler(); + } + return skiaReconciler; +}; export class SkiaSGRoot { - private root: OpaqueRoot; + private root: OpaqueRoot | null = null; private container: Container; constructor( public Skia: Skia, nativeId = -1 ) { - const strictMode = false; this.container = createContainer(Skia, nativeId); - this.root = skiaReconciler.createContainer( - this.container, - 0, - null, - strictMode, - null, - "", - console.error, - null - ); + } + + /** + * The React root, created by the first render: a Canvas constructs its + * SkiaSGRoot while it renders, which a server does too, and renders into + * it from a layout effect, which a server never runs. + */ + private getRoot() { + if (this.root === null) { + const strictMode = false; + this.root = getReconciler().createContainer( + this.container, + 0, + null, + strictMode, + null, + "", + console.error, + null + ); + } + return this.root; } get sg() { @@ -44,8 +74,9 @@ export class SkiaSGRoot { } private updateContainer(element: ReactNode) { + const root = this.getRoot(); return new Promise((resolve) => { - skiaReconciler.updateContainer(element, this.root, null, () => { + getReconciler().updateContainer(element, root, null, () => { resolve(true); }); }); @@ -72,8 +103,13 @@ export class SkiaSGRoot { unmount() { this.container.unmount(); + const { root } = this; + if (root === null) { + // Never rendered: React holds nothing for this root. + return Promise.resolve(true); + } return new Promise((resolve) => { - skiaReconciler.updateContainer(null, this.root, null, () => { + getReconciler().updateContainer(null, root, null, () => { debug("unmountContainer"); resolve(true); }); diff --git a/packages/skia/src/sksg/__tests__/LazyReconciler.spec.tsx b/packages/skia/src/sksg/__tests__/LazyReconciler.spec.tsx new file mode 100644 index 0000000000..36794fcfbe --- /dev/null +++ b/packages/skia/src/sksg/__tests__/LazyReconciler.spec.tsx @@ -0,0 +1,46 @@ +import React from "react"; + +import { importSkia } from "../../renderer/__tests__/setup"; +import { SkiaSGRoot } from "../Reconciler"; + +// What a reconciler hooks itself into when it is built: it wraps +// onStartTransitionFinish (S) around the one already there. +const internals = ( + React as unknown as { + __CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE: { + S: unknown; + }; + } +).__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE; + +// In its own file: the reconciler is built once per module registry, so no +// other test may render before this one. +describe("Reconciler creation", () => { + it("leaves React untouched until the first render", async () => { + const { Skia, Group } = importSkia(); + const atImport = internals.S; + + // What a server render of a Canvas does: construct the root, never + // render into it. + const idle = new SkiaSGRoot(Skia); + expect(internals.S).toBe(atImport); + expect(await idle.unmount()).toBe(true); + expect(internals.S).toBe(atImport); + + const root = new SkiaSGRoot(Skia); + await root.render(); + const installed = internals.S; + expect(installed).not.toBe(atImport); + expect(root.sg.children).toHaveLength(1); + + // One reconciler for every root. + const other = new SkiaSGRoot(Skia); + await other.render(); + await idle.render(); + expect(internals.S).toBe(installed); + expect(idle.sg.children).toHaveLength(1); + + await Promise.all([root.unmount(), other.unmount(), idle.unmount()]); + expect(internals.S).toBe(installed); + }); +});