From 8356d772b9bcb5c40449a9484e3fa5fb3645d992 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 30 Jul 2026 08:35:38 +0000 Subject: [PATCH] fix(sdui): stop the react page's "no adapter yet" fallback churning its provider context MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Audit of the remaining half of ReactKindPage's scope memo, [schema, adapter]. The schema half was the live bug fixed in #2984; this is the adapter half. The hosts are fine. Both AdapterCtx.Provider call sites pass a stable value — AdapterProvider from useState, the console preview from a module constant — so there is no state loss in the shipped app. One real instance remained, one layer down: `` minted a fresh object every render while the adapter was still null (the window before the host connects). That is a context value and SchemaRendererProvider memoises on its identity, so every block inside the page had its schema re-cloned and its expressions re-run on each render of the page. Now a module constant, mirroring the SchemaRenderer fallback. The `adapter` dependency itself must stay, and is now pinned. It looks like the obvious thing to optimise away — it is the last remaining trigger that can recompile a page and cost its useState. But ReactRunner hands React the same element object while (code, scope) hold, and React bails out on an identical element reference, so the page subtree never re-renders on its own: recompiling is the ONLY path by which a new adapter reaches the blocks inside the page. Verified by removing the dependency — blocks stay pinned to the first adapter forever, with no error, just a dead data source. react-page-adapter.test.tsx pins both directions so the tradeoff cannot be quietly re-litigated. Docs: the react-pages guide now states the host-side requirement — an adapter constructed inline on every render resets every react page on every render. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01N4mrr1ihhwnfEHFSWmGoMp --- .changeset/react-page-adapter-identity.md | 34 ++++++ content/docs/guide/react-pages.md | 23 +++- .../src/__tests__/react-page-adapter.test.tsx | 108 ++++++++++++++++++ .../src/renderers/layout/react-page.tsx | 14 ++- 4 files changed, 176 insertions(+), 3 deletions(-) create mode 100644 .changeset/react-page-adapter-identity.md create mode 100644 packages/components/src/__tests__/react-page-adapter.test.tsx diff --git a/.changeset/react-page-adapter-identity.md b/.changeset/react-page-adapter-identity.md new file mode 100644 index 0000000000..670cb9ab2e --- /dev/null +++ b/.changeset/react-page-adapter-identity.md @@ -0,0 +1,34 @@ +--- +"@object-ui/components": patch +--- + +fix(sdui): the react page's "no adapter yet" fallback stops churning its provider context + +Audit of the remaining half of `ReactKindPage`'s scope memo, `[schema, adapter]`. +The `schema` half was the live bug fixed in objectui#2984; this is the adapter +half. + +**The hosts are fine.** Both `AdapterCtx.Provider` call sites pass a stable +value — `AdapterProvider` from `useState`, the console preview from a module +constant — so there is no state loss in the shipped app. + +**One real instance remained**, one layer down: `` minted a fresh object on every render while the +adapter was still null (the window before the host connects). That is a context +value, and `SchemaRendererProvider` memoises on its identity, so every block +inside the page had its schema re-cloned and its expressions re-run on each +render of the page. Now a module constant, like the `SchemaRenderer` fallback +it mirrors. + +**The `adapter` dependency itself must stay**, and is now pinned. It looks like +the obvious thing to optimise away — it is the last remaining trigger that can +recompile a page and cost its `useState`. But `ReactRunner` hands React the same +element object while `(code, scope)` hold, and React bails out on an identical +element reference, so the page subtree never re-renders on its own: recompiling +is the *only* path by which a new adapter reaches the blocks inside the page. +Removing the dependency strands every block on the first adapter forever — no +error, just a dead data source. `react-page-adapter.test.tsx` pins both +directions, so the tradeoff cannot be quietly re-litigated. + +Docs: the react-pages guide now states the host-side requirement — an adapter +constructed inline on every render resets every react page on every render. diff --git a/content/docs/guide/react-pages.md b/content/docs/guide/react-pages.md index 08ee11a1c2..f2784866cb 100644 --- a/content/docs/guide/react-pages.md +++ b/content/docs/guide/react-pages.md @@ -167,8 +167,27 @@ injected — use HTML) or a block outside the public contract (use `Block`). ### Page state A react page keeps its own `useState` across re-renders and across lazy plugin -loads. Two things reset it, both intentional: a change to `source`, and a change -to the page's data/variables — the page is genuinely a different page then. +loads. Three things reset it, all intentional: a change to `source`, a change to +the page's data/variables, and a **new data source** — the page is genuinely a +different page then. + +That last one is a requirement on the **host**, not the author. The page is +recompiled when the adapter's *identity* changes, because recompiling is the +only way the new adapter reaches the blocks inside the page. So a host that +constructs a new adapter on every render resets every react page on every +render. Provide it from state or a module constant: + +```tsx +// ❌ new adapter object every render — every react page below loses its state + + +// ✅ +const [adapter, setAdapter] = useState(null); + +``` + +`@object-ui/app-shell`'s `AdapterProvider` already does this correctly; the rule +matters for custom hosts and preview surfaces. ## `kind:'html'` diff --git a/packages/components/src/__tests__/react-page-adapter.test.tsx b/packages/components/src/__tests__/react-page-adapter.test.tsx new file mode 100644 index 0000000000..f2d7efc583 --- /dev/null +++ b/packages/components/src/__tests__/react-page-adapter.test.tsx @@ -0,0 +1,108 @@ +/** + * ObjectUI + * Copyright (c) 2024-present ObjectStack Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * The adapter reaches a `kind:'react'` page's blocks, and keeps reaching them + * when the host swaps it. + * + * `ReactKindPage` memoises the injected scope on `[schema, adapter]`, and that + * `adapter` dependency looks like something to optimise away — it is the one + * remaining thing that can recompile the page and cost its `useState` + * (objectui#2954). It cannot go. + * + * `ReactRunner` returns the SAME element object while `(code, scope)` are + * unchanged, and React bails out of re-rendering a child whose element is + * referentially identical. So the page subtree does not re-render on its own: + * recompiling is the ONLY way a new adapter reaches the blocks inside the page. + * Drop the dependency (or read the adapter through a ref to "keep the scope + * stable") and every block in every react page silently keeps querying through + * the dead adapter — no error, just stale or failing data. + * + * These pin both halves so the tradeoff cannot be quietly re-litigated: + * a swapped adapter must reach the blocks, and an unchanged one must not + * disturb them. + */ + +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; +import { render, waitFor } from '@testing-library/react'; +import React from 'react'; +import { ComponentRegistry } from '@object-ui/core'; +import { SchemaRenderer, AdapterCtx } from '@object-ui/react'; + +/** The dataSource each render of the stand-in block was handed. */ +const seen: unknown[] = []; + +const makeAdapter = (id: string) => ({ id, find: async () => [] }) as any; + +const SOURCE = ` +function Page() { + const [n, setN] = React.useState(0); + return ( +
+ + +
+ ); +}`; + +const SCHEMA = { type: 'home', kind: 'react', name: 'adapter_page', source: SOURCE }; + +beforeAll(async () => { + await import('../renderers'); + ComponentRegistry.register('list-view', (props: any) => { + seen.push(props.schema?.dataSource); + return
; + }); +}, 30000); + +afterAll(() => { + ComponentRegistry.unregister('list-view'); +}); + +function Host({ adapter }: { adapter: any }) { + return ( + + + + ); +} + +describe('kind:\'react\' page ↔ adapter identity', () => { + it('hands blocks the new adapter when the host swaps it', async () => { + seen.length = 0; + const first = makeAdapter('first'); + const second = makeAdapter('second'); + + const { findByTestId, rerender } = render(); + await findByTestId('list-view-double'); + expect(seen.at(-1)).toBe(first); + + rerender(); + + // Nothing else re-renders the page subtree — ReactRunner hands React the + // same element object while (code, scope) hold, and React bails out on an + // identical element reference. Recompiling on the new scope is what carries + // the adapter in. + await waitFor(() => expect(seen.at(-1)).toBe(second)); + }); + + it('leaves the page alone while the adapter identity holds', async () => { + seen.length = 0; + const adapter = makeAdapter('stable'); + + const { findByTestId, getByTestId, rerender } = render(); + const counter = await findByTestId('counter'); + counter.click(); + await waitFor(() => expect(getByTestId('counter').textContent).toBe('1')); + + rerender(); + + // The other half of the tradeoff: the same adapter must not recompile, or + // the page loses its state on every parent render. + expect(getByTestId('counter').textContent).toBe('1'); + expect(seen.at(-1)).toBe(adapter); + }); +}); diff --git a/packages/components/src/renderers/layout/react-page.tsx b/packages/components/src/renderers/layout/react-page.tsx index ac7135854d..d633a6ec88 100644 --- a/packages/components/src/renderers/layout/react-page.tsx +++ b/packages/components/src/renderers/layout/react-page.tsx @@ -93,6 +93,18 @@ function buildComponentScope(dataSource: unknown): Record @@ -179,7 +191,7 @@ export const ReactKindPage: React.FC<{ schema: any }> = ({ schema }) => { const { ReactRunner } = runtime; return ( - +