Skip to content

feat(nextjs): Add URL to tags of server components and generation functions issues #16500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 16, 2025

Conversation

RulaKhaled
Copy link
Member

@RulaKhaled RulaKhaled commented Jun 6, 2025

Next.js’s uses an internal AsyncLocalStorage (ALS) to share minimal request context—it deliberately does not populate the full Request object or URL in ALS, so only headers (Referer, x-forwarded-host, x-forwarded-proto) are available at runtime in Server Components.

To still capture a usable URL for metadata we use a three-step best-effort fallback:

  1. Referer header
Use it as the fully qualified URL if present (note: this could be omitted by the browser).
  2. Proxy headers + route
Reconstruct protocol://host from x-forwarded-proto / x-forwarded-host (or host), then append the normalized component route with substituted [param] values. Query strings and fragments are TBD as query params fetched from args are always undefined for some reason, this needs investigation
  3. Span fallback
As a last resort, use the parent span’s http.target attribute (which contains the path and query) from the root HTTP span.
Screenshot 2025-06-02 at 4 04 31 PM

closes: https://linear.app/getsentry/issue/JS-487/capture-requesturl-for-nextjs-server-spans

@RulaKhaled RulaKhaled changed the title [WIP] feat(nextjs): Add URL to tags of server components and generation functions issues feat(nextjs): Add URL to tags of server components and generation functions issues Jun 10, 2025
@RulaKhaled RulaKhaled marked this pull request as ready for review June 10, 2025 10:32
Copy link
Member

@chargome chargome left a comment

Choose a reason for hiding this comment

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

LGTM! Could you also add a test case for a generation function in of the e2e tests?

@RulaKhaled RulaKhaled merged commit c4dcfa3 into develop Jun 16, 2025
269 of 270 checks passed
@RulaKhaled RulaKhaled deleted the add-url-metatag-to-server-issues branch June 16, 2025 14:50

const spanData = spanToJSON(rootSpan);

if (spanData.data && 'http.target' in spanData.data) {
Copy link
Member

Choose a reason for hiding this comment

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

l: this can be simplified to:

if (typeof spanData.data['http.target'] === 'string') {
  pathname = spanData.data['http.target'];
}

spanData.data should never be empty I believe, and we can just check for stringiness and then avoid the typecase below :)


const spanData = spanToJSON(rootSpan);

if (spanData.data && 'http.target' in spanData.data) {
Copy link
Member

Choose a reason for hiding this comment

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

same here as above :)

* @param params - Optional route parameters to replace in the template
* @param headersDict - Optional headers containing protocol and host information
* @param pathname - Optional pathname coming from parent span "http.target"
* @returns A sanitized URL string
Copy link
Member

Choose a reason for hiding this comment

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

l: I would always mention here if things are only exported for tests, for clarity. In this file, I believe only getSanitizedRequestUrl is actually used, all the other exports are for tests only right? By mentioning this with each export is is easier to follow why things are exported (vs. "hey can I un-export this because it is not actually used anyhwere?")

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.

3 participants