Skip to content

Commit 3ab5016

Browse files
committed
feat(app-shell): entering an app loads its metadata bundle; record open reads no metadata
The bundle's members are read as the by-name envelopes the spec's AppMetadataBundleResponse declares; acceptance pins a warm record open at one page request and zero metadata requests, a cold one at one bundle request. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibzFu8iUogiZhxL9tAo
1 parent bc9c79e commit 3ab5016

5 files changed

Lines changed: 112 additions & 49 deletions

File tree

‎ui/packages/app-shell/src/console/AppContent.tsx‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,17 @@ export function AppContent({ extraRoutes, extraRoutesNoApp }: AppContentProps =
387387
// (objectui#11818).
388388
const appSegment = appAddressSegment(apps, matchedApp) ?? appName ?? appRouteSegment(activeApp);
389389

390+
// objectstack#27627 — entering an app loads its metadata bundle, and
391+
// re-entering it revalidates the one held (`loadAppBundle`): from then on the
392+
// by-name metadata reads of everything the app reaches are answered from it.
393+
// A host adapter without the bundle keeps every by-name read.
394+
const bundleAppName: string | undefined = typeof activeApp?.name === 'string' ? activeApp.name : undefined;
395+
useEffect(() => {
396+
const load = dataSource?.loadAppBundle;
397+
if (!bundleAppName || typeof load !== 'function') return;
398+
void load.call(dataSource, bundleAppName);
399+
}, [dataSource, bundleAppName]);
400+
390401
// objectstack-ai/objectstack#26183 — a URL that reached the app through its
391402
// NAME alias moves to the app's one address, path, search, hash and history
392403
// state kept. The alias still opens (old bookmarks and shared links keep

‎ui/packages/app-shell/src/views/RecordDetailView.recordPagePayload-27627.test.tsx‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
* approval / lock / record read, one wave of data requests; the badges draw the
2222
* payload's totals; and the record a drawer step shows next is asked ahead, so stepping to it
2323
* asks nothing for the record it opens.
24+
*
25+
* And once the app's metadata bundle is in (`loadAppBundle`, on app entry), the
26+
* object definitions the blocks read come from it: a record open in a warm
27+
* session asks the page and no metadata at all, and a cold one adds at most
28+
* the one bundle request.
2429
*/
2530
import * as React from 'react';
2631
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
@@ -106,7 +111,14 @@ function pageAnswer(id: string, body: any) {
106111
};
107112
}
108113

