Skip to content

Commit 1d796de

Browse files
committed
fix(components): age defer blocks on the page clock and show their notes in the panel
A defer block was aged with the server's clock against a time taken on the page's, so a page whose clock ran ahead or behind showed wrong ages. Each component push now carries the page clock, and the server ages blocks by the offset. The panel's defer rows show the same notes the agent gets (failed, stuck, dehydrated, error block) and the placeholder and loading times. A block with several root elements is highlighted with one box around all of them, and the SSR and HTTP scan names a hydration mismatch by its component class instead of the DOM node. Refs #366
1 parent 445934c commit 1d796de

23 files changed

Lines changed: 803 additions & 223 deletions

‎app/src/__tests__/component-tree-defer.test.ts‎

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { TestBed } from '@angular/core/testing';
22
import type { DevframeRpcClient } from 'devframe/client';
3-
import { afterEach, describe, expect, it } from 'vitest';
3+
import { afterEach, describe, expect, it, vi } from 'vitest';
44
import { ComponentTree } from '../pages/component-tree';
55

6-
function client(pages: Record<string, unknown>): DevframeRpcClient {
6+
function client(
7+
pages: Record<string, unknown>,
8+
call: (...args: unknown[]) => Promise<unknown> = () => Promise.resolve([]),
9+
): DevframeRpcClient {
710
const rpc = {
8-
call: () => Promise.resolve([]),
11+
call,
912
callEvent: () => Promise.resolve(),
1013
sharedState: (name: string) =>
1114
Promise.resolve({
@@ -120,4 +123,108 @@ describe('defer blocks', () => {
120123
expect(await pill(page())).toBe('2');
121124
expect(await pill(page(true))).toBe('2+');
122125
});
126+
127+
it('shows the notes the agent tool prints, the @error block and the block times', async () => {
128+
const hour = 3_600_000;
129+
// The page's clock runs an hour ahead of the panel's.
130+
const pageNow = Date.now() + hour;
131+
const block = (id: string, extra: Record<string, unknown>) => ({
132+
id,
133+
owner: { id: 'c1', name: 'App', tag: 'app-root' },
134+
state: 'complete',
135+
hydration: 'not-configured',
136+
triggers: ['on idle'],
137+
rootIds: [],
138+
since: pageNow,
139+
hasErrorBlock: false,
140+
...extra,
141+
});
142+
const page = {
143+
pageId: 'p1',
144+
roots: [{ id: 'c1', name: 'App', tag: 'app-root', children: [] }],
145+
count: 1,
146+
detail: null,
147+
reportedAt: 1000,
148+
clockOffset: -hour,
149+
deferBlocks: [
150+
block('d1', { state: 'error', hasErrorBlock: true }),
151+
block('d2', { state: 'placeholder', since: pageNow - 30_000 }),
152+
block('d3', { state: 'placeholder', since: pageNow - 2_000 }),
153+
block('d4', { hydration: 'dehydrated', triggers: ['hydrate on viewport'] }),
154+
block('d5', {
155+
state: 'loading',
156+
loading: { minimumTime: 1000, afterTime: 100 },
157+
placeholder: { minimumTime: 500 },
158+
}),
159+
],
160+
};
161+
const fixture = TestBed.createComponent(ComponentTree);
162+
fixture.componentRef.setInput('rpc', client({ p1: page }));
163+
for (let i = 0; i < 3; i++) {
164+
await new Promise((resolve) => setTimeout(resolve));
165+
await fixture.whenStable();
166+
}
167+
const rows = Array.from(
168+
(fixture.nativeElement as HTMLElement).querySelectorAll<HTMLElement>('.defer-row'),
169+
);
170+
const flags = (row: HTMLElement) =>
171+
Array.from(row.querySelectorAll('.flag')).map((el) => el.textContent?.trim());
172+
expect(flags(rows[0]!)).toEqual(['failed', '@error block']);
173+
expect(rows[0]!.querySelector('.note-failed')?.getAttribute('title')).toBe('failed to load');
174+
// 30 s on the page's clock, not an hour and 30 s.
175+
expect(flags(rows[1]!)).toEqual(['stuck 30s']);
176+
expect(rows[1]!.querySelector('.note-stuck')?.getAttribute('title')).toBe(
177+
'on its placeholder for 30s',
178+
);
179+
expect(flags(rows[2]!)).toEqual([]);
180+
expect(flags(rows[3]!)).toEqual(['dehydrated']);
181+
expect(rows[4]!.querySelector('.timings')?.textContent?.trim()).toBe(
182+
'placeholder min 500ms · loading after 100ms, min 1s',
183+
);
184+
expect(rows[0]!.querySelector('.timings')).toBeNull();
185+
fixture.destroy();
186+
});
187+
188+
it('highlights every root element of a block in one request', async () => {
189+
const call = vi.fn((..._args: unknown[]) => Promise.resolve([] as unknown));
190+
const block = (id: string, rootIds: string[]) => ({
191+
id,
192+
owner: { id: 'c1', name: 'App', tag: 'app-root' },
193+
state: 'complete',
194+
hydration: 'not-configured',
195+
triggers: [],
196+
rootIds,
197+
});
198+
const page = {
199+
pageId: 'p1',
200+
roots: [{ id: 'c1', name: 'App', tag: 'app-root', children: [] }],
201+
count: 1,
202+
detail: null,
203+
reportedAt: 1000,
204+
deferBlocks: [block('d1', ['e1', 'e2']), block('d2', ['e3']), block('d3', [])],
205+
};
206+
const fixture = TestBed.createComponent(ComponentTree);
207+
fixture.componentRef.setInput('rpc', client({ p1: page }, call));
208+
for (let i = 0; i < 3; i++) {
209+
await new Promise((resolve) => setTimeout(resolve));
210+
await fixture.whenStable();
211+
}
212+
const rows = (fixture.nativeElement as HTMLElement).querySelectorAll('.defer-row');
213+
const highlights = () =>
214+
call.mock.calls.filter((args) => args[0] === 'request-page-highlight').map((args) => args[1]);
215+
call.mockClear();
216+
for (const row of Array.from(rows)) {
217+
row.dispatchEvent(new Event('mouseenter'));
218+
row.dispatchEvent(new Event('mouseleave'));
219+
}
220+
expect(highlights()).toEqual([
221+
{ pageId: 'p1', ids: ['e1', 'e2'] },
222+
null,
223+
{ pageId: 'p1', id: 'e3' },
224+
null,
225+
{ pageId: 'p1', id: 'c1' },
226+
null,
227+
]);
228+
fixture.destroy();
229+
});
123230
});

‎app/src/pages/component-tree.ts‎

Lines changed: 22 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import { ValueTree } from './component-value-tree';
5050
import { ValueStore } from './component-value-store';
5151
import { nestedMatches } from './component-value-state';
5252
import { StoreGlobal } from '../ui/store-global';
53+
import { DeferBlocksSection, type DeferBlock } from './defer-blocks-section';
5354
import { ForBlocksSection, type ForBlock } from './for-blocks-section';
5455
import { LeaksSection, type LeaksPage } from './leaks-section';
5556
import { VirtualWindow } from '../ui/virtual-window';
@@ -115,16 +116,6 @@ interface Detail {
115116
expanded?: Record<string, unknown>;
116117
}
117118

118-
interface DeferBlock {
119-
id: string;
120-
owner?: { id: string; name: string; tag: string };
121-
state: string;
122-
hydration: string;
123-
hydrateNever?: boolean;
124-
triggers: string[];
125-
rootIds: string[];
126-
}
127-
128119
interface Page {
129120
pageId: string;
130121
url?: string;
@@ -144,6 +135,8 @@ interface Page {
144135
truncatedBy?: { components?: number; depth?: number; directiveHosts?: number };
145136
detail: Detail | null;
146137
reportedAt: number;
138+
/** The server clock minus the page clock, in ms, for ages of page times. */
139+
clockOffset?: number;
147140
}
148141

149142
interface OutletInfo {
@@ -204,6 +197,7 @@ function storedDirectiveHosts(): boolean {
204197
SourceLink,
205198
ValueTree,
206199
StoreGlobal,
200+
DeferBlocksSection,
207201
ForBlocksSection,
208202
HydrationSection,
209203
LeaksSection,
@@ -759,43 +753,13 @@ function storedDirectiveHosts(): boolean {
759753
}
760754
</div>
761755
762-
@if (deferBlocks().length) {
763-
<section class="defer" aria-labelledby="ct-defer-title">
764-
<h2 id="ct-defer-title" class="defer-title">
765-
Defer blocks
766-
<span class="pill">{{ deferCount() }}</span>
767-
</h2>
768-
<ul class="defer-list">
769-
@for (block of deferBlocks(); track block.id) {
770-
<li>
771-
<button
772-
type="button"
773-
class="defer-row"
774-
[attr.aria-disabled]="block.owner ? null : 'true'"
775-
(click)="showOwner(block)"
776-
(focus)="highlightBlock(block)"
777-
(blur)="highlight(null)"
778-
(mouseenter)="highlightBlock(block)"
779-
(mouseleave)="highlight(null)"
780-
>
781-
<span class="name mono">{{ block.owner?.name ?? 'Unknown component' }}</span>
782-
<span class="defer-state" [class]="'defer-state-' + block.state">{{
783-
block.state
784-
}}</span>
785-
@if (block.hydrateNever) {
786-
<span class="flag">hydrate never</span>
787-
} @else if (block.hydration !== 'not-configured') {
788-
<span class="flag">{{ block.hydration }}</span>
789-
}
790-
<span class="triggers mono">{{
791-
block.triggers.join(', ') || 'no triggers'
792-
}}</span>
793-
</button>
794-
</li>
795-
}
796-
</ul>
797-
</section>
798-
}
756+
<app-defer-blocks
757+
[blocks]="deferBlocks()"
758+
[truncated]="page()?.deferTruncated === true"
759+
[clockOffset]="page()?.clockOffset ?? 0"
760+
(showOwner)="showOwner($event)"
761+
(highlight)="$event ? highlightBlock($event) : highlight(null)"
762+
/>
799763
@if (deferUnavailable()) {
800764
<app-angular-unavailable
801765
class="defer"
@@ -1131,58 +1095,6 @@ function storedDirectiveHosts(): boolean {
11311095
.defer {
11321096
margin-top: 16px;
11331097
}
1134-
.defer-title {
1135-
@include m.label;
1136-
display: flex;
1137-
align-items: center;
1138-
gap: 6px;
1139-
margin: 0 0 8px;
1140-
}
1141-
.defer-list {
1142-
display: grid;
1143-
gap: 6px;
1144-
margin: 0;
1145-
padding: 0;
1146-
list-style: none;
1147-
}
1148-
.defer-row {
1149-
justify-content: flex-start;
1150-
flex-wrap: wrap;
1151-
width: 100%;
1152-
height: auto;
1153-
min-height: var(--control-h);
1154-
padding: 6px 12px;
1155-
background: var(--surface);
1156-
border-color: var(--border);
1157-
text-align: left;
1158-
}
1159-
.defer-row[aria-disabled='true'] {
1160-
cursor: default;
1161-
}
1162-
.defer-state {
1163-
padding: 0 8px;
1164-
border-radius: 99px;
1165-
background: var(--surface-3);
1166-
color: var(--text-2);
1167-
font-size: 11px;
1168-
line-height: 18px;
1169-
}
1170-
.defer-state-complete {
1171-
@include m.tint(ok);
1172-
}
1173-
.defer-state-error {
1174-
@include m.tint(danger);
1175-
}
1176-
.defer-state-loading,
1177-
.defer-state-placeholder {
1178-
@include m.tint(warn);
1179-
}
1180-
.triggers {
1181-
min-width: 0;
1182-
color: var(--text-2);
1183-
font-size: 12px;
1184-
@include m.truncate;
1185-
}
11861098
.pick-message {
11871099
margin: 0 0 12px;
11881100
color: var(--text-2);
@@ -1929,10 +1841,6 @@ export class ComponentTree {
19291841
);
19301842

19311843
readonly deferBlocks = computed(() => this.page()?.deferBlocks ?? []);
1932-
/** The block count, with a `+` when the page has more blocks than it reported. */
1933-
readonly deferCount = computed(
1934-
() => `${this.deferBlocks().length}${this.page()?.deferTruncated ? '+' : ''}`,
1935-
);
19361844

19371845
readonly countLabel = computed(() =>
19381846
countText(
@@ -2645,19 +2553,24 @@ export class ComponentTree {
26452553
}
26462554

26472555
highlight(id: string | null) {
2556+
this.requestHighlight(id ? { id } : null);
2557+
}
2558+
2559+
/** One box around every root element of the block, or its owner when none is rendered. */
2560+
highlightBlock(block: DeferBlock) {
2561+
if (block.rootIds.length > 1) this.requestHighlight({ ids: block.rootIds });
2562+
else this.highlight(block.rootIds[0] ?? block.owner?.id ?? null);
2563+
}
2564+
2565+
private requestHighlight(target: { id: string } | { ids: string[] } | null) {
26482566
const client = this.rpc();
26492567
if (!client) return;
2650-
const target = id ? { pageId: this.page()?.pageId, id } : null;
26512568
void client
26522569
.scope('pangular')
2653-
.rpc.call('request-page-highlight', target)
2570+
.rpc.call('request-page-highlight', target && { pageId: this.page()?.pageId, ...target })
26542571
.catch(() => {});
26552572
}
26562573

2657-
highlightBlock(block: DeferBlock) {
2658-
this.highlight(block.rootIds[0] ?? block.owner?.id ?? null);
2659-
}
2660-
26612574
showOwner(block: DeferBlock) {
26622575
if (block.owner && this.index().map.has(block.owner.id)) this.reveal(block.owner.id);
26632576
}

0 commit comments

Comments
 (0)