Skip to content

Commit 9185684

Browse files
authored
fix(analog): scope server calls to their tab and keep the source scan (#467)
- Server calls and renders are tagged with the page that made them, and the Server view and analog-list-server-calls show the current tab's calls by default. - The source scan is cached until a file changes (Vite watcher, or fs.watch with re-watch on errors), and open pages are checked against each rescan. Refs #369
1 parent caf4829 commit 9185684

24 files changed

Lines changed: 1296 additions & 87 deletions

‎app/src/__tests__/analog-inspector-pages.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,55 @@ describe('Analog open pages', () => {
148148
expect(inspector.isOpen(route('/src/app/pages/about.page.ts'))).toBe(false);
149149
});
150150
});
151+
152+
describe('Analog server calls of the tab', () => {
153+
afterEach(() => {
154+
scopeToPage(null);
155+
document.body.innerHTML = '';
156+
});
157+
158+
const call = (id: number, url: string, pageId?: string) => ({
159+
id,
160+
at: id,
161+
kind: 'load',
162+
method: 'GET',
163+
url,
164+
status: 200,
165+
ms: 1,
166+
from: 'browser',
167+
...(pageId ? { pageId } : {}),
168+
});
169+
const state = {
170+
pages: [page('A', '/a'), page('B', '/b')],
171+
calls: [call(1, '/from-a', 'A'), call(2, '/from-b', 'B'), call(3, '/untagged')],
172+
duplicates: [{ route: '/only-b', ssrAt: 1, browserAt: 2 }],
173+
};
174+
175+
it('shows only the panel tab\'s calls until "All tabs" is ticked', async () => {
176+
scopeToPage('A');
177+
const fixture = await render(client({ state }));
178+
const inspector = fixture.componentInstance;
179+
inspector.view.set('server');
180+
await settle(fixture);
181+
const root: HTMLElement = fixture.nativeElement;
182+
const urls = () => Array.from(root.querySelectorAll('td.request .url'), (e) => e.textContent);
183+
expect(urls()).toEqual(['/from-a']);
184+
// A warning about a route this tab never called stays hidden.
185+
expect(inspector.duplicates()).toEqual([]);
186+
187+
const toggle = root.querySelector<HTMLInputElement>('[data-testid="analog-all-tabs"]')!;
188+
expect(toggle.parentElement?.textContent).toContain('All tabs (including untagged)');
189+
toggle.click();
190+
await settle(fixture);
191+
expect(urls()).toEqual(['/untagged', '/from-b', '/from-a']);
192+
});
193+
194+
it('shows every call when no page reports', async () => {
195+
const fixture = await render(client({ state: { calls: state.calls } }));
196+
fixture.componentInstance.view.set('server');
197+
await settle(fixture);
198+
const root: HTMLElement = fixture.nativeElement;
199+
expect(root.querySelectorAll('td.request .url')).toHaveLength(3);
200+
expect(root.querySelector('[data-testid="analog-all-tabs"]')).toBeNull();
201+
});
202+
});

‎app/src/pages/analog-inspector.ts‎

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ interface AnalogCall {
8080
location?: string;
8181
seeded?: boolean;
8282
preview?: string;
83+
/** The tab the call came from, when the dev server could tell. */
84+
pageId?: string;
8385
}
8486