114+
const APP = 'demo';
115+
const BUNDLE = `/api/v1/meta/app/${APP}/bundle`;
116+
109117
function answerFor(h: Held): unknown {
118+
if (h.path === BUNDLE) {
119+
const env = (type: string, item: { name: string }) => ({ type, name: item.name, item });
120+
return { app: env('app', { name: APP }), objects: ALL.map((o) => env('object', o)), views: [], pages: [], dashboards: [] };
121+
}
110122
const meta = /\/api\/v1\/meta\/object\/([^/?]+)/.exec(h.path);
111123
if (meta) return { success: true, data: ALL.find((o) => o.name === meta[1]) ?? null };
112124
const page = /\/api\/v1\/data\/[^/?]+\/([^/?]+)\/page$/.exec(h.path);
@@ -206,9 +218,9 @@ describe('objectstack#27627 — one request opens a record page', () => {
206218
const data = dataRequests(held);
207219
expect(data.map((h) => `${h.method} ${h.path}`)).toEqual([`POST /api/v1/data/${OBJECT}/c1/page`]);
208220
expect(held.filter((h) => /[?&](\$?top)=1\b/.test(h.path))).toEqual([]);
209-
// Every DATA request goes out in the first wave. The object definitions the
210-
// blocks read (`/meta/object/…`) are the app bundle's to serve once per
211-
// session; until then they are read as before, beside it.
221+
// Every DATA request goes out in the first wave. With no app entered there
222+
// is no metadata bundle, so the object definitions the blocks read
223+
// (`/meta/object/…`) are read by name beside it; with one, see below.
212224
expect(data.every((h) => h.wave === 1)).toBe(true);
213225

214226
// The request names what the page draws: its four related lists, the
@@ -301,6 +313,31 @@ describe('objectstack#27627 — one request opens a record page', () => {
301313
expect(dataRequests(held).filter((h) => !isPage(h))).toEqual([]);
302314
});
303315

316+
it('a cold session, entering the app as the record opens, adds one bundle request and no by-name metadata read', async () => {
317+
const { held, state, open, ds } = await boot();
318+
void ds.loadAppBundle(APP);
319+
open('c1');
320+
await runWaves(held, state);
321+
expect(held.filter(isMeta).map((h) => `${h.method} ${h.path}`)).toEqual([`GET ${BUNDLE}`]);
322+
expect(dataRequests(held).map((h) => `${h.method} ${h.path}`)).toEqual([`POST /api/v1/data/${OBJECT}/c1/page`]);
323+
// Both go out in the first wave: nothing waits on the other.
324+
expect(held.every((h) => h.wave === 1)).toBe(true);
325+
});
326+
327+
it('a warm session opens a record with one page request and zero metadata requests', async () => {
328+
const { held, state, open, ds } = await boot();
329+
// The app was entered earlier in the session; no record was opened yet,
330+
// so no object definition was ever read by name.
331+
const entered = ds.loadAppBundle(APP);
332+
await runWaves(held, state);
333+
await entered;
334+
held.length = 0;
335+
state.wave = 1;
336+
open('c1');
337+
await runWaves(held, state);
338+
expect(held.map((h) => `${h.method} ${h.path}`)).toEqual([`POST /api/v1/data/${OBJECT}/c1/page`]);
339+
});
340+
304341
it('in a drawer, an adapter without the payload keeps the count warm-up: each of the next record\'s counts is asked once', async () => {
305342
const { held, state, open, ds } = await boot();
306343
Object.assign(ds, { getRecordPage: undefined, prefetchRecordPage: undefined });

‎ui/packages/data-objectstack/src/app-bundle.ts‎

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
* asks the server and reports what it answered.
3131
*/
3232

33-
/** One bundled item, as the by-name route's envelope would carry it. */
33+
/** One bundled item. */
3434
export interface AppBundleEntry {
35-
/** The item's document. */
35+
/** The item's document (the envelope's `item`). */
3636
item: unknown;
37-
/** The envelope keys the by-name route serves beside `item` (an object's `sortability`, `disabledActions`). */
37+
/** The whole envelope its by-name read answers (`type`, `name`, `item`, an object's `sortability`, …). */
3838
envelope: Record<string, unknown>;
3939
}
4040

@@ -164,7 +164,7 @@ export class AppBundleStore {
164164
}
165165
}
166166

167-
/** Where a bundle answer lists each metadata type. */
167+
/** Where the bundle lists each metadata type. */
168168
const BUNDLE_LISTS: ReadonlyArray<readonly [key: string, type: string]> = [
169169
['objects', 'object'],
170170
['views', 'view'],
@@ -173,37 +173,29 @@ const BUNDLE_LISTS: ReadonlyArray<readonly [key: string, type: string]> = [
173173
];
174174

175175
/**
176-
* Index a bundle answer's data by type and name. Each listed part is either the
177-
* by-name route's envelope (`{ item, … }`) or the bare document; the app rides
178-
* under `app`.
176+
* Index the bundle's answer (`AppMetadataBundleResponse`, `@objectstack/spec/api`)
177+
* by type and name. The answer is bare, as the `/meta` item reads answer: the
178+
* app under `app`, and the other items listed under `objects`, `views`,
179+
* `pages` and `dashboards`. Every member is the envelope its own
180+
* `GET /meta/{type}/{name}` answers (`GetMetaItemResponse`), so it is kept
181+
* whole and handed back as that read's answer.
179182
*/
180-
export function indexAppBundle(data: unknown): AppBundleItems {
183+
export function indexAppBundle(body: unknown): AppBundleItems {
181184
const items: AppBundleItems = new Map();
182-
if (!data || typeof data !== 'object') return items;
183-
const bag = data as Record<string, unknown>;
184-
const put = (type: string, part: unknown) => {
185-
const entry = toEntry(part);
186-
if (!entry) return;
187-
const name = (entry.item as { name?: unknown }).name;
188-
if (typeof name !== 'string' || !name) return;
185+
if (!body || typeof body !== 'object') return items;
186+
const bag = body as Record<string, unknown>;
187+
const put = (type: string, member: unknown) => {
188+
if (!member || typeof member !== 'object') return;
189+
const envelope = member as Record<string, unknown>;
190+
if (typeof envelope.name !== 'string' || !envelope.name) return;
189191
let byName = items.get(type);
190192
if (!byName) items.set(type, (byName = new Map()));
191-
byName.set(name, entry);
193+
byName.set(envelope.name, { item: envelope.item, envelope });
192194
};
193-
if (bag.app !== undefined) put('app', bag.app);
195+
put('app', bag.app);
194196
for (const [key, type] of BUNDLE_LISTS) {
195197
const list = bag[key];
196-
if (Array.isArray(list)) for (const part of list) put(type, part);
198+
if (Array.isArray(list)) for (const member of list) put(type, member);
197199
}
198200
return items;
199201
}
200-
201-
function toEntry(part: unknown): AppBundleEntry | undefined {
202-
if (!part || typeof part !== 'object') return undefined;
203-
const p = part as Record<string, unknown>;
204-
if ('item' in p) {
205-
if (!p.item || typeof p.item !== 'object') return undefined;
206-
return { item: p.item, envelope: p };
207-
}
208-
return { item: p, envelope: { item: p } };
209-
}

‎ui/packages/data-objectstack/src/appBundle-27627.test.ts‎

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
* - re-entering sends `If-None-Match`, and a `304` keeps what is held;
2323
* - an invalidation naming a bundled item, and any metadata write through the
2424
* client, make the next bundled read ask the bundle again first;
25-
* - a cache clear downloads the bundle again, with no validator;
25+
* - a bundled object carries its envelope's `sortability`, as its by-name read does;
26+
* - a cache clear, or a bundle served with no validator, downloads it again;
2627
* - a refused bundle leaves every read to its by-name route.
2728
*/
2829
import { describe, it, expect, vi } from 'vitest';
30+
import { readObjectSortability } from '@object-ui/core';
2931
import { ObjectStackAdapter } from './index';
3032

3133
const BASE = 'http://test.local';
@@ -78,19 +80,18 @@ async function drain() {
7880
const contact = { name: 'b27627_contact', label: 'Contact', fields: { name: { type: 'text', label: 'Name' } } };
7981
const task = { name: 'b27627_task', label: 'Task', fields: { contact: { type: 'lookup', reference: 'b27627_contact' } } };
8082

83+
/** The bundle's answer: bare, every member the envelope its by-name read answers. */
8184
function bundle(label = 'Contact') {
85+
const env = (type: string, item: { name: string }, extra: Record<string, unknown> = {}) => ({ type, name: item.name, item, ...extra });
8286
return {
83-
success: true,
84-
data: {
85-
app: { name: APP, label: 'App' },
86-
objects: [
87-
{ item: { ...contact, label }, sortability: { name: { sortable: true } } },
88-
{ item: task },
89-
],
90-
views: [{ name: 'b27627_contact.all', object: 'b27627_contact', label: 'All' }],
91-
pages: [{ name: 'b27627_contact_page', label: 'Contact page' }],
92-
dashboards: [{ name: 'b27627_home', label: 'Home' }],
93-
},
87+
app: env('app', { name: APP, label: 'App' }),
88+
objects: [
89+
env('object', { ...contact, label }, { sortability: { fields: { name: { sortable: true } } } }),
90+
env('object', task),
91+
],
92+
views: [env('view', { name: 'b27627_contact.all', object: 'b27627_contact', label: 'All' } as { name: string })],
93+
pages: [env('page', { name: 'b27627_contact_page', label: 'Contact page' } as { name: string })],
94+
dashboards: [env('dashboard', { name: 'b27627_home', label: 'Home' } as { name: string })],
9495
};
9596
}
9697

@@ -212,7 +213,7 @@ describe('objectstack#27627 — the app metadata bundle', () => {
212213
expect(held[2].path).toBe(BUNDLE_PATH);
213214
expect(held[2].headers['if-none-match']).toBe('W/"b1"');
214215
const next = bundle();
215-
next.data.pages = [{ name: 'b27627_contact_page', label: 'Renamed' }];
216+
next.pages = [{ type: 'page', name: 'b27627_contact_page', item: { name: 'b27627_contact_page', label: 'Renamed' } }];
216217
held[2].respond(200, next, { ETag: 'W/"b2"' });
217218
expect((await reread as any).item.label).toBe('Renamed');
218219
});
@@ -233,11 +234,35 @@ describe('objectstack#27627 — the app metadata bundle', () => {
233234
expect((await reread as any).label).toBe('Kontakt');
234235
});
235236

237+
it("a bundled object carries its envelope's sortability, as its by-name read does", async () => {
238+
const { ds, held } = await heldAdapter();
239+
const entered = ds.loadAppBundle(APP);
240+
await drain();
241+
held[0].respond(200, bundle(), { ETag: '"b1"' });
242+
await entered;
243+
expect(readObjectSortability(await ds.getObjectSchema('b27627_contact'))?.fields.name?.sortable).toBe(true);
244+
});
245+
246+
it('a bundle served with no validator (`no-store`) is downloaded again, never revalidated', async () => {
247+
const { ds, held } = await heldAdapter();
248+
const first = ds.loadAppBundle(APP);
249+
await drain();
250+
held[0].respond(200, bundle(), { 'Cache-Control': 'private, no-store' });
251+
await first;
252+
const again = ds.loadAppBundle(APP);
253+
await drain();
254+
expect(held[1].path).toBe(BUNDLE_PATH);
255+
expect(held[1].headers['if-none-match']).toBeUndefined();
256+
held[1].respond(200, bundle());
257+
await again;
258+
});
259+
236260
it('a refused bundle leaves every read to its by-name route', async () => {
237261
const { ds, held } = await heldAdapter();
238262
const first = ds.loadAppBundle(APP);
239263
await drain();
240-
held[0].respond(404, { success: false, error: { code: 'NOT_FOUND', message: 'No such app' } });
264+
// An app the caller cannot read refuses the whole bundle with the app read's own answer.
265+
held[0].respond(403, { success: false, error: { code: 'PERMISSION_DENIED', message: 'Not your app' } });
241266
await first;
242267

243268
const read = ds.getObjectSchema('b27627_contact');

‎ui/packages/data-objectstack/src/index.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4495,7 +4495,7 @@ export class ObjectStackAdapter<T = unknown> implements DataSource<T> {
44954495
const [type, name, options] = args;
44964496
if (options !== undefined) return original(...args);
44974497
return this.appBundles.read(type, name).then((entry) =>
4498-
entry ? ({ type, name, ...structuredClone(entry.envelope) } as Awaited<ReturnType<typeof original>>) : original(type, name),
4498+
entry ? (structuredClone(entry.envelope) as Awaited<ReturnType<typeof original>>) : original(type, name),
44994499
);
45004500
}) as typeof meta.getItem;
45014501

@@ -4534,9 +4534,7 @@ export class ObjectStackAdapter<T = unknown> implements DataSource<T> {
45344534
const res = await this.fetchImpl(url, { method: 'GET', headers, cache: 'no-store' });
45354535
if (res.status === 304) return { status: 'unchanged' };
45364536
if (!res.ok) return { status: 'absent' };
4537-
const body: any = await res.json();
4538-
const data = body && typeof body === 'object' && 'success' in body && 'data' in body ? body.data : body;
4539-
return { status: 'fresh', etag: res.headers.get('etag') ?? undefined, items: indexAppBundle(data) };
4537+
return { status: 'fresh', etag: res.headers.get('etag') ?? undefined, items: indexAppBundle(await res.json()) };
45404538
}
45414539

45424540
/**

0 commit comments

Comments
 (0)