Skip to content

🐛 Prevent duplicate Next.js RUM views from discarded renders - #4940

Closed
BeltranBulbarellaDD wants to merge 16 commits into
mainfrom
beltran.bulbarella/next_js_render_issue
Closed

BeltranBulbarellaDD wants to merge 16 commits into
mainfrom
beltran.bulbarella/next_js_render_issue

Conversation

@BeltranBulbarellaDD

@BeltranBulbarellaDD BeltranBulbarellaDD commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4931.

Problem

DatadogAppRouter called startNextjsView() during React's render phase, guarded only by a useRef. React can discard and retry a render before it commits (e.g. Suspense, concurrent interruptions). Each retry got a fresh ref, so startNextjsView() fired again for renders that were later thrown away, creating several RUM views for one page.

There was also no reliable way to tell, once a render finally committed, whether a newer navigation had already started somewhere else in the meantime. The old guard compared pathnames, which breaks when a navigation returns to a pathname it had already visited.

Goal

Start a view as soon as navigation begins (so timing is accurate even for slow routes), but only ever create or rename a view from a render that actually commits — never from one that gets discarded or superseded.

Current behavior

  • Initial load: nextjsPlugin.onInit() starts one view immediately.
  • Navigation start: Next.js calls onRouterTransitionStart() before React renders the new route. This starts the view right away. Query/hash-only changes and duplicate callbacks for the same transition are filtered out.
  • Commit: DatadogAppRouter's useEffect calls setNextjsViewName() after React commits the route, normalizing the path (e.g. /user/42 -> /user/[id]). Each navigation bumps a generation counter; the effect only applies its rename if that generation is still the active one when it runs. If a newer navigation has started in the meantime, the commit is dropped instead of overwriting the view that's actually displayed now.

🤖 Generated with Claude Code

@datadog-datadog-prod-us1

datadog-datadog-prod-us1 Bot commented Aug 12, 2026 •

Copy link
Copy Markdown

Pipelines  Tests

✨ Unblock PR with BitsAI

❌ Errors

Your PR has failed checks. Please review the issues below and take necessary action before merging.

🚦 1 Pipeline job failed

DataDog/browser-sdk | format — 🔧 Needs a code fix, caused by this PR

View more details · View in GitLab

⚠️ Warnings

❄️ 1 New flaky test detected

↳ plugin: nextjs › should start a slow navigation view before the route commits from plugins/nextjsPlugin.scenario.ts

View in Flaky Test Management

ℹ️ Info

No other issues found (see more)

🧪 All tests passed

🎯 Code Coverage (details)
• Patch Coverage: 75.00%
• Overall Coverage: 77.18% (-0.00%)

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 497f026 | Docs | View more details | Give us feedback!

@BeltranBulbarellaDD BeltranBulbarellaDD changed the title test nextjs rendering issue 🐛 Prevent duplicate Next.js RUM views from discarded renders Aug 17, 2026
@BeltranBulbarellaDD
BeltranBulbarellaDD marked this pull request as ready for review August 19, 2026 15:16
@BeltranBulbarellaDD
BeltranBulbarellaDD requested a review from a team as a code owner August 19, 2026 15:16
Comment thread packages/browser-rum-nextjs/src/domain/nextJSRouter/useStartNextjsView.ts Outdated
@sbarrio
sbarrio requested a review from bdibon August 20, 2026 07:06
@BeltranBulbarellaDD
BeltranBulbarellaDD marked this pull request as draft August 27, 2026 09:49
@BeltranBulbarellaDD
BeltranBulbarellaDD removed the request for review from bdibon August 27, 2026 09:49
@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Aug 27, 2026 •

Copy link
Copy Markdown

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum Salesforce N/A 185.56 KiB N/A N/A N/A
Rum 185.54 KiB 185.54 KiB 0 B 0.00% ✅
Rum-salesforce Profiler N/A 8.38 KiB N/A N/A N/A
Rum Profiler 8.38 KiB 8.38 KiB 0 B 0.00% ✅
Rum-salesforce Recorder N/A 30.57 KiB N/A N/A N/A
Rum Recorder 30.57 KiB 30.57 KiB 0 B 0.00% ✅
Logs 59.00 KiB 59.00 KiB 0 B 0.00% ✅
Rum Slim 143.53 KiB 143.53 KiB 0 B 0.00% ✅
Worker 22.96 KiB 22.96 KiB 0 B 0.00% ✅
Rum Shopify N/A 214.59 KiB N/A N/A N/A
Rum-shopify Profiler N/A 8.38 KiB N/A N/A N/A
Rum-shopify Recorder N/A 4.77 KiB N/A N/A N/A

