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..5dc2dc6597 --- /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/SkiaViewNativeComponent", () => + require("../../specs/SkiaViewNativeComponent.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); + reconciler.injectIntoDevTools(); + return reconciler; +}; -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, - console.error, - 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, + console.error, + console.error, + () => {}, + null + ); + } + return this.root; } get sg() { @@ -47,8 +77,9 @@ export class SkiaSGRoot { private updateContainer(element: ReactNode) { const { recordCount } = this.container; + const root = this.getRoot(); return new Promise((resolve) => { - skiaReconciler.updateContainer(element, this.root, null, () => { + getReconciler().updateContainer(element, root, null, () => { if (this.container.recordCount === recordCount) { this.container.recordAfterCommit(); } @@ -90,8 +121,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); + }); +});