8587
interface AnalogPage {
@@ -356,7 +358,7 @@ function walk(routes: AnalogRoute[], depth = 0, out: { route: AnalogRoute; depth
356358
</div>
357359
<div class="stat">
358360
<span class="label">Server calls</span>
359-
<span class="value">{{ allCalls().length }}</span>
361+
<span class="value">{{ tabCalls().length }}</span>
360362
</div>
361363
<div class="stat" [attr.data-tone]="lintTone() || 'good'">
362364
<span class="label">Issues</span>
@@ -626,8 +628,19 @@ function walk(routes: AnalogRoute[], depth = 0, out: { route: AnalogRoute; depth
626628
/>
627629
Failed only
628630
</label>
631+
@if (scopePageId()) {
632+
<label class="check">
633+
<input
634+
type="checkbox"
635+
data-testid="analog-all-tabs"
636+
[checked]="allTabs()"
637+
(change)="allTabs.set($any($event.target).checked)"
638+
/>
639+
All tabs (including untagged)
640+
</label>
641+
}
629642
<span class="total" aria-live="polite"
630-
>{{ matchingCalls().length }} of {{ allCalls().length }}</span
643+
>{{ matchingCalls().length }} of {{ tabCalls().length }}</span
631644
>
632645
</div>
633646
@if (shownCount() < matchingCalls().length || serverFull()) {
@@ -720,14 +733,14 @@ function walk(routes: AnalogRoute[], depth = 0, out: { route: AnalogRoute; depth
720733
</tbody>
721734
</table>
722735
</div>
723-
} @else if (allCalls().length && callFiltered()) {
736+
} @else if (tabCalls().length && callFiltered()) {
724737
<div class="empty-box no-match" role="status">
725738
<p class="empty-lead">No calls match.</p>
726739
<p class="muted">
727740
@if (callQuery().trim()) {
728741
Nothing matches “{{ callQuery().trim() }}” with these filters.
729742
} @else {
730-
None of the {{ allCalls().length }} calls fit these filters.
743+
None of the {{ tabCalls().length }} calls fit these filters.
731744
}
732745
</p>
733746
<div class="empty-actions">
@@ -739,15 +752,21 @@ function walk(routes: AnalogRoute[], depth = 0, out: { route: AnalogRoute; depth
739752
} @else {
740753
<div class="empty-box">
741754
<p class="empty-lead">
742-
@if (allCalls().length) {
755+
@if (tabCalls().length) {
743756
No calls of this kind yet.
757+
} @else if (allCalls().length) {
758+
No calls from this tab yet.
744759
} @else {
745760
No calls yet.
746761
}
747762
</p>
748763
<p class="muted">
749764
Navigate in the app to see page renders, load() fetches, form actions, server
750765
functions and API calls.
766+
@if (!tabCalls().length && allCalls().length) {
767+
Tick “All tabs (including untagged)” to see the
768+
{{ allCalls().length }} calls from other tabs or tied to no tab.
769+
}
751770
</p>
752771
</div>
753772
}
@@ -2212,7 +2231,18 @@ export class AnalogInspector {
22122231
}
22132232
return open;
22142233
});
2234+
/** Every call the dev server keeps, from every tab. */
22152235
readonly allCalls = computed(() => this.state().calls ?? []);
2236+
/** The tab the Server view shows by default: the panel's page. */
2237+
readonly scopePageId = computed(() => this.page()?.pageId);
2238+
/** Shows the calls of every tab, and those the dev server tied to no tab. */
2239+
readonly allTabs = signal(false);
2240+
/** The calls of the panel's tab, or every call with "All tabs" on or no page reporting. */
2241+
readonly tabCalls = computed(() => {
2242+
const pageId = this.scopePageId();
2243+
const calls = this.allCalls();
2244+
return this.allTabs() || !pageId ? calls : calls.filter((c) => c.pageId === pageId);
2245+
});
22162246
readonly maxServerCalls = MAX_SERVER_CALLS;
22172247
readonly callQuery = signal('');
22182248
readonly failedOnly = signal(false);
@@ -2224,7 +2254,7 @@ export class AnalogInspector {
22242254
const kind = this.kind();
22252255
const query = this.callQuery().trim().toLowerCase();
22262256
const failedOnly = this.failedOnly();
2227-
return this.allCalls().filter(
2257+
return this.tabCalls().filter(
22282258
(c) =>
22292259
(kind === 'all' || c.kind === kind) &&
22302260
(!failedOnly || this.isFailed(c)) &&
@@ -2249,13 +2279,27 @@ export class AnalogInspector {
22492279
});
22502280
readonly serverFns = computed(() => this.project()?.serverFns ?? []);
22512281
private readonly fnById = computed(() => new Map(this.serverFns().map((fn) => [fn.id, fn])));
2282+
/** The routes and server functions the shown calls touch, so a warning about another tab stays hidden. */
2283+
private readonly shownRoutes = computed(
2284+
() => new Set(this.tabCalls().map((c) => `${c.kind} ${c.route ?? ''}`)),
2285+
);
22522286
readonly refetchedFns = computed(() =>
22532287
Array.from(
2254-
new Set((this.state().refetches ?? []).map((r) => this.fnById().get(r.id)?.name ?? r.id)),
2288+
new Set(
2289+
(this.state().refetches ?? [])
2290+
.filter((r) => this.shownRoutes().has(`fn ${r.id}`))
2291+
.map((r) => this.fnById().get(r.id)?.name ?? r.id),
2292+
),
22552293
),
22562294
);
22572295
readonly duplicates = computed(() =>
2258-
Array.from(new Set((this.state().duplicates ?? []).map((d) => d.route))),
2296+
Array.from(
2297+
new Set(
2298+
(this.state().duplicates ?? [])
2299+
.filter((d) => this.shownRoutes().has(`load ${d.route}`))
2300+
.map((d) => d.route),
2301+
),
2302+
),
22592303
);
22602304
readonly modeCounts = computed(() =>
22612305
(['ssr', 'ssg', 'cached', 'client', 'redirect'] as const)
@@ -2310,7 +2354,7 @@ export class AnalogInspector {
23102354
const { errors, warnings } = this.lintCount();
23112355
return [
23122356
{ id: 'routes' as View, label: 'Routes', count: this.pageCount(), tone: '' },
2313-
{ id: 'server' as View, label: 'Server', count: this.allCalls().length, tone: '' },
2357+
{ id: 'server' as View, label: 'Server', count: this.tabCalls().length, tone: '' },
23142358
{ id: 'render' as View, label: 'Render', count: this.renderRows().length, tone: '' },
23152359
{
23162360
id: 'content' as View,
@@ -2432,8 +2476,8 @@ export class AnalogInspector {
24322476

24332477
kindCount(kind: Kind): number {
24342478
return kind === 'all'
2435-
? this.allCalls().length
2436-
: this.allCalls().filter((c) => c.kind === kind).length;
2479+
? this.tabCalls().length
2480+
: this.tabCalls().filter((c) => c.kind === kind).length;
24372481
}
24382482

24392483
modeLabel(mode: RenderRow['mode']): string {
@@ -2450,7 +2494,7 @@ export class AnalogInspector {
24502494
}
24512495

24522496
fnCalls(id: string): number {
2453-
return this.allCalls().filter((c) => c.kind === 'fn' && c.route === id).length;
2497+
return this.tabCalls().filter((c) => c.kind === 'fn' && c.route === id).length;
24542498
}
24552499

24562500
outcomeLabel(outcome: ActionOutcome): string {

‎apps/docs/src/content/agents/tools.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -968,10 +968,10 @@ These tools cover *Analog apps. Most read your source. Some also read what the V
968968

969969
### The running page
970970

971-
| Tool | What it answers | Reads | Arguments |
972-
| -------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------- | ------------------------------------------------------------------ |
973-
| `analog-inspect-page` | The open page: its files (layouts first, then pages in named outlets with the outlet name), the `load()` data it received, server rendering and hydration state, and hydration errors. | page | none |
974-
| `analog-list-server-calls` | Recent page renders, `load()` fetches, form actions (with their outcome), server functions by name and API calls, with status, time and size. Flags a `load()` fetched twice and a seeded server function read called again. | Vite plugin | `kind` (`page`, `load`, `action`, `fn` or `api`), `route`, `limit` |
971+
| Tool | What it answers | Reads | Arguments |
972+
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
973+
| `analog-inspect-page` | The open page: its files (layouts first, then pages in named outlets with the outlet name), the `load()` data it received, server rendering and hydration state, and hydration errors. | page | none |
974+
| `analog-list-server-calls` | Recent page renders, `load()` fetches, form actions (with their outcome), server functions by name and API calls, with status, time and size. Flags a `load()` fetched twice and a seeded server function read called again. Each call names its tab when the dev server could tell. | Vite plugin | `kind` (`page`, `load`, `action`, `fn` or `api`), `route`, `limit`, `page` (only that tab's calls; leave it out for every call, untagged ones included) |
975975

976976
### Rendering
977977

‎apps/docs/src/content/inspectors/analog.md‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,15 @@ Type a URL into **Test a URL** and click **Explain** to see which files render i
5656

5757
Page renders (server rendered or client only), `load()` fetches, form actions, server functions and API calls, newest first. Each row shows the status, the time, who called it, and a redacted response preview.
5858

59-
| Control | What it does |
60-
| --------------------------------------- | ------------------------------------------------------------------------------------------------------ |
61-
| Kind filter | Show one kind of call. Each choice counts every call of that kind. |
62-
| **Find a URL, path or function** search | Keeps calls whose URL, route path, server function name or file contains the text. `Escape` clears it. |
63-
| **Failed only** | Keeps calls with a status of 400 or more, no status, or a form action that failed. |
64-
| **Clear calls** | Empties the list on the dev server. The tab says **Calls cleared.** or **Could not clear the calls.** |
59+
| Control | What it does |
60+
| --------------------------------------- | -------------------------------------------------------------------------------------------------------------------- |
61+
| Kind filter | Show one kind of call. Each choice counts the tab's calls of that kind, before the search and **Failed only**. |
62+
| **Find a URL, path or function** search | Keeps calls whose URL, route path, server function name or file contains the text. `Escape` clears it. |
63+
| **Failed only** | Keeps calls with a status of 400 or more, no status, or a form action that failed. |
64+
| **All tabs (including untagged)** | Shows the calls of every tab, and calls the dev server could not tie to a tab. Off by default. |
65+
| **Clear calls** | Empties the list on the dev server, for every tab. The tab says **Calls cleared.** or **Could not clear the calls.** |
66+
67+
The list shows the calls of the tab the panel belongs to (with no page id, the tab that reported last). **All tabs (including untagged)** appears while a page reports. See [Which tab a call belongs to](#which-tab-a-call-belongs-to).
6568

6669
The filters combine, and the count next to them reads like **12 of 80**. If nothing matches, click **Clear filters** to reset the search, **Failed only** and the kind.
6770

@@ -114,6 +117,18 @@ Checks grouped by rule, each with a fix. The count on the tab adds up errors and
114117
</ngmd-card>
115118
</ngmd-card-grid>
116119

120+
### Which tab a call belongs to
121+
122+
The dev server ties each call to a tab from the best source it has:
123+
124+
| Source | Calls it covers |
125+
| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
126+
| `x-pangular-page` header | Browser `HttpClient` calls, such as `load()` fetches on navigation and server functions. With `providePangularHttp()` or `withPangular()`, a dev build sends the tab's page id on requests to its own origin only. |
127+
| Render id | Page renders, and the `load()` and server function requests a render makes over HTTP. The Vite plugin sends the render an id in `Server-Timing` (`pangular-analog`), and the page reports it. Needs a secure context, such as `localhost`. |
128+
| `Referer` | Other requests, such as form actions sent with `fetch()`, when exactly one open tab shows that path. |
129+
130+
A call none of these covers stays untagged, and shows only with **All tabs (including untagged)** or in `analog-list-server-calls` without `page`. A tab's calls leave with the tab: when it closes or reloads, or 30 seconds after its last report or heartbeat.
131+
117132
### Supported layouts
118133

119134
- **Single app**: `package.json`, `vite.config.ts` and `src/app/pages` in one folder. The build output is read from `dist/analog/public`.
@@ -190,7 +205,7 @@ If an option comes from a variable, a spread (`...`), a template string or a com
190205
| `pangular:analog-scan-routes` | `filter` | File routes in match order, with page, layout and server files. |
191206
| `pangular:analog-scan-url` | `url` (required) | Which files render a URL, or the closest candidates. |
192207
| `pangular:analog-inspect-page` | `page` | The open page: its files, `load()` data, rendering and hydration state. |
193-
| `pangular:analog-list-server-calls` | `kind`, `route`, `limit` | Recent server calls, with server function names and action outcomes. |
208+
| `pangular:analog-list-server-calls` | `kind`, `route`, `limit`, `page` | Recent server calls, with server function names and action outcomes. |
194209
| `pangular:analog-scan-api-routes` | | Server routes with method, URL and file, plus middleware. |
195210
| `pangular:analog-scan-server-functions` | | Server functions with name, method, file, id and call counts. |
196211
| `pangular:analog-run-api` | `path` (required), `method`, `body`, `confirm` | Sends a real request to the dev server. |
@@ -213,7 +228,7 @@ Response previews and `load()` data redact secret-looking keys, tokens, `Bearer`
213228

214229
### Call history size
215230

216-
The server keeps the last 200 calls, and the **Server** tab renders the newest 150 that match its filters. The server cuts previews to 1000 characters, and page renders have no preview.
231+
The server keeps the last 200 calls of all tabs, and the **Server** tab renders the newest 150 that match its filters. The server cuts previews to 1000 characters, and page renders have no preview.
217232

218233
## FAQ
219234

0 commit comments

Comments
 (0)