-
Notifications
You must be signed in to change notification settings - Fork 175
✨ NextJS Pages Router Integration #4290
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
BeltranBulbarellaDD
merged 12 commits into
main
from
beltran.bulbarella/NextJS-pagesRouterIntegration
Mar 10, 2026
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1e2eadb
Add .rum-ai-toolkit/ to gitignore
BeltranBulbarellaDD 372af36
Add datadogPagesRouter, unit and e2e tests, e2e test app.
BeltranBulbarellaDD dc498c0
Build pages router e2e test app, merge tests scenarios for nextjs
BeltranBulbarellaDD d36bc66
simplify nextjs e2e setup, choose random port
BeltranBulbarellaDD 23eedb3
fix e2e ci tests
BeltranBulbarellaDD ed231d8
add empty favicon
BeltranBulbarellaDD 81a79a2
use path instead of pathname for view change
BeltranBulbarellaDD 2a3bb4f
Fix test
BeltranBulbarellaDD 99b888f
Add e2e test for change in query params, wait until router is ready i…
BeltranBulbarellaDD 11afd17
Fix README
BeltranBulbarellaDD 4cb9c39
Refactor e2e test app.
BeltranBulbarellaDD 3c6b98b
Add unit tests for nextJSRouter
BeltranBulbarellaDD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| 'use client' | ||
|
|
||
| import { useRef } from 'react' | ||
| import { useRouter } from 'next/router' | ||
| import { startNextjsView } from './nextjsPlugin' | ||
|
|
||
| export function DatadogPagesRouter() { | ||
| const router = useRouter() | ||
| const previousAsPath = useRef<string | null>(null) | ||
|
|
||
| if (previousAsPath.current !== router.asPath) { | ||
|
BeltranBulbarellaDD marked this conversation as resolved.
Outdated
|
||
| previousAsPath.current = router.asPath | ||
| // router.pathname is the route pattern (e.g., "/user/[id]") — used as the view name | ||
| // router.asPath is the actual URL (e.g., "/user/42") — used to detect navigations between | ||
| // different concrete URLs of the same dynamic route (e.g., /user/42 → /user/43) | ||
| startNextjsView(router.pathname) | ||
| } | ||
|
|
||
| return null | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export { nextjsPlugin, onRouterTransitionStart } from '../domain/nextjsPlugin' | ||
| export type { NextjsPlugin } from '../domain/nextjsPlugin' | ||
| export { DatadogAppRouter } from '../domain/datadogAppRouter' | ||
| export { DatadogPagesRouter } from '../domain/datadogPagesRouter' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| /// <reference types="next" /> | ||
| /// <reference types="next/image-types/global" /> | ||
| import "./.next/types/routes.d.ts"; | ||
| import "./.next/dev/types/routes.d.ts"; | ||
|
|
||
| // NOTE: This file should not be edited | ||
| // see https://nextjs.org/docs/app/api-reference/config/typescript for more information. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| .next | ||
| node_modules | ||
| .yarn/* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { datadogRum } from '@datadog/browser-rum' | ||
| import { nextjsPlugin } from '@datadog/browser-rum-nextjs' | ||
|
|
||
| const params = new URLSearchParams(window.location.search) | ||
| const config = params.get('rum-config') | ||
| const context = params.get('rum-context') | ||
|
|
||
| if (config && !datadogRum.getInitConfiguration()) { | ||
| datadogRum.init({ ...JSON.parse(config), plugins: [nextjsPlugin()] }) | ||
| if (context) { | ||
| datadogRum.setGlobalContext(JSON.parse(context)) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| /// <reference types="next" /> | ||
| /// <reference types="next/image-types/global" /> | ||
| import "./.next/dev/types/routes.d.ts"; | ||
|
|
||
| // NOTE: This file should not be edited | ||
| // see https://nextjs.org/docs/pages/api-reference/config/typescript for more information. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| /** @type {import('next').NextConfig} */ | ||
| const nextConfig = { | ||
| turbopack: { | ||
| root: __dirname, | ||
| }, | ||
| } | ||
|
|
||
| module.exports = nextConfig |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| { | ||
| "name": "nextjs-pages-router", | ||
| "private": true, | ||
| "scripts": { | ||
| "dev": "next dev -p 0", | ||
| "build": "next build", | ||
| "start": "next start -p 0" | ||
| }, | ||
| "dependencies": { | ||
| "@datadog/browser-rum": "file:../../../packages/rum/package.tgz", | ||
| "@datadog/browser-rum-nextjs": "file:../../../packages/rum-nextjs/package.tgz", | ||
| "next": "16.1.6", | ||
| "react": "19.2.3", | ||
| "react-dom": "19.2.3" | ||
| }, | ||
| "resolutions": { | ||
| "@datadog/browser-rum-core": "file:../../../packages/rum-core/package.tgz", | ||
| "@datadog/browser-core": "file:../../../packages/core/package.tgz", | ||
| "@datadog/browser-rum": "file:../../../packages/rum/package.tgz", | ||
| "@datadog/browser-rum-nextjs": "file:../../../packages/rum-nextjs/package.tgz", | ||
| "@datadog/browser-rum-slim": "file:../../../packages/rum-slim/package.tgz", | ||
| "@datadog/browser-worker": "file:../../../packages/worker/package.tgz" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "22.16.0", | ||
| "@types/react": "19.2.8", | ||
| "@types/react-dom": "19.2.3", | ||
| "typescript": "5.9.3" | ||
| }, | ||
| "volta": { | ||
| "extends": "../../../package.json" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import type { AppProps } from 'next/app' | ||
| import Link from 'next/link' | ||
| import { DatadogPagesRouter } from '@datadog/browser-rum-nextjs' | ||
|
|
||
| export default function MyApp({ Component, pageProps }: AppProps) { | ||
| return ( | ||
| <> | ||
| <DatadogPagesRouter /> | ||
| <nav style={{ background: '#632ca6', padding: '1rem', marginBottom: '1rem' }}> | ||
| <Link href="/" style={{ color: 'white', textDecoration: 'none' }}> | ||
| Home | ||
| </Link> | ||
| </nav> | ||
| <main style={{ maxWidth: '800px', margin: '0 auto', padding: '1rem' }}> | ||
| <Component {...pageProps} /> | ||
| </main> | ||
| </> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import Link from 'next/link' | ||
| import { useRouter } from 'next/router' | ||
|
|
||
| export default function GuidesPage() { | ||
| const router = useRouter() | ||
| const { slug } = router.query | ||
| const slugParts = Array.isArray(slug) ? slug : slug ? [slug] : [] | ||
|
|
||
| return ( | ||
| <div> | ||
| <Link href="/">← Back to Home</Link> | ||
| <h1>Guides: {slugParts.join('/')}</h1> | ||
| <p>This is a catch-all route testing slug normalization.</p> | ||
| </div> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import Link from 'next/link' | ||
|
|
||
| export default function HomePage() { | ||
| return ( | ||
| <div> | ||
| <h1>Home</h1> | ||
| <ul> | ||
| <li> | ||
| <Link href="/user/42?admin=true">Go to User 42</Link> | ||
| </li> | ||
| <li> | ||
| <Link href="/guides/123">Go to Guides 123</Link> | ||
| </li> | ||
| </ul> | ||
| </div> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import Link from 'next/link' | ||
| import { useRouter } from 'next/router' | ||
|
|
||
| export default function UserPage() { | ||
| const router = useRouter() | ||
| const { id } = router.query | ||
|
|
||
| return ( | ||
| <div> | ||
| <Link href="/">← Back to Home</Link> | ||
| <h1>User {id}</h1> | ||
| <p>This is a dynamic route testing view name normalization.</p> | ||
| <Link href="/user/999?admin=true">Go to User 999</Link> | ||
| </div> | ||
| ) | ||
| } |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "lib": ["dom", "dom.iterable", "esnext"], | ||
| "allowJs": true, | ||
| "skipLibCheck": true, | ||
| "strict": true, | ||
| "noEmit": true, | ||
| "esModuleInterop": true, | ||
| "module": "esnext", | ||
| "moduleResolution": "bundler", | ||
| "resolveJsonModule": true, | ||
| "isolatedModules": true, | ||
| "jsx": "react-jsx", | ||
| "incremental": true, | ||
| "plugins": [{ "name": "next" }], | ||
| "paths": { "@/*": ["./*"] }, | ||
| "target": "ES2017" | ||
| }, | ||
| "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "**/*.js", ".next/types/**/*.ts", ".next/dev/types/**/*.ts"], | ||
| "exclude": ["node_modules"] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.