Skip to content

fix(nextjs): Include basePath in request url of Pages Router errors - #24985

Merged
chargome merged 2 commits into
developfrom
pages-router-requesturl
Oct 5, 2026
Merged

chargome merged 2 commits into
developfrom
pages-router-requesturl

Conversation

@chargome

@chargome chargome commented Oct 2, 2026

Copy link
Copy Markdown
Member

Next.js removes the basePath from req.url before running Pages Router data fetchers and API routes. So errors captured there reported a request.url without it, for example http://localhost:3000/foo/bar instead of http://localhost:3000/base/foo/bar.

Next keeps the URL as it was originally requested in its internal request meta (initURL), set before the basePath is removed. We now take the path and query from there. The origin still comes from the request headers, because Next builds the absolute initURL from its own hostname and port, which can differ from the public host behind a proxy. API routes were affected the same way, so they're covered too.

Fixes #24975

Next.js strips the basePath from req.url before running Pages Router data
fetchers and API routes, so captured errors reported a request url without it.
Use the originally requested path and query from Next.js' initURL request meta
instead, keeping the origin from the request headers.

Fixes #24975
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chargome

chargome commented Oct 2, 2026

Copy link
Copy Markdown
Member Author

bugbot run

@chargome chargome self-assigned this Oct 2, 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.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit ca14e8d. Configure here.

@chargome
chargome marked this pull request as ready for review October 2, 2026 11:37
@chargome
chargome requested a review from a team as a code owner October 2, 2026 11:37
@chargome
chargome requested review from logaretm, nicohrubec and s1gr1d and removed request for a team October 2, 2026 11:37
Comment thread packages/nextjs/src/common/utils/pagesRouterRequestToRequestData.ts Outdated
Simplify reading the original path and query from initURL with core's
parseUrl, and assert that streamed segment spans also carry the basePath
in url.full.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chargome
chargome requested a review from nicohrubec October 5, 2026 12:06

@nicohrubec nicohrubec left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nice

@chargome
chargome merged commit ad1d641 into develop Oct 5, 2026
352 of 353 checks passed
@chargome
chargome deleted the pages-router-requesturl branch October 5, 2026 12:20
chargome added a commit that referenced this pull request Oct 6, 2026
…rs (#25064)

Backport of: #24985

## Differences to the original PR

-
`packages/nextjs/src/common/pages-router-instrumentation/wrapApiHandlerWithSentry.ts`:
v10 still has the older API route wrapper that starts its own
`http.server` span, so the swap to `pagesRouterRequestToRequestData`
lands on the `normalizedRequest` used there instead of the inline
`setSDKProcessingMetadata` call. Because that span builds `url.full` and
`url.path` from the same request data, on v10 those span attributes also
include the `basePath` now.
-
`dev-packages/e2e-tests/test-applications/nextjs-15-basepath/tests/pages-router-request-url.test.ts`:
v10 does not stream spans by default, so the tests wait for the
`http.server` transaction and check its `request.url` instead of the
streamed segment span's `url.full`. The transaction's `request.url` has
no query string (it is in `query_string`), so the assertion leaves it
out.

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@sentry/nextjs: request.url of Pages Router data fetcher errors is missing basePath

2 participants