Skip to content

fix(astro): Initialize the server SDK on the first endpoint request - #25731

Open
AyeshaWaseemAhmad wants to merge 1 commit into
getsentry:developfrom
AyeshaWaseemAhmad:fix/astro-init-sdk-before-first-endpoint-request
Open

AyeshaWaseemAhmad wants to merge 1 commit into
getsentry:developfrom
AyeshaWaseemAhmad:fix/astro-init-sdk-before-first-endpoint-request

Conversation

@AyeshaWaseemAhmad

Copy link
Copy Markdown

The server init was only imported through injectScript('page-ssr', ...), which Astro adds to page modules but not to API endpoints. On a server that has not rendered a page yet, getClient() is undefined, so the request middleware skips the request and errors thrown by endpoints are dropped.

The middleware is now registered through a virtual module that imports the server init before re-exporting onRequest from @sentry/astro/middleware. With a server init file, the init is that file, which page-ssr imports as well. With the default generated snippet, the snippet lives in a virtual module that both the page-ssr injection and the middleware import. Either way the module is shared, so Sentry.init() still runs once, ahead of a page's own imports.

On a fresh Node-adapter build the first request to an endpoint now runs the init and the error is captured, on Astro 4, 5, 6 and 7 (also in astro dev on 7, on @astrojs/vercel serverless and on Cloudflare Pages). Before, nothing was captured until a page had rendered.

Cloudflare Workers is unchanged, because the Worker wrapper already initializes the SDK for every request. Netlify is unchanged too: its optional edge middleware is bundled into a separate Deno function and can't be detected here, and running the init there throws. Not covered: Netlify, autoInstrumentation.requestHandler: false, and errors thrown before the first request. An init file that throws at import time now fails endpoint requests as well.

Fixes #25729

The server init was only imported through injectScript('page-ssr'),
which Astro adds to page modules but not to API endpoints. On a server
that had not rendered a page yet, getClient() was undefined, so the
request middleware skipped the request and errors thrown by endpoints
were dropped.

The middleware is now registered through a virtual module that imports
the server init before re-exporting onRequest from
@sentry/astro/middleware. With a server init file, the init is that
file, which the page-ssr injection imports as well. With the default
generated snippet, the snippet lives in a virtual module that both the
page-ssr injection and the middleware import. Either way the module is
shared, so Sentry.init() still runs once and still runs ahead of a
page's own imports.

Cloudflare Workers is unchanged, because the Worker wrapper already
initializes the SDK for every request. Netlify is unchanged too: its
optional edge middleware is bundled separately and cannot be detected
here, and running the init there fails on Deno.

Fixes getsentry#25729
@AyeshaWaseemAhmad
AyeshaWaseemAhmad requested a review from a team as a code owner October 25, 2026 01:24
@AyeshaWaseemAhmad
AyeshaWaseemAhmad requested review from chargome and nicohrubec and removed request for a team October 25, 2026 01:24
@github-actions github-actions Bot added the external PR from an external contributor label Oct 25, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit aa825c3. Configure here.

} finally {
wranglerPagesConfig = undefined;
}
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conditional assertion in parameterized test

Low Severity

The Cloudflare Pages it.each still picks the expected page-ssr script with a ternary on serverInitPath. Nearby Netlify cases already put that expected script in the table. This was flagged because the review rules ask to split conditional paths instead of branching inside one test.

Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot

Reviewed by Cursor Bugbot for commit aa825c3. Configure here.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external PR from an external contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: sentry for Astro integration does NOT capture errors on API routes

1 participant