diff --git a/.changeset/38170-bundle-on-app-list.md b/.changeset/38170-bundle-on-app-list.md new file mode 100644 index 00000000000..980ae37f10c --- /dev/null +++ b/.changeset/38170-bundle-on-app-list.md @@ -0,0 +1,10 @@ +--- +'@object-ui/app-shell': patch +--- + +perf(app-shell): on a cold Console load the app bundle request leaves the moment the app list answers + +Clause-②: no +Metadata-protocol: none + +On an app route the console root asked the app's metadata bundle (`GET /api/v1/meta/app/:name/bundle`) from an effect of the render that names the app, so a whole tree re-render sat between the app list's answer (`GET /api/v1/meta/app`) and the bundle request: 226 ms on an idle box and 378 ms on a busy one (medians over 27 cold loads each). The bundle is now asked in the app list's own answer. The app list still decides which app the route names (the route segment is a package id or an app name) and whether the user may see it, so it is the same request, through the same adapter and session, for the same app, once; a route naming an app the list does not hold still asks no bundle. Measured on the live lane's build (backend `--fresh`, `vite build` with `VITE_BASE_PATH=/`, `vite preview`), the bundle request moved from 946 ms to 764 ms after navigation idle and from 1343 ms to 974 ms with four CPU-bound processes running; the page heading moved from 2300 ms to 2035 ms (median) on the busy box and did not move outside noise on the idle one. diff --git a/ui/packages/app-shell/src/console/__tests__/appObjectScope-12811.test.tsx b/ui/packages/app-shell/src/console/__tests__/appObjectScope-12811.test.tsx index 909e876b4da..fc1a70725f8 100644 --- a/ui/packages/app-shell/src/console/__tests__/appObjectScope-12811.test.tsx +++ b/ui/packages/app-shell/src/console/__tests__/appObjectScope-12811.test.tsx @@ -617,3 +617,153 @@ describe('objectstack#35455 — round trips until an app route can render, count expect(seen).toEqual([['app'], ['page']]); }); }); + +/** + * objectstack#38170 — the bundle request leaves the moment the app list lands. + * + * Measured on a cold Console load (backend `--fresh`, `vite build` with + * `VITE_BASE_PATH=/`, `vite preview`): the bundle request left one whole tree + * re-render after the app list answered — 226 ms later idle, 378 ms on a busy + * box (medians) — because it was asked from an effect of the render that + * named the app. It is now asked in the app list's own answer. + * + * The app list is still what names the app — the route carries a package id + * (ADR-0048) or a name, and an app the list does not hold has no bundle + * asked — so the request is the same one, through the same adapter, once. + */ +describe('objectstack#38170 — the console root enters the bundle as the app list lands', () => { + const PACKAGED = [{ name: 'crm', _packageId: CRM, navigation: [] }]; + + /** An adapter whose app list answers only when the test says so, and which records the order things happen in. */ + function makeHeldAdapter(events: string[], apps: unknown[]) { + const calls: string[] = []; + const base = makeAdapter(calls, apps) as any; + let answer!: () => void; + const held = new Promise((resolve) => { answer = resolve; }); + const client = base.getClient(); + const adapter = { + ...base, + getClient: () => ({ + meta: { + ...client.meta, + getItems: (type: string, options?: Record) => { + if (type !== 'app' || options) return client.meta.getItems(type, options); + events.push('list:asked'); + return held.then(() => { + events.push('list:answered'); + return client.meta.getItems(type); + }); + }, + }, + }), + loadAppBundle(this: unknown, app: string) { + events.push(`bundle:${app}${this === adapter ? '' : ':foreign-this'}`); + return Promise.resolve(); + }, + }; + return { adapter: adapter as Parameters[0]['adapter'], answer, calls }; + } + + /** Records each render's view of the app list, beside the requests. */ + function Recorder({ events }: { events: string[] }) { + const { apps } = useMetadata(); + useAppObjectScopePrefetch(); + const named = Array.isArray(apps) && apps.length > 0; + if (events[events.length - 1] !== `render:${named ? 'named' : 'empty'}`) events.push(`render:${named ? 'named' : 'empty'}`); + return null; + } + + function mountHeld(url: string, apps: unknown[] = PACKAGED) { + sessionStorage.clear(); + const events: string[] = []; + const held = makeHeldAdapter(events, apps); + const view = render( + + + + + + + , + ); + return { events, ...held, view }; + } + + const bundles = (events: string[]) => events.filter((e) => e.startsWith('bundle:')); + + it('asks the bundle in the app list answer, before the render that names the app', async () => { + const { events, answer, calls, adapter } = mountHeld(`/apps/${CRM}/crm_account`); + await waitFor(() => expect(events).toContain('list:asked')); + expect(bundles(events)).toEqual([]); + await act(async () => answer()); + await waitFor(() => expect(events).toContain('render:named')); + await new Promise((resolve) => setTimeout(resolve, 50)); + // The ordering pin: the request is not sequenced after the render (and + // the boot reads it waits on) any more. Reverting to the render-time + // entry puts `bundle:crm` after `render:named` and turns this red. + const at = events.indexOf('bundle:crm'); + expect(at).toBeGreaterThan(events.indexOf('list:answered')); + expect(at).toBeLessThan(events.indexOf('render:named')); + // Same request count: entered once, the app list read once. + expect(bundles(events)).toEqual(['bundle:crm']); + expect(calls.filter((c) => c === 'app')).toEqual(['app']); + // `AppContent` reuses the root's entry: still one bundle request. + enterAppBundle(adapter, 'crm'); + expect(bundles(events)).toEqual(['bundle:crm']); + }); + + it('names the bundle by the app, never by the route segment (ADR-0048), through the same adapter', async () => { + const { events, answer } = mountHeld(`/apps/${CRM}/crm_account`); + await act(async () => answer()); + await waitFor(() => expect(events).toContain('render:named')); + await new Promise((resolve) => setTimeout(resolve, 50)); + // `app.crm` is the package id the route carries; `crm` is the app it names. + expect(bundles(events)).toEqual(['bundle:crm']); + }); + + it('CONTROL: an app the list does not hold (one this user may not see) has no bundle asked, before or after the list', async () => { + const { events, answer } = mountHeld('/apps/hr/hr_employee'); + await act(async () => answer()); + await waitFor(() => expect(events).toContain('render:named')); + await new Promise((resolve) => setTimeout(resolve, 50)); + expect(bundles(events)).toEqual([]); + }); + + it('CONTROL: outside an app, or after leaving before the list lands, nothing is entered', async () => { + const outside = mountHeld('/home'); + await act(async () => outside.answer()); + await waitFor(() => expect(outside.events).toContain('render:named')); + const left = mountHeld(`/apps/${CRM}/crm_account`); + await waitFor(() => expect(left.events).toContain('list:asked')); + left.view.unmount(); + await act(async () => left.answer()); + await new Promise((resolve) => setTimeout(resolve, 50)); + expect(bundles(outside.events)).toEqual([]); + expect(bundles(left.events)).toEqual([]); + }); + + it('CONTROL: an app list already in hand enters the bundle from the render, once, as before', async () => { + const calls: string[] = []; + const entered: string[] = []; + // The first mount reads the list into the session; the second answers from it. + const first = mountHeld(`/apps/${CRM}/crm_account`); + await act(async () => first.answer()); + await waitFor(() => expect(first.events).toContain('bundle:crm')); + cleanup(); + const adapter = Object.assign(makeAdapter(calls, PACKAGED) as object, { + loadAppBundle: (app: string) => { entered.push(app); return Promise.resolve(); }, + }) as Parameters[0]['adapter']; + render( + + + + + + + , + ); + await waitFor(() => expect(entered).toEqual(['crm'])); + await new Promise((resolve) => setTimeout(resolve, 50)); + expect(entered).toEqual(['crm']); + }); +}); diff --git a/ui/packages/app-shell/src/console/appObjectScope.ts b/ui/packages/app-shell/src/console/appObjectScope.ts index 68aefa57dbb..9da27dd0c06 100644 --- a/ui/packages/app-shell/src/console/appObjectScope.ts +++ b/ui/packages/app-shell/src/console/appObjectScope.ts @@ -88,7 +88,7 @@ * permission read rather than after it, as the whole list used to. */ -import { useEffect } from 'react'; +import { useEffect, useRef } from 'react'; import { matchRoutes, useLocation } from 'react-router-dom'; import { isRecordPageOf, useAdapter, useMetadata, type AnsweredLoadFailure, type MetadataContextValue } from '@object-ui/react'; import { defaultRecordPageObject } from '@objectstack/spec/ui'; @@ -926,6 +926,9 @@ function inRelatedOrder(scope: AppObjectScope, relatedNames: readonly string[]): return { ...scope, objects } as AppObjectScope; } +/** An app as the app list answers it, read for the route lookup alone. */ +type ListedApp = { name?: unknown; _packageId?: unknown } & Record; + /** An adapter that loads app metadata bundles (`ObjectStackAdapter.loadAppBundle`). */ type BundleHost = { loadAppBundle?: (appName: string) => unknown }; @@ -989,10 +992,55 @@ export function useAppObjectScopePrefetch(): void { useEffect(() => { if (recordRoute && ensureType) void ensureType('page'); }, [recordRoute, ensureType]); + // objectstack#38170 — the bundle leaves the moment the app list lands, not + // one render later. The list is what names the app: the route carries a + // package id (ADR-0048) or a name, and only an app the list holds (an app + // this user may see) has its bundle asked at all, so the request is the one + // the effect below would make, from the same adapter, for the same app. On a + // cold load the list is still on the wire when this mounts, and waiting for + // the render that names the app put a re-render of the whole tree between + // the list's answer and the bundle request. `ensureType('app')` joins the + // provider's eager app-list read: it never asks the list again. + const routeSegment = segments[0] === 'apps' ? segments[1] : undefined; + const ensureTypeRef = useRef(ensureType); + useEffect(() => { + ensureTypeRef.current = ensureType; + }); + const listEnteredRef = useRef(null); + useEffect( + () => () => { + // The entry belongs to the visit it was made for: leaving the app, or an + // adapter swap, forgets it, so the next visit enters the app again. + listEnteredRef.current = null; + }, + [dataSource, routeSegment], + ); + useEffect(() => { + const ensure = ensureTypeRef.current; + const load = dataSource?.loadAppBundle; + // Named by the list in hand already: the effect below enters it now. + if (bundleAppName || !routeSegment || !dataSource || !ensure || typeof load !== 'function') return; + let live = true; + void ensure('app').then((list: unknown) => { + if (!live) return; + const listed = matchAppBySegment(Array.isArray(list) ? (list as ListedApp[]) : [], routeSegment); + const name = typeof listed?.name === 'string' && listed.name ? listed.name : undefined; + if (!name || rootEnteredApps.has(name)) return; + listEnteredRef.current = name; + void load.call(dataSource, name); + }); + return () => { + live = false; + }; + }, [dataSource, routeSegment, bundleAppName]); useEffect(() => { const load = dataSource?.loadAppBundle; if (!dataSource || !bundleAppName || typeof load !== 'function') return; - void load.call(dataSource, bundleAppName); + // Entered when the list landed, in this same visit: that request is the + // entry, and asking again would revalidate a bundle still on the wire. + const enteredByList = listEnteredRef.current === bundleAppName; + listEnteredRef.current = null; + if (!enteredByList) void load.call(dataSource, bundleAppName); rootEnteredApps.add(bundleAppName); return () => { rootEnteredApps.delete(bundleAppName);