@BeltranBulbarellaDD
BeltranBulbarellaDD marked this pull request as ready for review August 28, 2026 14:11

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 31857be6e6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/browser-rum-nextjs/src/domain/nextJSRouter/datadogAppRouter.tsx Outdated
Comment thread packages/browser-rum-nextjs/src/domain/nextjsPlugin.ts Outdated
@sbarrio
sbarrio requested a review from bdibon September 1, 2026 07:04
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 8, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-24T16:59:45.475956Z 497f026 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 58fc0e9b68

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/browser-rum-nextjs/src/domain/nextjsPlugin.ts Outdated

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9e9ab88490

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/browser-rum-nextjs/src/domain/nextjsPlugin.ts
Comment thread packages/browser-rum-nextjs/src/domain/nextjsPlugin.ts Outdated

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e9c6d7cfe4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/browser-rum-nextjs/src/domain/nextjsPlugin.ts Outdated
@BeltranBulbarellaDD
BeltranBulbarellaDD requested review from lierniel and removed request for bdibon September 24, 2026 13:32

@lierniel lierniel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM 🚀

@BeltranBulbarellaDD
BeltranBulbarellaDD requested review from a team as code owners September 24, 2026 13:38
@BeltranBulbarellaDD
BeltranBulbarellaDD force-pushed the beltran.bulbarella/next_js_render_issue branch from d19f03a to ce58867 Compare September 24, 2026 13:42

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ce588679b4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/browser-rum-nextjs/src/domain/nextjsPlugin.ts Outdated

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cb890f9278

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/browser-rum-nextjs/src/domain/nextjsPlugin.ts Outdated
A layout-effect redirect that returns to an already-committed pathname
(e.g. / -> /protected -> router.replace('/')) could let /protected's
stale passive effect rename the restored view, because staleness was
inferred from pathname equality, which can't tell a revisited pathname
from a fresh one.

Track a monotonic generation counter per router transition instead.
DatadogAppRouter captures the active generation at render time, and
setNextjsViewName compares it at commit time, dropping the commit if a
newer transition has since started.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e8ec37b2dc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/browser-rum-nextjs/src/domain/nextjsPlugin.ts Outdated
Comment thread packages/browser-rum-nextjs/src/domain/nextjsPlugin.ts
Comment thread packages/browser-rum-nextjs/src/domain/nextjsPlugin.ts Outdated
BeltranBulbarellaDD and others added 3 commits September 24, 2026 18:37
The generation counter used to reject stale commits was a global clock
not scoped to any route, so a re-render of an old, still-mounted route
while a different navigation was pending could pick up a freshly-bumped
generation value alongside its own stale pathname, defeating the check.

Compare the commit's pathname directly against activeAppRouterPathname
instead — it catches the same stale-commit cases without needing any
counter.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Next.js forwards the raw href from router.push()/replace() unresolved,
which can be relative to the current page (e.g. '?sort=asc' or
'details') rather than root-relative. Resolving it against
window.location.origin instead of the current URL produced the wrong
path for these navigations.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
setNextjsViewName compared the committed pathname (from usePathname(),
which strips basePath) against activeAppRouterPathname (set from
window.location.pathname, which does not). On an app with a basePath
configured, these never matched, so the initial route's normalized
view name was never applied.

Pass window.location.pathname as the commit identity instead, keeping
usePathname() only for computing the normalized view name.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 497f026c2e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/browser-rum-nextjs/src/domain/nextjsPlugin.ts
@BeltranBulbarellaDD

Copy link
Copy Markdown
Contributor Author

Closing this one in favour of #5111

@github-actions github-actions Bot locked and limited conversation to collaborators Oct 6, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Router components create duplicate RUM views: render-phase startView() guarded only by useRef

2 participants