Skip to content

✨ NextJS- add nextjs error boundary component#4352

Open
BeltranBulbarellaDD wants to merge 10 commits intomainfrom
beltran.bulbarella/nextjs_pages__router_error
Open

✨ NextJS- add nextjs error boundary component#4352
BeltranBulbarellaDD wants to merge 10 commits intomainfrom
beltran.bulbarella/nextjs_pages__router_error

Conversation

@BeltranBulbarellaDD
Copy link
Contributor

@BeltranBulbarellaDD BeltranBulbarellaDD commented Mar 17, 2026

Motivation

Adds React error boundary to NextJS pages router.

Changes

Adds ErrorBoundary — a React error boundary component that wraps createErrorBoundary from @datadog/browser-rum-react, pre-wired to call addNextjsError.

Exports ErrorBoundaryFallback and ErrorBoundaryProps types alongside it.

Also adds @datadog/browser-rum-react as an optional peer dependency of rum-nextjs, and allows @datadog/browser-rum-react/internal through the disallow-side-effects ESLint rule.

Test instructions

Test app — new pages for both routers to exercise all error paths:

Scenario Implementation
Pages Router client error ErrorBoundary in pages-router/error-test.tsx
App Router client error error.tsx segment boundary calling addNextjsError
App Router server error (with digest) Server component throw → error.tsx calling addNextjsError
App Router global error Root-level throw → global-error.tsx calling addNextjsError

Checklist

  • Tested locally
  • Tested on staging
  • Added unit tests for this change.
  • Added e2e/integration tests for this change.
  • Updated documentation and/or relevant AGENTS.md file

@datadog-official
Copy link

datadog-official bot commented Mar 17, 2026

✅ Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

🎯 Code Coverage (details)
Patch Coverage: 100.00%
Overall Coverage: 77.24% (-0.01%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 6d7f023 | Docs | Datadog PR Page | Was this helpful? React with 👍/👎 or give us feedback!

@cit-pr-commenter-54b7da
Copy link

cit-pr-commenter-54b7da bot commented Mar 17, 2026

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 175.02 KiB 175.02 KiB 0 B 0.00%
Rum Profiler 6.16 KiB 6.16 KiB 0 B 0.00%
Rum Recorder 27.46 KiB 27.46 KiB 0 B 0.00%
Logs 56.80 KiB 56.80 KiB 0 B 0.00%
Rum Slim 130.66 KiB 130.66 KiB 0 B 0.00%
Worker 23.63 KiB 23.63 KiB 0 B 0.00%
🚀 CPU Performance

Pending...

🧠 Memory Performance

Pending...

🔗 RealWorld

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since all these tests are the same for rum-react (except for the last one) should I remove them?

"build:docs:json": "typedoc --logLevel Verbose --json ./docs.json",
"build:docs:html": "typedoc --out ./docs",
"pack": "yarn workspaces foreach --all --parallel --include '@datadog/*' exec yarn pack",
"pack": "yarn workspaces foreach --all --parallel --topological --include '@datadog/*' exec yarn pack",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is required because rum-nextjs needs rum-react

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about this... rum depends on rum-code which depends on core and that was not an issue before.

stdout: 'pipe' as const,
cwd: path.join(__dirname, '../apps/nextjs'),
command: isLocal ? 'yarn dev' : 'yarn start',
command: 'yarn start',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is for 2 reasons:

  1. So that StrictMode does not double-fires in dev mode.
  2. The dev error overlay does not appear (ex)

@BeltranBulbarellaDD BeltranBulbarellaDD changed the title Beltran.bulbarella/nextjs pages router error ✨ NextJS- add nextjs error boundary component Mar 17, 2026
@BeltranBulbarellaDD BeltranBulbarellaDD marked this pull request as ready for review March 18, 2026 20:01
@BeltranBulbarellaDD BeltranBulbarellaDD requested a review from a team as a code owner March 18, 2026 20:01
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

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: 6d7f0239ab

ℹ️ 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".

export { DatadogAppRouter } from '../domain/nextJSRouter/datadogAppRouter'
export { DatadogPagesRouter } from '../domain/nextJSRouter/datadogPagesRouter'
export { addNextjsError } from '../domain/error/addNextjsError'
export { ErrorBoundary } from '../domain/error/errorBoundary'

Choose a reason for hiding this comment

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

P1 Badge Avoid forcing rum-react on every rum-nextjs import

Re-exporting ErrorBoundary from the root entrypoint makes @datadog/browser-rum-react/internal a hard runtime dependency for all consumers, because both CJS and ESM need to resolve ../domain/error/errorBoundary when @datadog/browser-rum-nextjs is imported. That breaks existing integrations that only use DatadogAppRouter/addNextjsError and rely on @datadog/browser-rum-react being optional (as declared in peerDependenciesMeta), resulting in module-resolution failures unless they install rum-react.

Useful? React with 👍 / 👎.

"build:docs:json": "typedoc --logLevel Verbose --json ./docs.json",
"build:docs:html": "typedoc --out ./docs",
"pack": "yarn workspaces foreach --all --parallel --include '@datadog/*' exec yarn pack",
"pack": "yarn workspaces foreach --all --parallel --topological --include '@datadog/*' exec yarn pack",
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about this... rum depends on rum-code which depends on core and that was not an issue before.

expect(customErrors[0].context).toMatchObject({
framework: 'nextjs',
nextjs: { digest: expect.any(String) },
})
Copy link
Member

Choose a reason for hiding this comment

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

suggestion:

          expect(errorEvent.error.component_stack).toBeDefined()

Copy link
Member

Choose a reason for hiding this comment

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

suggestion: this should not be needed. During development, the path points to the correct source, and for production builds the package.json is pointing to the right .d.ts file

"@datadog/browser-rum-core": "6.31.0"
},
"peerDependencies": {
"@datadog/browser-rum-react": "6.31.0",
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: @datadog/browser-rum-react should be a direct dependencies, not a peer dep or a dev dep. Customers should not need to install the react integration themselves

Copy link
Collaborator

@thomas-lebeau thomas-lebeau Mar 19, 2026

Choose a reason for hiding this comment

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

sounds like this as nothing to with this PR

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