Skip to content

Commit b221c4e

Browse files
chargomeclaude
andcommitted
feat(astro): Send the render start of a prerendered page to the browser
Use the renamed _INTERNAL_getCacheOriginMetaTags, which also writes sentry-render-start, so the pageload gets page.server_rendering.age like Next.js, Nuxt and SvelteKit. Refs JSSDK-38 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b04f5c8 commit b221c4e

3 files changed

Lines changed: 24 additions & 9 deletions

File tree

‎dev-packages/e2e-tests/test-applications/astro-7/tests/tracing.static.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ test.describe('tracing in static/pre-rendered routes', () => {
3737
'sentry.segment.name.source': { value: 'route', type: 'string' },
3838
'page.server_rendering.type': { value: 'static', type: 'string' },
3939
});
40+
// The build rendered the page, so the age is the time since the build.
41+
const renderStart = Number(await page.locator('meta[name="sentry-render-start"]').getAttribute('content'));
42+
expect(clientPageloadSpan.attributes['page.server_rendering.age']).toEqual({
43+
type: 'integer',
44+
value: Math.max(0, Math.floor(clientPageloadSpan.start_timestamp - renderStart)),
45+
});
4046
// The build sends no spans, so there is no render to link.
4147
expect(clientPageloadSpan.links).toBeUndefined();
4248

‎packages/astro/src/server/middleware.ts‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
setHttpStatus,
3232
spanToJSON,
3333
startSpan,
34+
timestampInSeconds,
3435
winterCGHeadersToDict,
3536
winterCGRequestToRequestData,
3637
withIsolationScope,
@@ -39,7 +40,7 @@ import {
3940
} from '@sentry/core';
4041
import {
4142
flushIfServerless,
42-
_INTERNAL_getCacheOriginMetaTag,
43+
_INTERNAL_getCacheOriginMetaTags,
4344
_INTERNAL_handleServedTunnelRequest as handleServedTunnelRequest,
4445
} from '@sentry/core/server';
4546
import { setHttpServerSpanRouteAttribute } from '@sentry/server-utils';
@@ -113,10 +114,11 @@ export const handleRequest: (options?: MiddlewareOptions) => MiddlewareHandler =
113114

114115
async function handleStaticRoute(ctx: APIContext, next: MiddlewareNext): Promise<Response> {
115116
const parametrizedRoute = getParametrizedRoute(ctx);
117+
const requestStart = timestampInSeconds();
116118
try {
117119
const originalResponse = await next();
118120

119-
const metaTagsStr = getMetaTagsStr({ isPrerendered: true, parametrizedRoute });
121+
const metaTagsStr = getMetaTagsStr({ prerenderStart: requestStart, parametrizedRoute });
120122
return injectMetaTagsInResponse(originalResponse, metaTagsStr);
121123
} catch (e) {
122124
sendErrorToSentry(e);
@@ -154,7 +156,7 @@ async function enhanceHttpServerSpan(ctx: APIContext, next: MiddlewareNext, root
154156

155157
try {
156158
const originalResponse = await next();
157-
const metaTagsStr = getMetaTagsStr({ isPrerendered: false, parametrizedRoute });
159+
const metaTagsStr = getMetaTagsStr({ parametrizedRoute });
158160
return injectMetaTagsInResponse(originalResponse, metaTagsStr);
159161
} catch (e) {
160162
sendErrorToSentry(e);
@@ -256,7 +258,7 @@ async function instrumentRequestStartHttpServerSpan(
256258
setHttpStatus(span, originalResponse.status);
257259
}
258260

259-
const metaTagsStr = getMetaTagsStr({ isPrerendered: false, parametrizedRoute });
261+
const metaTagsStr = getMetaTagsStr({ parametrizedRoute });
260262
return injectMetaTagsInResponse(originalResponse, metaTagsStr);
261263
} catch (e) {
262264
sendErrorToSentry(e);
@@ -294,16 +296,21 @@ function addMetaTagToHead(htmlChunk: string, metaTagsStr: string): string {
294296
});
295297
}
296298

299+
/**
300+
* `prerenderStart` is set for a prerendered page, in seconds. Every visitor gets the same prerendered HTML, so it
301+
* must not carry the trace of the render. The empty `sentry-cache-origin` tag marks the pageload as static without a
302+
* link, because the build sends no spans.
303+
*/
297304
function getMetaTagsStr({
298-
isPrerendered,
305+
prerenderStart,
299306
parametrizedRoute,
300307
}: {
301-
isPrerendered: boolean;
308+
prerenderStart?: number;
302309
parametrizedRoute: string | undefined;
303310
}): string {
304-
// Every visitor gets the same prerendered HTML, so it must not carry the trace of the render. The empty
305-
// `sentry-cache-origin` tag marks the pageload as static without a link, because the build sends no spans.
306-
const parts = [isPrerendered ? _INTERNAL_getCacheOriginMetaTag(undefined) : getTraceMetaTags()];
311+
const parts = [
312+
prerenderStart === undefined ? getTraceMetaTags() : _INTERNAL_getCacheOriginMetaTags(undefined, prerenderStart),
313+
];
307314
if (parametrizedRoute) {
308315
parts.push(`<meta name="sentry-route-name" content="${encodeURIComponent(parametrizedRoute)}"/>`);
309316
}

‎packages/astro/test/server/middleware.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ describe('sentryMiddleware', () => {
464464
// parametrized route is injected
465465
expect(html).toContain('<meta name="sentry-route-name" content="%2Fusers"/>');
466466
expect(html).toContain('<meta name="sentry-cache-origin" content=""/>');
467+
expect(html).toMatch(/<meta name="sentry-render-start" content="\d+"\/>/);
467468
expect(html).not.toContain('<meta name="sentry-trace" content="');
468469
expect(html).not.toContain('<meta name="baggage" content="');
469470
});
@@ -551,6 +552,7 @@ describe('sentryMiddleware', () => {
551552
expect(html).toContain('<meta name="sentry-trace" content="');
552553
expect(html).toContain('<meta name="baggage" content="');
553554
expect(html).not.toContain('sentry-cache-origin');
555+
expect(html).not.toContain('sentry-render-start');
554556
});
555557

556558
it('preserves response status, statusText, and all headers when injecting meta tags', async () => {

0 commit comments

Comments
 (0)