Repository navigation
Expand file tree
/
Copy pathcacheOrigin-pageloadConnection.spec.ts
More file actions
138 lines (101 loc) · 6.03 KB
/
Copy pathcacheOrigin-pageloadConnection.spec.ts
File metadata and controls
138 lines (101 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import type { Page } from '@playwright/test';
import { expect, test } from '@playwright/test';
import { getSpanOp, waitForStreamedSpan } from '@sentry-internal/test-utils';
const APP = 'nextjs-16-streaming-cacheComponents';
// The server only writes `sentry-trace` meta tags for a render that happened for the current request,
// never while it prerenders a shell. So a tag in the document can always be continued; the only
// question is when it arrives. The resumed part of a prerendered shell arrives after the SDK started,
// which the pageload instrumentation waits for.
test('connects the pageload trace to the runtime server request', async ({ page }) => {
// The shell is prerendered, but the page's dynamic part (`DynamicContent` awaits `headers()`)
// runs on the server on every request and produces the `GET /pageload-tracing` root span.
const serverSpanPromise = waitForStreamedSpan(APP, span => {
return span.name === 'GET /pageload-tracing' && getSpanOp(span) === 'http.server' && span.is_segment;
});
const pageloadSpanPromise = waitForStreamedSpan(APP, span => {
return span.name === '/pageload-tracing' && getSpanOp(span) === 'pageload' && span.is_segment;
});
await page.goto('/pageload-tracing');
await expect(page.locator('#todos-fetched')).toHaveText('Todos fetched: 5');
const [serverSpan, pageloadSpan] = await Promise.all([serverSpanPromise, pageloadSpanPromise]);
expect(serverSpan.trace_id).toBeTruthy();
expect(pageloadSpan.trace_id).toBe(serverSpan.trace_id);
expect(pageloadSpan.links).toBeUndefined();
});
test('connects the pageload trace to a blocking render', async ({ page }) => {
const serverSpanPromise = waitForStreamedSpan(APP, span => {
return span.name === 'GET /blocking' && getSpanOp(span) === 'http.server' && span.is_segment;
});
const pageloadSpanPromise = waitForStreamedSpan(APP, span => {
return span.name === '/blocking' && getSpanOp(span) === 'pageload' && span.is_segment;
});
await page.goto('/blocking');
await expect(page.locator('#blocking')).toHaveText('Blocking render for a browser');
const [serverSpan, pageloadSpan] = await Promise.all([serverSpanPromise, pageloadSpanPromise]);
// The whole page renders per request, so the tag in <head> is this request's and is continued.
expect(serverSpan.trace_id).toBeTruthy();
expect(pageloadSpan.trace_id).toBe(serverSpan.trace_id);
expect(pageloadSpan.links).toBeUndefined();
});
test('does not share a trace between visitors of a shell generated at request time', async ({ browser }) => {
test.skip(process.env.TEST_ENV !== 'production', 'Shells are only cached in production builds');
// `[id]` only lists `test` in `generateStaticParams`, so the first visit prerenders and caches the
// shell for this id. The prerender writes no trace tag, so every visitor starts a fresh trace.
const id = `shell-${Date.now()}`;
const visit = async (): Promise<{ traceId: string | undefined; links: unknown }> => {
const context = await browser.newContext();
const page: Page = await context.newPage();
const pageloadSpanPromise = waitForStreamedSpan(APP, span => {
return getSpanOp(span) === 'pageload' && span.is_segment && span.attributes['url.path']?.value === `/${id}`;
});
await page.goto(`/${id}`);
await expect(page.locator('#result')).toHaveText(`Hello, ${id}!`);
expect(await page.locator('meta[name="sentry-trace"]').count()).toBe(0);
const pageloadSpan = await pageloadSpanPromise;
await context.close();
return { traceId: pageloadSpan.trace_id, links: pageloadSpan.links };
};
const first = await visit();
const second = await visit();
expect(first.traceId).toBeTruthy();
expect(second.traceId).toBeTruthy();
expect(second.traceId).not.toBe(first.traceId);
expect(first.links).toBeUndefined();
expect(second.links).toBeUndefined();
});
test('starts a fresh trace on a fully static page', async ({ page }) => {
test.skip(process.env.TEST_ENV !== 'production', 'Pages are only prerendered in production builds');
const pageloadSpanPromise = waitForStreamedSpan(APP, span => {
return span.name === '/static-shell' && getSpanOp(span) === 'pageload' && span.is_segment;
});
await page.goto('/static-shell');
await expect(page.locator('#static-shell')).toHaveText('Static shell');
const pageloadSpan = await pageloadSpanPromise;
// A build-time shell carries no trace context at all.
expect(await page.locator('meta[name="sentry-trace"]').count()).toBe(0);
expect(pageloadSpan.trace_id).toBeTruthy();
expect(pageloadSpan.links).toBeUndefined();
});
test('keeps a navigation that starts while the pageload waits for the trace meta tag', async ({ page }) => {
test.skip(process.env.TEST_ENV !== 'production', 'Only a prerendered shell makes the trace meta tag arrive late');
const pageloadSpanPromise = waitForStreamedSpan(APP, span => {
return span.name === '/slow-hole' && getSpanOp(span) === 'pageload' && span.is_segment;
});
const navigationSpanPromise = waitForStreamedSpan(APP, span => {
return span.name === '/static-shell' && getSpanOp(span) === 'navigation' && span.is_segment;
});
await page.goto('/slow-hole', { waitUntil: 'commit' });
await page.waitForFunction(() => !!(window as { next?: { router?: unknown } }).next?.router);
// The resumed part, which carries the tag, has not arrived yet, so the pageload is still waiting.
await expect(page.locator('#slow-fallback')).toBeVisible();
expect(await page.locator('meta[name="sentry-trace"]').count()).toBe(0);
await page.locator('#navigate').click();
await expect(page.locator('#static-shell')).toHaveText('Static shell');
const [pageloadSpan, navigationSpan] = await Promise.all([pageloadSpanPromise, navigationSpanPromise]);
// The navigation ends the pageload like any pageload it interrupts, and runs to its own idle timeout.
expect(pageloadSpan.attributes['sentry.idle_span_finish_reason']).toEqual({ type: 'string', value: 'cancelled' });
expect(navigationSpan.attributes['sentry.idle_span_finish_reason']).toEqual({
type: 'string',
value: 'idleTimeout',
});
});