@datadog/browser-rum-nextjs 7.8.0 → 7.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/domain/error/addNextjsError.js.map +1 -1
- package/cjs/domain/error/errorBoundary.js.map +1 -1
- package/cjs/domain/nextJSRouter/datadogAppRouter.js +1 -1
- package/cjs/domain/nextJSRouter/datadogAppRouter.js.map +1 -1
- package/cjs/domain/nextJSRouter/datadogPagesRouter.js +1 -1
- package/cjs/domain/nextJSRouter/datadogPagesRouter.js.map +1 -1
- package/cjs/domain/nextjsPlugin.d.ts +1 -1
- package/cjs/domain/nextjsPlugin.js +13 -0
- package/cjs/domain/nextjsPlugin.js.map +1 -1
- package/esm/domain/nextJSRouter/datadogAppRouter.mjs +1 -1
- package/esm/domain/nextJSRouter/datadogPagesRouter.mjs +1 -1
- package/esm/domain/nextjsPlugin.mjs +14 -1
- package/esm/domain/nextjsPlugin.mjs.map +1 -1
- package/package.json +7 -7
- package/src/domain/nextjsPlugin.ts +23 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"addNextjsError.js","names":[],"sources":["../../../src/domain/error/addNextjsError.ts"],"sourcesContent":["import { clocksNow } from '@datadog/js-core/time'\nimport { callMonitored, createHandlingStack } from '@datadog/browser-core'\nimport type { Context } from '@datadog/browser-core'\nimport type { ErrorInfo } from 'react'\nimport { onRumStart } from '../nextjsPlugin'\n\n/**\n * Add a Next.js error to the RUM session.\n *\n * @category Error\n * @example\n * ```ts\n * // app/error.tsx (or app/global-error.tsx)\n * 'use client'\n * import { useEffect } from 'react'\n * import { addNextjsError } from '@datadog/browser-rum-nextjs'\n *\n * export default function Error({ error }: { error: Error & { digest?: string } }) {\n * useEffect(() => {\n * addNextjsError(error)\n * }, [error])\n * return <div>Something went wrong</div>\n * }\n * ```\n */\nexport function addNextjsError(error: Error & { digest?: string }, errorInfo?: ErrorInfo) {\n const handlingStack = createHandlingStack('nextjs error')\n const startClocks = clocksNow()\n onRumStart((addError) => {\n callMonitored(() => {\n addError({\n error,\n handlingStack,\n componentStack: errorInfo?.componentStack ?? undefined,\n startClocks,\n context: {\n ...(error as Error & { dd_context?: Context }).dd_context,\n ...(error.digest && { nextjs: { digest: error.digest } }),\n framework: 'nextjs',\n },\n })\n })\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAyBA,SAAgB,eAAe,OAAoC,WAAuB;CACxF,MAAM,iBAAA,
|
|
1
|
+
{"version":3,"file":"addNextjsError.js","names":["createHandlingStack","clocksNow"],"sources":["../../../src/domain/error/addNextjsError.ts"],"sourcesContent":["import { clocksNow } from '@datadog/js-core/time'\nimport { callMonitored, createHandlingStack } from '@datadog/browser-core'\nimport type { Context } from '@datadog/browser-core'\nimport type { ErrorInfo } from 'react'\nimport { onRumStart } from '../nextjsPlugin'\n\n/**\n * Add a Next.js error to the RUM session.\n *\n * @category Error\n * @example\n * ```ts\n * // app/error.tsx (or app/global-error.tsx)\n * 'use client'\n * import { useEffect } from 'react'\n * import { addNextjsError } from '@datadog/browser-rum-nextjs'\n *\n * export default function Error({ error }: { error: Error & { digest?: string } }) {\n * useEffect(() => {\n * addNextjsError(error)\n * }, [error])\n * return <div>Something went wrong</div>\n * }\n * ```\n */\nexport function addNextjsError(error: Error & { digest?: string }, errorInfo?: ErrorInfo) {\n const handlingStack = createHandlingStack('nextjs error')\n const startClocks = clocksNow()\n onRumStart((addError) => {\n callMonitored(() => {\n addError({\n error,\n handlingStack,\n componentStack: errorInfo?.componentStack ?? undefined,\n startClocks,\n context: {\n ...(error as Error & { dd_context?: Context }).dd_context,\n ...(error.digest && { nextjs: { digest: error.digest } }),\n framework: 'nextjs',\n },\n })\n })\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAyBA,SAAgB,eAAe,OAAoC,WAAuB;CACxF,MAAM,iBAAA,GAAgBA,sBAAAA,oBAAAA,CAAoB,cAAc;CACxD,MAAM,eAAA,GAAcC,sBAAAA,UAAAA,CAAU;CAC9B,qBAAA,YAAY,aAAa;EACvB,CAAA,GAAA,sBAAA,cAAA,OAAoB;GAClB,SAAS;IACP;IACA;IACA,gBAAgB,WAAW,kBAAkB,KAAA;IAC7C;IACA,SAAS;KACP,GAAI,MAA2C;KAC/C,GAAI,MAAM,UAAU,EAAE,QAAQ,EAAE,QAAQ,MAAM,OAAO,EAAE;KACvD,WAAW;IACb;GACF,CAAC;EACH,CAAC;CACH,CAAC;AACH"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errorBoundary.js","names":["addNextjsError"],"sources":["../../../src/domain/error/errorBoundary.ts"],"sourcesContent":["'use client'\nimport { createErrorBoundary } from '@datadog/browser-rum-react/internal'\nexport type { ErrorBoundaryFallback, ErrorBoundaryProps } from '@datadog/browser-rum-react/internal'\nimport { addNextjsError } from './addNextjsError'\n\n/**\n * ErrorBoundary component to report React errors to Datadog using the Next.js error context.\n *\n * For more advanced error handling, you can use the {@link addNextjsError} function.\n *\n * @category Error\n * @example\n * ```ts\n * import { ErrorBoundary } from '@datadog/browser-rum-nextjs'\n *\n * <ErrorBoundary fallback={() => null}>\n * <Component />\n * </ErrorBoundary>\n * ```\n */\n// eslint-disable-next-line local-rules/disallow-side-effects\nexport const ErrorBoundary = createErrorBoundary(addNextjsError)\n"],"mappings":";;;;;;;;;;;;;;;;;;AAqBA,MAAa,iBAAA,
|
|
1
|
+
{"version":3,"file":"errorBoundary.js","names":["createErrorBoundary","addNextjsError"],"sources":["../../../src/domain/error/errorBoundary.ts"],"sourcesContent":["'use client'\nimport { createErrorBoundary } from '@datadog/browser-rum-react/internal'\nexport type { ErrorBoundaryFallback, ErrorBoundaryProps } from '@datadog/browser-rum-react/internal'\nimport { addNextjsError } from './addNextjsError'\n\n/**\n * ErrorBoundary component to report React errors to Datadog using the Next.js error context.\n *\n * For more advanced error handling, you can use the {@link addNextjsError} function.\n *\n * @category Error\n * @example\n * ```ts\n * import { ErrorBoundary } from '@datadog/browser-rum-nextjs'\n *\n * <ErrorBoundary fallback={() => null}>\n * <Component />\n * </ErrorBoundary>\n * ```\n */\n// eslint-disable-next-line local-rules/disallow-side-effects\nexport const ErrorBoundary = createErrorBoundary(addNextjsError)\n"],"mappings":";;;;;;;;;;;;;;;;;;AAqBA,MAAa,iBAAA,gDAAgBA,CAAAA,CAAAA,oBAAAA,CAAoBC,uBAAAA,cAAc"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
const require_nextjsPlugin = require("../nextjsPlugin.js");
|
|
3
3
|
const require_computeViewNameFromParams = require("./computeViewNameFromParams.js");
|
|
4
|
+
let _datadog_browser_core = require("@datadog/browser-core");
|
|
4
5
|
let react = require("react");
|
|
5
6
|
let next_navigation_js = require("next/navigation.js");
|
|
6
|
-
let _datadog_browser_core = require("@datadog/browser-core");
|
|
7
7
|
//#region src/domain/nextJSRouter/datadogAppRouter.tsx
|
|
8
8
|
function DatadogAppRouter() {
|
|
9
9
|
const pathname = (0, _datadog_browser_core.mockable)(next_navigation_js.usePathname)();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"datadogAppRouter.js","names":["usePathname","useParams","useRef","computeViewNameFromParams"],"sources":["../../../src/domain/nextJSRouter/datadogAppRouter.tsx"],"sourcesContent":["'use client'\n\nimport { useRef } from 'react'\nimport { usePathname, useParams } from 'next/navigation'\nimport { mockable } from '@datadog/browser-core'\nimport { startNextjsView } from '../nextjsPlugin'\nimport { computeViewNameFromParams } from './computeViewNameFromParams'\n\nexport function DatadogAppRouter() {\n const pathname = mockable(usePathname)()\n const params = mockable(useParams)()\n const previousPathname = mockable(useRef)<string | null>(null)\n\n if (previousPathname.current !== pathname) {\n previousPathname.current = pathname\n startNextjsView(computeViewNameFromParams(pathname, params))\n }\n\n return null\n}\n"],"mappings":";;;;;;;AAQA,SAAgB,mBAAmB;CACjC,MAAM,YAAA,
|
|
1
|
+
{"version":3,"file":"datadogAppRouter.js","names":["mockable","usePathname","useParams","useRef","computeViewNameFromParams"],"sources":["../../../src/domain/nextJSRouter/datadogAppRouter.tsx"],"sourcesContent":["'use client'\n\nimport { useRef } from 'react'\nimport { usePathname, useParams } from 'next/navigation'\nimport { mockable } from '@datadog/browser-core'\nimport { startNextjsView } from '../nextjsPlugin'\nimport { computeViewNameFromParams } from './computeViewNameFromParams'\n\nexport function DatadogAppRouter() {\n const pathname = mockable(usePathname)()\n const params = mockable(useParams)()\n const previousPathname = mockable(useRef)<string | null>(null)\n\n if (previousPathname.current !== pathname) {\n previousPathname.current = pathname\n startNextjsView(computeViewNameFromParams(pathname, params))\n }\n\n return null\n}\n"],"mappings":";;;;;;;AAQA,SAAgB,mBAAmB;CACjC,MAAM,YAAA,GAAWA,sBAAAA,SAAAA,CAASC,mBAAAA,WAAW,CAAC,CAAC;CACvC,MAAM,UAAA,GAASD,sBAAAA,SAAAA,CAASE,mBAAAA,SAAS,CAAC,CAAC;CACnC,MAAM,oBAAA,GAAmBF,sBAAAA,SAAAA,CAASG,MAAAA,MAAM,CAAC,CAAgB,IAAI;CAE7D,IAAI,iBAAiB,YAAY,UAAU;EACzC,iBAAiB,UAAU;EAC3B,qBAAA,gBAAgBC,kCAAAA,0BAA0B,UAAU,MAAM,CAAC;CAC7D;CAEA,OAAO;AACT"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const require_nextjsPlugin = require("../nextjsPlugin.js");
|
|
2
|
-
let react = require("react");
|
|
3
2
|
let _datadog_browser_core = require("@datadog/browser-core");
|
|
3
|
+
let react = require("react");
|
|
4
4
|
let next_router_js = require("next/router.js");
|
|
5
5
|
//#region src/domain/nextJSRouter/datadogPagesRouter.tsx
|
|
6
6
|
function DatadogPagesRouter() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"datadogPagesRouter.js","names":["useRouter","useRef"],"sources":["../../../src/domain/nextJSRouter/datadogPagesRouter.tsx"],"sourcesContent":["import { useRef } from 'react'\nimport { useRouter } from 'next/router'\nimport { mockable } from '@datadog/browser-core'\nimport { startNextjsView } from '../nextjsPlugin'\n\nexport function DatadogPagesRouter() {\n const router = mockable(useRouter)()\n const previousPath = mockable(useRef)<string | null>(null)\n\n if (!router.isReady) {\n return null\n }\n\n // Extract the path portion of asPath (without query params or hash) to detect navigations.\n const path = router.asPath.split(/[?#]/)[0]\n\n if (previousPath.current !== path) {\n // router.pathname is the route pattern (e.g., \"/user/[id]\") — used as the view name\n // router.asPath is the actual URL (e.g., \"/user/42\") — used to detect navigations between\n // different concrete URLs of the same dynamic route (e.g., /user/42 → /user/43)\n previousPath.current = path\n startNextjsView(router.pathname)\n }\n\n return null\n}\n"],"mappings":";;;;;AAKA,SAAgB,qBAAqB;CACnC,MAAM,UAAA,
|
|
1
|
+
{"version":3,"file":"datadogPagesRouter.js","names":["mockable","useRouter","useRef"],"sources":["../../../src/domain/nextJSRouter/datadogPagesRouter.tsx"],"sourcesContent":["import { useRef } from 'react'\nimport { useRouter } from 'next/router'\nimport { mockable } from '@datadog/browser-core'\nimport { startNextjsView } from '../nextjsPlugin'\n\nexport function DatadogPagesRouter() {\n const router = mockable(useRouter)()\n const previousPath = mockable(useRef)<string | null>(null)\n\n if (!router.isReady) {\n return null\n }\n\n // Extract the path portion of asPath (without query params or hash) to detect navigations.\n const path = router.asPath.split(/[?#]/)[0]\n\n if (previousPath.current !== path) {\n // router.pathname is the route pattern (e.g., \"/user/[id]\") — used as the view name\n // router.asPath is the actual URL (e.g., \"/user/42\") — used to detect navigations between\n // different concrete URLs of the same dynamic route (e.g., /user/42 → /user/43)\n previousPath.current = path\n startNextjsView(router.pathname)\n }\n\n return null\n}\n"],"mappings":";;;;;AAKA,SAAgB,qBAAqB;CACnC,MAAM,UAAA,GAASA,sBAAAA,SAAAA,CAASC,eAAAA,SAAS,CAAC,CAAC;CACnC,MAAM,gBAAA,GAAeD,sBAAAA,SAAAA,CAASE,MAAAA,MAAM,CAAC,CAAgB,IAAI;CAEzD,IAAI,CAAC,OAAO,SACV,OAAO;CAIT,MAAM,OAAO,OAAO,OAAO,MAAM,MAAM,CAAC,CAAC;CAEzC,IAAI,aAAa,YAAY,MAAM;EAIjC,aAAa,UAAU;EACvB,qBAAA,gBAAgB,OAAO,QAAQ;CACjC;CAEA,OAAO;AACT"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { RumPlugin, RumPublicApi, StartRumResult } from '@datadog/browser-rum-core';
|
|
2
|
-
export type NextjsPlugin = Pick<Required<RumPlugin>, 'name' | 'onInit' | 'onRumStart'>;
|
|
2
|
+
export type NextjsPlugin = Pick<Required<RumPlugin>, 'name' | 'onInit' | 'onRumStart' | 'getConfigurationTelemetry'>;
|
|
3
3
|
type InitSubscriber = (rumPublicApi: RumPublicApi) => void;
|
|
4
4
|
type StartSubscriber = (addError: StartRumResult['addError']) => void;
|
|
5
5
|
export declare function nextjsPlugin(): NextjsPlugin;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
let _datadog_js_core_util = require("@datadog/js-core/util");
|
|
2
|
+
let _datadog_browser_core = require("@datadog/browser-core");
|
|
2
3
|
//#region src/domain/nextjsPlugin.ts
|
|
3
4
|
let globalPublicApi;
|
|
4
5
|
let globalAddError;
|
|
5
6
|
let lastNavigationUrl;
|
|
7
|
+
let routerType;
|
|
6
8
|
const onRumInitSubscribers = [];
|
|
7
9
|
const onRumStartSubscribers = [];
|
|
8
10
|
function nextjsPlugin() {
|
|
@@ -11,14 +13,25 @@ function nextjsPlugin() {
|
|
|
11
13
|
onInit({ publicApi, initConfiguration }) {
|
|
12
14
|
globalPublicApi = publicApi;
|
|
13
15
|
initConfiguration.trackViewsManually = true;
|
|
16
|
+
routerType = (0, _datadog_browser_core.mockable)(detectNextjsRouterType)();
|
|
14
17
|
for (const subscriber of onRumInitSubscribers) subscriber(publicApi);
|
|
15
18
|
},
|
|
16
19
|
onRumStart({ addError }) {
|
|
17
20
|
globalAddError = addError;
|
|
18
21
|
if (addError) for (const subscriber of onRumStartSubscribers) subscriber(addError);
|
|
22
|
+
},
|
|
23
|
+
getConfigurationTelemetry() {
|
|
24
|
+
const nextjsVersion = _datadog_js_core_util.globalObject.next?.version;
|
|
25
|
+
return {
|
|
26
|
+
router: true,
|
|
27
|
+
integrations: (0, _datadog_browser_core.toIntegrations)(nextjsVersion && (0, _datadog_browser_core.toMajorVersionIntegration)("nextjs", nextjsVersion), routerType)
|
|
28
|
+
};
|
|
19
29
|
}
|
|
20
30
|
};
|
|
21
31
|
}
|
|
32
|
+
function detectNextjsRouterType() {
|
|
33
|
+
return document.getElementById("__NEXT_DATA__") ? "pages-router" : "app-router";
|
|
34
|
+
}
|
|
22
35
|
function startNextjsView(viewName) {
|
|
23
36
|
if (globalPublicApi) {
|
|
24
37
|
const url = lastNavigationUrl ? (0, _datadog_js_core_util.buildUrl)(lastNavigationUrl, window.location.origin).href : void 0;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nextjsPlugin.js","names":[],"sources":["../../src/domain/nextjsPlugin.ts"],"sourcesContent":["import { buildUrl } from '@datadog/js-core/util'\nimport type { RumPlugin, RumPublicApi, StartRumResult } from '@datadog/browser-rum-core'\n\nexport type NextjsPlugin = Pick<Required<RumPlugin>, 'name' | 'onInit' | 'onRumStart'>\n\ntype InitSubscriber = (rumPublicApi: RumPublicApi) => void\ntype StartSubscriber = (addError: StartRumResult['addError']) => void\n\nlet globalPublicApi: RumPublicApi | undefined\nlet globalAddError: StartRumResult['addError'] | undefined\nlet lastNavigationUrl: string | undefined\n\nconst onRumInitSubscribers: InitSubscriber[] = []\nconst onRumStartSubscribers: StartSubscriber[] = []\n\nexport function nextjsPlugin(): NextjsPlugin {\n return {\n name: 'nextjs',\n onInit({ publicApi, initConfiguration }) {\n globalPublicApi = publicApi\n initConfiguration.trackViewsManually = true\n\n for (const subscriber of onRumInitSubscribers) {\n subscriber(publicApi)\n }\n },\n onRumStart({ addError }) {\n globalAddError = addError\n if (addError) {\n for (const subscriber of onRumStartSubscribers) {\n subscriber(addError)\n }\n }\n },\n } satisfies RumPlugin\n}\n\nexport function startNextjsView(viewName: string) {\n if (globalPublicApi) {\n // Use the URL captured by onRouterTransitionStart if available, since React renders before pushState updates window.location\n const url = lastNavigationUrl ? buildUrl(lastNavigationUrl, window.location.origin).href : undefined\n lastNavigationUrl = undefined\n globalPublicApi.startView({ name: viewName, url })\n }\n}\n\n// Must be re-exported from the user's instrumentation-client.ts so we can capture the URL before React renders\nexport function onRouterTransitionStart(url: string) {\n lastNavigationUrl = url\n}\n\nexport function onRumInit(callback: InitSubscriber) {\n if (globalPublicApi) {\n callback(globalPublicApi)\n } else {\n onRumInitSubscribers.push(callback)\n }\n}\n\nexport function onRumStart(callback: StartSubscriber) {\n if (globalAddError) {\n callback(globalAddError)\n } else {\n onRumStartSubscribers.push(callback)\n }\n}\n\nexport function resetNextjsPlugin() {\n globalPublicApi = undefined\n globalAddError = undefined\n onRumInitSubscribers.length = 0\n onRumStartSubscribers.length = 0\n lastNavigationUrl = undefined\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"nextjsPlugin.js","names":["mockable","globalObject","toIntegrations","toMajorVersionIntegration","buildUrl"],"sources":["../../src/domain/nextjsPlugin.ts"],"sourcesContent":["import { buildUrl, globalObject } from '@datadog/js-core/util'\nimport { mockable, toIntegrations, toMajorVersionIntegration } from '@datadog/browser-core'\nimport type { RumPlugin, RumPublicApi, StartRumResult } from '@datadog/browser-rum-core'\n\nexport type NextjsPlugin = Pick<Required<RumPlugin>, 'name' | 'onInit' | 'onRumStart' | 'getConfigurationTelemetry'>\n\ntype NextjsRouterType = 'app-router' | 'pages-router'\ninterface NextjsGlobalObject {\n next?: { version?: string }\n}\ntype InitSubscriber = (rumPublicApi: RumPublicApi) => void\ntype StartSubscriber = (addError: StartRumResult['addError']) => void\n\nlet globalPublicApi: RumPublicApi | undefined\nlet globalAddError: StartRumResult['addError'] | undefined\nlet lastNavigationUrl: string | undefined\nlet routerType: NextjsRouterType | undefined\n\nconst onRumInitSubscribers: InitSubscriber[] = []\nconst onRumStartSubscribers: StartSubscriber[] = []\n\nexport function nextjsPlugin(): NextjsPlugin {\n return {\n name: 'nextjs',\n onInit({ publicApi, initConfiguration }) {\n globalPublicApi = publicApi\n initConfiguration.trackViewsManually = true\n routerType = mockable(detectNextjsRouterType)()\n\n for (const subscriber of onRumInitSubscribers) {\n subscriber(publicApi)\n }\n },\n onRumStart({ addError }) {\n globalAddError = addError\n if (addError) {\n for (const subscriber of onRumStartSubscribers) {\n subscriber(addError)\n }\n }\n },\n getConfigurationTelemetry() {\n const nextjsVersion = (globalObject as NextjsGlobalObject).next?.version\n return {\n router: true,\n integrations: toIntegrations(nextjsVersion && toMajorVersionIntegration('nextjs', nextjsVersion), routerType),\n }\n },\n } satisfies RumPlugin\n}\n\n// The App Router doesn't hydrate from a `__NEXT_DATA__` payload (it streams RSC data instead),\n// so its absence is used as a best-effort signal that the app router is in use.\nfunction detectNextjsRouterType(): NextjsRouterType {\n return document.getElementById('__NEXT_DATA__') ? 'pages-router' : 'app-router'\n}\n\nexport function startNextjsView(viewName: string) {\n if (globalPublicApi) {\n // Use the URL captured by onRouterTransitionStart if available, since React renders before pushState updates window.location\n const url = lastNavigationUrl ? buildUrl(lastNavigationUrl, window.location.origin).href : undefined\n lastNavigationUrl = undefined\n globalPublicApi.startView({ name: viewName, url })\n }\n}\n\n// Must be re-exported from the user's instrumentation-client.ts so we can capture the URL before React renders\nexport function onRouterTransitionStart(url: string) {\n lastNavigationUrl = url\n}\n\nexport function onRumInit(callback: InitSubscriber) {\n if (globalPublicApi) {\n callback(globalPublicApi)\n } else {\n onRumInitSubscribers.push(callback)\n }\n}\n\nexport function onRumStart(callback: StartSubscriber) {\n if (globalAddError) {\n callback(globalAddError)\n } else {\n onRumStartSubscribers.push(callback)\n }\n}\n\nexport function resetNextjsPlugin() {\n globalPublicApi = undefined\n globalAddError = undefined\n onRumInitSubscribers.length = 0\n onRumStartSubscribers.length = 0\n lastNavigationUrl = undefined\n routerType = undefined\n}\n"],"mappings":";;;AAaA,IAAI;AACJ,IAAI;AACJ,IAAI;AACJ,IAAI;AAEJ,MAAM,uBAAyC,CAAC;AAChD,MAAM,wBAA2C,CAAC;AAElD,SAAgB,eAA6B;CAC3C,OAAO;EACL,MAAM;EACN,OAAO,EAAE,WAAW,qBAAqB;GACvC,kBAAkB;GAClB,kBAAkB,qBAAqB;GACvC,cAAA,GAAaA,sBAAAA,SAAAA,CAAS,sBAAsB,CAAC,CAAC;GAE9C,KAAK,MAAM,cAAc,sBACvB,WAAW,SAAS;EAExB;EACA,WAAW,EAAE,YAAY;GACvB,iBAAiB;GACjB,IAAI,UACF,KAAK,MAAM,cAAc,uBACvB,WAAW,QAAQ;EAGzB;EACA,4BAA4B;GAC1B,MAAM,gBAAiBC,sBAAAA,aAAoC,MAAM;GACjE,OAAO;IACL,QAAQ;IACR,eAAA,GAAcC,sBAAAA,eAAAA,CAAe,kBAAA,GAAiBC,sBAAAA,0BAAAA,CAA0B,UAAU,aAAa,GAAG,UAAU;GAC9G;EACF;CACF;AACF;AAIA,SAAS,yBAA2C;CAClD,OAAO,SAAS,eAAe,eAAe,IAAI,iBAAiB;AACrE;AAEA,SAAgB,gBAAgB,UAAkB;CAChD,IAAI,iBAAiB;EAEnB,MAAM,MAAM,qBAAA,GAAoBC,sBAAAA,SAAAA,CAAS,mBAAmB,OAAO,SAAS,MAAM,CAAC,CAAC,OAAO,KAAA;EAC3F,oBAAoB,KAAA;EACpB,gBAAgB,UAAU;GAAE,MAAM;GAAU;EAAI,CAAC;CACnD;AACF;AAGA,SAAgB,wBAAwB,KAAa;CACnD,oBAAoB;AACtB;AAUA,SAAgB,WAAW,UAA2B;CACpD,IAAI,gBACF,SAAS,cAAc;MAEvB,sBAAsB,KAAK,QAAQ;AAEvC"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { startNextjsView } from "../nextjsPlugin.mjs";
|
|
3
3
|
import { computeViewNameFromParams } from "./computeViewNameFromParams.mjs";
|
|
4
|
+
import { mockable } from "@datadog/browser-core";
|
|
4
5
|
import { useRef } from "react";
|
|
5
6
|
import { useParams, usePathname } from "next/navigation.js";
|
|
6
|
-
import { mockable } from "@datadog/browser-core";
|
|
7
7
|
//#region src/domain/nextJSRouter/datadogAppRouter.tsx
|
|
8
8
|
function DatadogAppRouter() {
|
|
9
9
|
const pathname = mockable(usePathname)();
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { startNextjsView } from "../nextjsPlugin.mjs";
|
|
2
|
-
import { useRef } from "react";
|
|
3
2
|
import { mockable } from "@datadog/browser-core";
|
|
3
|
+
import { useRef } from "react";
|
|
4
4
|
import { useRouter } from "next/router.js";
|
|
5
5
|
//#region src/domain/nextJSRouter/datadogPagesRouter.tsx
|
|
6
6
|
function DatadogPagesRouter() {
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import { buildUrl } from "@datadog/js-core/util";
|
|
1
|
+
import { buildUrl, globalObject } from "@datadog/js-core/util";
|
|
2
|
+
import { mockable, toIntegrations, toMajorVersionIntegration } from "@datadog/browser-core";
|
|
2
3
|
//#region src/domain/nextjsPlugin.ts
|
|
3
4
|
let globalPublicApi;
|
|
4
5
|
let globalAddError;
|
|
5
6
|
let lastNavigationUrl;
|
|
7
|
+
let routerType;
|
|
6
8
|
const onRumInitSubscribers = [];
|
|
7
9
|
const onRumStartSubscribers = [];
|
|
8
10
|
function nextjsPlugin() {
|
|
@@ -11,14 +13,25 @@ function nextjsPlugin() {
|
|
|
11
13
|
onInit({ publicApi, initConfiguration }) {
|
|
12
14
|
globalPublicApi = publicApi;
|
|
13
15
|
initConfiguration.trackViewsManually = true;
|
|
16
|
+
routerType = mockable(detectNextjsRouterType)();
|
|
14
17
|
for (const subscriber of onRumInitSubscribers) subscriber(publicApi);
|
|
15
18
|
},
|
|
16
19
|
onRumStart({ addError }) {
|
|
17
20
|
globalAddError = addError;
|
|
18
21
|
if (addError) for (const subscriber of onRumStartSubscribers) subscriber(addError);
|
|
22
|
+
},
|
|
23
|
+
getConfigurationTelemetry() {
|
|
24
|
+
const nextjsVersion = globalObject.next?.version;
|
|
25
|
+
return {
|
|
26
|
+
router: true,
|
|
27
|
+
integrations: toIntegrations(nextjsVersion && toMajorVersionIntegration("nextjs", nextjsVersion), routerType)
|
|
28
|
+
};
|
|
19
29
|
}
|
|
20
30
|
};
|
|
21
31
|
}
|
|
32
|
+
function detectNextjsRouterType() {
|
|
33
|
+
return document.getElementById("__NEXT_DATA__") ? "pages-router" : "app-router";
|
|
34
|
+
}
|
|
22
35
|
function startNextjsView(viewName) {
|
|
23
36
|
if (globalPublicApi) {
|
|
24
37
|
const url = lastNavigationUrl ? buildUrl(lastNavigationUrl, window.location.origin).href : void 0;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nextjsPlugin.mjs","names":[],"sources":["../../src/domain/nextjsPlugin.ts"],"sourcesContent":["import { buildUrl } from '@datadog/js-core/util'\nimport type { RumPlugin, RumPublicApi, StartRumResult } from '@datadog/browser-rum-core'\n\nexport type NextjsPlugin = Pick<Required<RumPlugin>, 'name' | 'onInit' | 'onRumStart'>\n\ntype InitSubscriber = (rumPublicApi: RumPublicApi) => void\ntype StartSubscriber = (addError: StartRumResult['addError']) => void\n\nlet globalPublicApi: RumPublicApi | undefined\nlet globalAddError: StartRumResult['addError'] | undefined\nlet lastNavigationUrl: string | undefined\n\nconst onRumInitSubscribers: InitSubscriber[] = []\nconst onRumStartSubscribers: StartSubscriber[] = []\n\nexport function nextjsPlugin(): NextjsPlugin {\n return {\n name: 'nextjs',\n onInit({ publicApi, initConfiguration }) {\n globalPublicApi = publicApi\n initConfiguration.trackViewsManually = true\n\n for (const subscriber of onRumInitSubscribers) {\n subscriber(publicApi)\n }\n },\n onRumStart({ addError }) {\n globalAddError = addError\n if (addError) {\n for (const subscriber of onRumStartSubscribers) {\n subscriber(addError)\n }\n }\n },\n } satisfies RumPlugin\n}\n\nexport function startNextjsView(viewName: string) {\n if (globalPublicApi) {\n // Use the URL captured by onRouterTransitionStart if available, since React renders before pushState updates window.location\n const url = lastNavigationUrl ? buildUrl(lastNavigationUrl, window.location.origin).href : undefined\n lastNavigationUrl = undefined\n globalPublicApi.startView({ name: viewName, url })\n }\n}\n\n// Must be re-exported from the user's instrumentation-client.ts so we can capture the URL before React renders\nexport function onRouterTransitionStart(url: string) {\n lastNavigationUrl = url\n}\n\nexport function onRumInit(callback: InitSubscriber) {\n if (globalPublicApi) {\n callback(globalPublicApi)\n } else {\n onRumInitSubscribers.push(callback)\n }\n}\n\nexport function onRumStart(callback: StartSubscriber) {\n if (globalAddError) {\n callback(globalAddError)\n } else {\n onRumStartSubscribers.push(callback)\n }\n}\n\nexport function resetNextjsPlugin() {\n globalPublicApi = undefined\n globalAddError = undefined\n onRumInitSubscribers.length = 0\n onRumStartSubscribers.length = 0\n lastNavigationUrl = undefined\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"nextjsPlugin.mjs","names":[],"sources":["../../src/domain/nextjsPlugin.ts"],"sourcesContent":["import { buildUrl, globalObject } from '@datadog/js-core/util'\nimport { mockable, toIntegrations, toMajorVersionIntegration } from '@datadog/browser-core'\nimport type { RumPlugin, RumPublicApi, StartRumResult } from '@datadog/browser-rum-core'\n\nexport type NextjsPlugin = Pick<Required<RumPlugin>, 'name' | 'onInit' | 'onRumStart' | 'getConfigurationTelemetry'>\n\ntype NextjsRouterType = 'app-router' | 'pages-router'\ninterface NextjsGlobalObject {\n next?: { version?: string }\n}\ntype InitSubscriber = (rumPublicApi: RumPublicApi) => void\ntype StartSubscriber = (addError: StartRumResult['addError']) => void\n\nlet globalPublicApi: RumPublicApi | undefined\nlet globalAddError: StartRumResult['addError'] | undefined\nlet lastNavigationUrl: string | undefined\nlet routerType: NextjsRouterType | undefined\n\nconst onRumInitSubscribers: InitSubscriber[] = []\nconst onRumStartSubscribers: StartSubscriber[] = []\n\nexport function nextjsPlugin(): NextjsPlugin {\n return {\n name: 'nextjs',\n onInit({ publicApi, initConfiguration }) {\n globalPublicApi = publicApi\n initConfiguration.trackViewsManually = true\n routerType = mockable(detectNextjsRouterType)()\n\n for (const subscriber of onRumInitSubscribers) {\n subscriber(publicApi)\n }\n },\n onRumStart({ addError }) {\n globalAddError = addError\n if (addError) {\n for (const subscriber of onRumStartSubscribers) {\n subscriber(addError)\n }\n }\n },\n getConfigurationTelemetry() {\n const nextjsVersion = (globalObject as NextjsGlobalObject).next?.version\n return {\n router: true,\n integrations: toIntegrations(nextjsVersion && toMajorVersionIntegration('nextjs', nextjsVersion), routerType),\n }\n },\n } satisfies RumPlugin\n}\n\n// The App Router doesn't hydrate from a `__NEXT_DATA__` payload (it streams RSC data instead),\n// so its absence is used as a best-effort signal that the app router is in use.\nfunction detectNextjsRouterType(): NextjsRouterType {\n return document.getElementById('__NEXT_DATA__') ? 'pages-router' : 'app-router'\n}\n\nexport function startNextjsView(viewName: string) {\n if (globalPublicApi) {\n // Use the URL captured by onRouterTransitionStart if available, since React renders before pushState updates window.location\n const url = lastNavigationUrl ? buildUrl(lastNavigationUrl, window.location.origin).href : undefined\n lastNavigationUrl = undefined\n globalPublicApi.startView({ name: viewName, url })\n }\n}\n\n// Must be re-exported from the user's instrumentation-client.ts so we can capture the URL before React renders\nexport function onRouterTransitionStart(url: string) {\n lastNavigationUrl = url\n}\n\nexport function onRumInit(callback: InitSubscriber) {\n if (globalPublicApi) {\n callback(globalPublicApi)\n } else {\n onRumInitSubscribers.push(callback)\n }\n}\n\nexport function onRumStart(callback: StartSubscriber) {\n if (globalAddError) {\n callback(globalAddError)\n } else {\n onRumStartSubscribers.push(callback)\n }\n}\n\nexport function resetNextjsPlugin() {\n globalPublicApi = undefined\n globalAddError = undefined\n onRumInitSubscribers.length = 0\n onRumStartSubscribers.length = 0\n lastNavigationUrl = undefined\n routerType = undefined\n}\n"],"mappings":";;;AAaA,IAAI;AACJ,IAAI;AACJ,IAAI;AACJ,IAAI;AAEJ,MAAM,uBAAyC,CAAC;AAChD,MAAM,wBAA2C,CAAC;AAElD,SAAgB,eAA6B;CAC3C,OAAO;EACL,MAAM;EACN,OAAO,EAAE,WAAW,qBAAqB;GACvC,kBAAkB;GAClB,kBAAkB,qBAAqB;GACvC,aAAa,SAAS,sBAAsB,CAAC,CAAC;GAE9C,KAAK,MAAM,cAAc,sBACvB,WAAW,SAAS;EAExB;EACA,WAAW,EAAE,YAAY;GACvB,iBAAiB;GACjB,IAAI,UACF,KAAK,MAAM,cAAc,uBACvB,WAAW,QAAQ;EAGzB;EACA,4BAA4B;GAC1B,MAAM,gBAAiB,aAAoC,MAAM;GACjE,OAAO;IACL,QAAQ;IACR,cAAc,eAAe,iBAAiB,0BAA0B,UAAU,aAAa,GAAG,UAAU;GAC9G;EACF;CACF;AACF;AAIA,SAAS,yBAA2C;CAClD,OAAO,SAAS,eAAe,eAAe,IAAI,iBAAiB;AACrE;AAEA,SAAgB,gBAAgB,UAAkB;CAChD,IAAI,iBAAiB;EAEnB,MAAM,MAAM,oBAAoB,SAAS,mBAAmB,OAAO,SAAS,MAAM,CAAC,CAAC,OAAO,KAAA;EAC3F,oBAAoB,KAAA;EACpB,gBAAgB,UAAU;GAAE,MAAM;GAAU;EAAI,CAAC;CACnD;AACF;AAGA,SAAgB,wBAAwB,KAAa;CACnD,oBAAoB;AACtB;AAUA,SAAgB,WAAW,UAA2B;CACpD,IAAI,gBACF,SAAS,cAAc;MAEvB,sBAAsB,KAAK,QAAQ;AAEvC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@datadog/browser-rum-nextjs",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.10.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "cjs/entries/main.js",
|
|
6
6
|
"module": "esm/entries/main.mjs",
|
|
@@ -17,10 +17,10 @@
|
|
|
17
17
|
"prepack": "yarn build"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@datadog/browser-core": "7.
|
|
21
|
-
"@datadog/browser-rum-core": "7.
|
|
22
|
-
"@datadog/browser-rum-react": "7.
|
|
23
|
-
"@datadog/js-core": "0.0.
|
|
20
|
+
"@datadog/browser-core": "7.10.0",
|
|
21
|
+
"@datadog/browser-rum-core": "7.10.0",
|
|
22
|
+
"@datadog/browser-rum-react": "7.10.0",
|
|
23
|
+
"@datadog/js-core": "0.0.11"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
26
|
"next": ">=13.0.0",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"extends": "../../package.json"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@types/react": "19.2.
|
|
47
|
-
"next": "16.
|
|
46
|
+
"@types/react": "19.2.18",
|
|
47
|
+
"next": "16.3.1",
|
|
48
48
|
"react": "19.2.8",
|
|
49
49
|
"react-dom": "19.2.8"
|
|
50
50
|
}
|
|
@@ -1,14 +1,20 @@
|
|
|
1
|
-
import { buildUrl } from '@datadog/js-core/util'
|
|
1
|
+
import { buildUrl, globalObject } from '@datadog/js-core/util'
|
|
2
|
+
import { mockable, toIntegrations, toMajorVersionIntegration } from '@datadog/browser-core'
|
|
2
3
|
import type { RumPlugin, RumPublicApi, StartRumResult } from '@datadog/browser-rum-core'
|
|
3
4
|
|
|
4
|
-
export type NextjsPlugin = Pick<Required<RumPlugin>, 'name' | 'onInit' | 'onRumStart'>
|
|
5
|
+
export type NextjsPlugin = Pick<Required<RumPlugin>, 'name' | 'onInit' | 'onRumStart' | 'getConfigurationTelemetry'>
|
|
5
6
|
|
|
7
|
+
type NextjsRouterType = 'app-router' | 'pages-router'
|
|
8
|
+
interface NextjsGlobalObject {
|
|
9
|
+
next?: { version?: string }
|
|
10
|
+
}
|
|
6
11
|
type InitSubscriber = (rumPublicApi: RumPublicApi) => void
|
|
7
12
|
type StartSubscriber = (addError: StartRumResult['addError']) => void
|
|
8
13
|
|
|
9
14
|
let globalPublicApi: RumPublicApi | undefined
|
|
10
15
|
let globalAddError: StartRumResult['addError'] | undefined
|
|
11
16
|
let lastNavigationUrl: string | undefined
|
|
17
|
+
let routerType: NextjsRouterType | undefined
|
|
12
18
|
|
|
13
19
|
const onRumInitSubscribers: InitSubscriber[] = []
|
|
14
20
|
const onRumStartSubscribers: StartSubscriber[] = []
|
|
@@ -19,6 +25,7 @@ export function nextjsPlugin(): NextjsPlugin {
|
|
|
19
25
|
onInit({ publicApi, initConfiguration }) {
|
|
20
26
|
globalPublicApi = publicApi
|
|
21
27
|
initConfiguration.trackViewsManually = true
|
|
28
|
+
routerType = mockable(detectNextjsRouterType)()
|
|
22
29
|
|
|
23
30
|
for (const subscriber of onRumInitSubscribers) {
|
|
24
31
|
subscriber(publicApi)
|
|
@@ -32,9 +39,22 @@ export function nextjsPlugin(): NextjsPlugin {
|
|
|
32
39
|
}
|
|
33
40
|
}
|
|
34
41
|
},
|
|
42
|
+
getConfigurationTelemetry() {
|
|
43
|
+
const nextjsVersion = (globalObject as NextjsGlobalObject).next?.version
|
|
44
|
+
return {
|
|
45
|
+
router: true,
|
|
46
|
+
integrations: toIntegrations(nextjsVersion && toMajorVersionIntegration('nextjs', nextjsVersion), routerType),
|
|
47
|
+
}
|
|
48
|
+
},
|
|
35
49
|
} satisfies RumPlugin
|
|
36
50
|
}
|
|
37
51
|
|
|
52
|
+
// The App Router doesn't hydrate from a `__NEXT_DATA__` payload (it streams RSC data instead),
|
|
53
|
+
// so its absence is used as a best-effort signal that the app router is in use.
|
|
54
|
+
function detectNextjsRouterType(): NextjsRouterType {
|
|
55
|
+
return document.getElementById('__NEXT_DATA__') ? 'pages-router' : 'app-router'
|
|
56
|
+
}
|
|
57
|
+
|
|
38
58
|
export function startNextjsView(viewName: string) {
|
|
39
59
|
if (globalPublicApi) {
|
|
40
60
|
// Use the URL captured by onRouterTransitionStart if available, since React renders before pushState updates window.location
|
|
@@ -71,4 +91,5 @@ export function resetNextjsPlugin() {
|
|
|
71
91
|
onRumInitSubscribers.length = 0
|
|
72
92
|
onRumStartSubscribers.length = 0
|
|
73
93
|
lastNavigationUrl = undefined
|
|
94
|
+
routerType = undefined
|
|
74
95
|
}
|