Skip to content

⚗ NextJS- add nextjs error boundary component#4352

Merged
BeltranBulbarellaDD merged 21 commits intomainfrom
beltran.bulbarella/nextjs_pages__router_error
Mar 24, 2026
Merged

⚗ NextJS- add nextjs error boundary component#4352
BeltranBulbarellaDD merged 21 commits intomainfrom
beltran.bulbarella/nextjs_pages__router_error

Conversation

@BeltranBulbarellaDD
Copy link
Copy Markdown
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
Copy Markdown

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.30% (-0.01%)

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

@cit-pr-commenter-54b7da
Copy link
Copy Markdown

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

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 176.56 KiB 176.56 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.88 KiB 56.88 KiB 0 B 0.00%
Rum Slim 132.19 KiB 132.19 KiB 0 B 0.00%
Worker 23.63 KiB 23.63 KiB 0 B 0.00%
🚀 CPU Performance
Action Name Base CPU Time (ms) Local CPU Time (ms) 𝚫%
RUM - add global context 0.0038 0.0038 0.00%
RUM - add action 0.0128 0.0131 +2.34%
RUM - add error 0.0128 0.0133 +3.91%
RUM - add timing 0.0025 0.0031 +24.00%
RUM - start view 0.0119 0.0129 +8.40%
RUM - start/stop session replay recording 0.0007 0.0007 0.00%
Logs - log message 0.0141 0.0141 0.00%
🧠 Memory Performance
Action Name Base Memory Consumption Local Memory Consumption 𝚫
RUM - add global context 26.92 KiB 27.57 KiB +664 B
RUM - add action 51.58 KiB 51.85 KiB +275 B
RUM - add timing 26.27 KiB 26.07 KiB -202 B
RUM - add error 56.09 KiB 55.79 KiB -306 B
RUM - start/stop session replay recording 26.11 KiB 25.40 KiB -735 B
RUM - start view 460.15 KiB 462.91 KiB +2.75 KiB
Logs - log message 44.22 KiB 45.99 KiB +1.78 KiB

🔗 RealWorld

"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
Copy Markdown
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
Copy Markdown
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hm let's discuss!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Discussed here

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.

Heeey so after a bit of investigation I think the issue comes from the integration packages having a "prepack" script. This script is called during yarn pack. It builds the package before packing it. This was useful when dogfooding the rum-react package in web-ui before publishing it (see #3182)

I think it makes sense and it should be added to all packages, not just integrations. By doing that, we won't need to run yarn build before running yarn run pack, the build will always be done during pack. As you noted, this requires to use the --topological flag when packing, similarly to the build script. I'll work on this in a separate PR. In the meantime, please add back the --topological option and remove the internal.d.ts file.

stdout: 'pipe' as const,
cwd: path.join(__dirname, '../apps/nextjs'),
command: isLocal ? 'yarn dev' : 'yarn start',
command: 'yarn start',
Copy link
Copy Markdown
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
Copy Markdown

@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".

"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
Copy Markdown
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.

@BeltranBulbarellaDD BeltranBulbarellaDD changed the title ✨ NextJS- add nextjs error boundary component ⚗ NextJS- add nextjs error boundary component Mar 20, 2026
@BeltranBulbarellaDD BeltranBulbarellaDD marked this pull request as draft March 23, 2026 09:52
@BeltranBulbarellaDD BeltranBulbarellaDD marked this pull request as ready for review March 23, 2026 13:19

import { disableJasmineUncaughtExceptionTracking, ignoreConsoleLogs } from '@datadog/browser-core/test'
import { appendComponent } from '../../../../rum-react/test/appendComponent'
import { initReactOldBrowsersSupport } from '../../../../rum-react/test/reactOldBrowsersSupport'
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.

The test imports appendComponent and initReactOldBrowsersSupport via relative paths across package boundaries (../../../../rum-react/test/...). Is there a plan to extract these into a shared test utility, or is this the accepted pattern? It works, just looks fragile if packages ever move around.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed. Maybe we can expose them as shared utility, that would be better.

Copy link
Copy Markdown
Contributor

@mormubis mormubis left a comment

Choose a reason for hiding this comment

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

Looks good!

@BeltranBulbarellaDD BeltranBulbarellaDD merged commit 1933b18 into main Mar 24, 2026
21 checks passed
@BeltranBulbarellaDD BeltranBulbarellaDD deleted the beltran.bulbarella/nextjs_pages__router_error branch March 24, 2026 11:09
@github-actions github-actions bot locked and limited conversation to collaborators Mar 24, 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.

5 participants