@datadog/browser-rum-nextjs 0.0.0 → 6.32.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/LICENSE +201 -0
- package/README.md +209 -1
- package/cjs/domain/error/addNextjsError.d.ts +23 -0
- package/cjs/domain/error/addNextjsError.js +45 -0
- package/cjs/domain/error/addNextjsError.js.map +1 -0
- package/cjs/domain/error/errorBoundary.d.ts +17 -0
- package/cjs/domain/error/errorBoundary.js +24 -0
- package/cjs/domain/error/errorBoundary.js.map +1 -0
- package/cjs/domain/nextJSRouter/computeViewNameFromParams.d.ts +4 -0
- package/cjs/domain/nextJSRouter/computeViewNameFromParams.js +36 -0
- package/cjs/domain/nextJSRouter/computeViewNameFromParams.js.map +1 -0
- package/cjs/domain/nextJSRouter/datadogAppRouter.d.ts +1 -0
- package/cjs/domain/nextJSRouter/datadogAppRouter.js +20 -0
- package/cjs/domain/nextJSRouter/datadogAppRouter.js.map +1 -0
- package/cjs/domain/nextJSRouter/datadogPagesRouter.d.ts +1 -0
- package/cjs/domain/nextJSRouter/datadogPagesRouter.js +25 -0
- package/cjs/domain/nextJSRouter/datadogPagesRouter.js.map +1 -0
- package/cjs/domain/nextjsPlugin.d.ts +11 -0
- package/cjs/domain/nextjsPlugin.js +70 -0
- package/cjs/domain/nextjsPlugin.js.map +1 -0
- package/cjs/entries/main.d.ts +7 -0
- package/cjs/entries/main.js +15 -0
- package/cjs/entries/main.js.map +1 -0
- package/esm/domain/error/addNextjsError.d.ts +23 -0
- package/esm/domain/error/addNextjsError.js +42 -0
- package/esm/domain/error/addNextjsError.js.map +1 -0
- package/esm/domain/error/errorBoundary.d.ts +17 -0
- package/esm/domain/error/errorBoundary.js +21 -0
- package/esm/domain/error/errorBoundary.js.map +1 -0
- package/esm/domain/nextJSRouter/computeViewNameFromParams.d.ts +4 -0
- package/esm/domain/nextJSRouter/computeViewNameFromParams.js +33 -0
- package/esm/domain/nextJSRouter/computeViewNameFromParams.js.map +1 -0
- package/esm/domain/nextJSRouter/datadogAppRouter.d.ts +1 -0
- package/esm/domain/nextJSRouter/datadogAppRouter.js +17 -0
- package/esm/domain/nextJSRouter/datadogAppRouter.js.map +1 -0
- package/esm/domain/nextJSRouter/datadogPagesRouter.d.ts +1 -0
- package/esm/domain/nextJSRouter/datadogPagesRouter.js +22 -0
- package/esm/domain/nextJSRouter/datadogPagesRouter.js.map +1 -0
- package/esm/domain/nextjsPlugin.d.ts +11 -0
- package/esm/domain/nextjsPlugin.js +62 -0
- package/esm/domain/nextjsPlugin.js.map +1 -0
- package/esm/entries/main.d.ts +7 -0
- package/esm/entries/main.js +6 -0
- package/esm/entries/main.js.map +1 -0
- package/package.json +46 -3
- package/src/domain/error/addNextjsError.ts +43 -0
- package/src/domain/error/errorBoundary.ts +22 -0
- package/src/domain/nextJSRouter/computeViewNameFromParams.ts +40 -0
- package/src/domain/nextJSRouter/datadogAppRouter.tsx +20 -0
- package/src/domain/nextJSRouter/datadogPagesRouter.tsx +26 -0
- package/src/domain/nextjsPlugin.ts +74 -0
- package/src/entries/main.ts +7 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.nextjsPlugin = nextjsPlugin;
|
|
4
|
+
exports.startNextjsView = startNextjsView;
|
|
5
|
+
exports.onRouterTransitionStart = onRouterTransitionStart;
|
|
6
|
+
exports.onRumInit = onRumInit;
|
|
7
|
+
exports.onRumStart = onRumStart;
|
|
8
|
+
exports.resetNextjsPlugin = resetNextjsPlugin;
|
|
9
|
+
const browser_core_1 = require("@datadog/browser-core");
|
|
10
|
+
let globalPublicApi;
|
|
11
|
+
let globalAddError;
|
|
12
|
+
let lastNavigationUrl;
|
|
13
|
+
const onRumInitSubscribers = [];
|
|
14
|
+
const onRumStartSubscribers = [];
|
|
15
|
+
function nextjsPlugin() {
|
|
16
|
+
return {
|
|
17
|
+
name: 'nextjs',
|
|
18
|
+
onInit({ publicApi, initConfiguration }) {
|
|
19
|
+
globalPublicApi = publicApi;
|
|
20
|
+
initConfiguration.trackViewsManually = true;
|
|
21
|
+
for (const subscriber of onRumInitSubscribers) {
|
|
22
|
+
subscriber(publicApi);
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
onRumStart({ addError }) {
|
|
26
|
+
globalAddError = addError;
|
|
27
|
+
if (addError) {
|
|
28
|
+
for (const subscriber of onRumStartSubscribers) {
|
|
29
|
+
subscriber(addError);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function startNextjsView(viewName) {
|
|
36
|
+
if (globalPublicApi) {
|
|
37
|
+
// Use the URL captured by onRouterTransitionStart if available, since React renders before pushState updates window.location
|
|
38
|
+
const url = lastNavigationUrl ? (0, browser_core_1.buildUrl)(lastNavigationUrl, window.location.origin).href : undefined;
|
|
39
|
+
lastNavigationUrl = undefined;
|
|
40
|
+
globalPublicApi.startView({ name: viewName, url });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// Must be re-exported from the user's instrumentation-client.ts so we can capture the URL before React renders
|
|
44
|
+
function onRouterTransitionStart(url) {
|
|
45
|
+
lastNavigationUrl = url;
|
|
46
|
+
}
|
|
47
|
+
function onRumInit(callback) {
|
|
48
|
+
if (globalPublicApi) {
|
|
49
|
+
callback(globalPublicApi);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
onRumInitSubscribers.push(callback);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function onRumStart(callback) {
|
|
56
|
+
if (globalAddError) {
|
|
57
|
+
callback(globalAddError);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
onRumStartSubscribers.push(callback);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function resetNextjsPlugin() {
|
|
64
|
+
globalPublicApi = undefined;
|
|
65
|
+
globalAddError = undefined;
|
|
66
|
+
onRumInitSubscribers.length = 0;
|
|
67
|
+
onRumStartSubscribers.length = 0;
|
|
68
|
+
lastNavigationUrl = undefined;
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=nextjsPlugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nextjsPlugin.js","sourceRoot":"","sources":["../../src/domain/nextjsPlugin.ts"],"names":[],"mappings":";;AAeA,oCAoBC;AAED,0CAOC;AAGD,0DAEC;AAED,8BAMC;AAED,gCAMC;AAED,8CAMC;AAzED,wDAAgD;AAQhD,IAAI,eAAyC,CAAA;AAC7C,IAAI,cAAsD,CAAA;AAC1D,IAAI,iBAAqC,CAAA;AAEzC,MAAM,oBAAoB,GAAqB,EAAE,CAAA;AACjD,MAAM,qBAAqB,GAAsB,EAAE,CAAA;AAEnD,SAAgB,YAAY;IAC1B,OAAO;QACL,IAAI,EAAE,QAAQ;QACd,MAAM,CAAC,EAAE,SAAS,EAAE,iBAAiB,EAAE;YACrC,eAAe,GAAG,SAAS,CAAA;YAC3B,iBAAiB,CAAC,kBAAkB,GAAG,IAAI,CAAA;YAE3C,KAAK,MAAM,UAAU,IAAI,oBAAoB,EAAE,CAAC;gBAC9C,UAAU,CAAC,SAAS,CAAC,CAAA;YACvB,CAAC;QACH,CAAC;QACD,UAAU,CAAC,EAAE,QAAQ,EAAE;YACrB,cAAc,GAAG,QAAQ,CAAA;YACzB,IAAI,QAAQ,EAAE,CAAC;gBACb,KAAK,MAAM,UAAU,IAAI,qBAAqB,EAAE,CAAC;oBAC/C,UAAU,CAAC,QAAQ,CAAC,CAAA;gBACtB,CAAC;YACH,CAAC;QACH,CAAC;KACkB,CAAA;AACvB,CAAC;AAED,SAAgB,eAAe,CAAC,QAAgB;IAC9C,IAAI,eAAe,EAAE,CAAC;QACpB,6HAA6H;QAC7H,MAAM,GAAG,GAAG,iBAAiB,CAAC,CAAC,CAAC,IAAA,uBAAQ,EAAC,iBAAiB,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAA;QACpG,iBAAiB,GAAG,SAAS,CAAA;QAC7B,eAAe,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,CAAC,CAAA;IACpD,CAAC;AACH,CAAC;AAED,+GAA+G;AAC/G,SAAgB,uBAAuB,CAAC,GAAW;IACjD,iBAAiB,GAAG,GAAG,CAAA;AACzB,CAAC;AAED,SAAgB,SAAS,CAAC,QAAwB;IAChD,IAAI,eAAe,EAAE,CAAC;QACpB,QAAQ,CAAC,eAAe,CAAC,CAAA;IAC3B,CAAC;SAAM,CAAC;QACN,oBAAoB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IACrC,CAAC;AACH,CAAC;AAED,SAAgB,UAAU,CAAC,QAAyB;IAClD,IAAI,cAAc,EAAE,CAAC;QACnB,QAAQ,CAAC,cAAc,CAAC,CAAA;IAC1B,CAAC;SAAM,CAAC;QACN,qBAAqB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IACtC,CAAC;AACH,CAAC;AAED,SAAgB,iBAAiB;IAC/B,eAAe,GAAG,SAAS,CAAA;IAC3B,cAAc,GAAG,SAAS,CAAA;IAC1B,oBAAoB,CAAC,MAAM,GAAG,CAAC,CAAA;IAC/B,qBAAqB,CAAC,MAAM,GAAG,CAAC,CAAA;IAChC,iBAAiB,GAAG,SAAS,CAAA;AAC/B,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { nextjsPlugin, onRouterTransitionStart } from '../domain/nextjsPlugin';
|
|
2
|
+
export type { NextjsPlugin } from '../domain/nextjsPlugin';
|
|
3
|
+
export { DatadogAppRouter } from '../domain/nextJSRouter/datadogAppRouter';
|
|
4
|
+
export { DatadogPagesRouter } from '../domain/nextJSRouter/datadogPagesRouter';
|
|
5
|
+
export { addNextjsError } from '../domain/error/addNextjsError';
|
|
6
|
+
export { ErrorBoundary } from '../domain/error/errorBoundary';
|
|
7
|
+
export type { ErrorBoundaryFallback, ErrorBoundaryProps } from '../domain/error/errorBoundary';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ErrorBoundary = exports.addNextjsError = exports.DatadogPagesRouter = exports.DatadogAppRouter = exports.onRouterTransitionStart = exports.nextjsPlugin = void 0;
|
|
4
|
+
var nextjsPlugin_1 = require("../domain/nextjsPlugin");
|
|
5
|
+
Object.defineProperty(exports, "nextjsPlugin", { enumerable: true, get: function () { return nextjsPlugin_1.nextjsPlugin; } });
|
|
6
|
+
Object.defineProperty(exports, "onRouterTransitionStart", { enumerable: true, get: function () { return nextjsPlugin_1.onRouterTransitionStart; } });
|
|
7
|
+
var datadogAppRouter_1 = require("../domain/nextJSRouter/datadogAppRouter");
|
|
8
|
+
Object.defineProperty(exports, "DatadogAppRouter", { enumerable: true, get: function () { return datadogAppRouter_1.DatadogAppRouter; } });
|
|
9
|
+
var datadogPagesRouter_1 = require("../domain/nextJSRouter/datadogPagesRouter");
|
|
10
|
+
Object.defineProperty(exports, "DatadogPagesRouter", { enumerable: true, get: function () { return datadogPagesRouter_1.DatadogPagesRouter; } });
|
|
11
|
+
var addNextjsError_1 = require("../domain/error/addNextjsError");
|
|
12
|
+
Object.defineProperty(exports, "addNextjsError", { enumerable: true, get: function () { return addNextjsError_1.addNextjsError; } });
|
|
13
|
+
var errorBoundary_1 = require("../domain/error/errorBoundary");
|
|
14
|
+
Object.defineProperty(exports, "ErrorBoundary", { enumerable: true, get: function () { return errorBoundary_1.ErrorBoundary; } });
|
|
15
|
+
//# sourceMappingURL=main.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.js","sourceRoot":"","sources":["../../src/entries/main.ts"],"names":[],"mappings":";;;AAAA,uDAA8E;AAArE,4GAAA,YAAY,OAAA;AAAE,uHAAA,uBAAuB,OAAA;AAE9C,4EAA0E;AAAjE,oHAAA,gBAAgB,OAAA;AACzB,gFAA8E;AAArE,wHAAA,kBAAkB,OAAA;AAC3B,iEAA+D;AAAtD,gHAAA,cAAc,OAAA;AACvB,+DAA6D;AAApD,8GAAA,aAAa,OAAA"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ErrorInfo } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Add a Next.js error to the RUM session.
|
|
4
|
+
*
|
|
5
|
+
* @category Error
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* // app/error.tsx (or app/global-error.tsx)
|
|
9
|
+
* 'use client'
|
|
10
|
+
* import { useEffect } from 'react'
|
|
11
|
+
* import { addNextjsError } from '@datadog/browser-rum-nextjs'
|
|
12
|
+
*
|
|
13
|
+
* export default function Error({ error }: { error: Error & { digest?: string } }) {
|
|
14
|
+
* useEffect(() => {
|
|
15
|
+
* addNextjsError(error)
|
|
16
|
+
* }, [error])
|
|
17
|
+
* return <div>Something went wrong</div>
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare function addNextjsError(error: Error & {
|
|
22
|
+
digest?: string;
|
|
23
|
+
}, errorInfo?: ErrorInfo): void;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { callMonitored, clocksNow, createHandlingStack } from '@datadog/browser-core';
|
|
2
|
+
import { onRumStart } from '../nextjsPlugin';
|
|
3
|
+
/**
|
|
4
|
+
* Add a Next.js error to the RUM session.
|
|
5
|
+
*
|
|
6
|
+
* @category Error
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* // app/error.tsx (or app/global-error.tsx)
|
|
10
|
+
* 'use client'
|
|
11
|
+
* import { useEffect } from 'react'
|
|
12
|
+
* import { addNextjsError } from '@datadog/browser-rum-nextjs'
|
|
13
|
+
*
|
|
14
|
+
* export default function Error({ error }: { error: Error & { digest?: string } }) {
|
|
15
|
+
* useEffect(() => {
|
|
16
|
+
* addNextjsError(error)
|
|
17
|
+
* }, [error])
|
|
18
|
+
* return <div>Something went wrong</div>
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export function addNextjsError(error, errorInfo) {
|
|
23
|
+
const handlingStack = createHandlingStack('nextjs error');
|
|
24
|
+
const startClocks = clocksNow();
|
|
25
|
+
onRumStart((addError) => {
|
|
26
|
+
callMonitored(() => {
|
|
27
|
+
var _a;
|
|
28
|
+
addError({
|
|
29
|
+
error,
|
|
30
|
+
handlingStack,
|
|
31
|
+
componentStack: (_a = errorInfo === null || errorInfo === void 0 ? void 0 : errorInfo.componentStack) !== null && _a !== void 0 ? _a : undefined,
|
|
32
|
+
startClocks,
|
|
33
|
+
context: {
|
|
34
|
+
...error.dd_context,
|
|
35
|
+
...(error.digest && { nextjs: { digest: error.digest } }),
|
|
36
|
+
framework: 'nextjs',
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=addNextjsError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"addNextjsError.js","sourceRoot":"","sources":["../../../src/domain/error/addNextjsError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAA;AAGrF,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAE5C;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,cAAc,CAAC,KAAkC,EAAE,SAAqB;IACtF,MAAM,aAAa,GAAG,mBAAmB,CAAC,cAAc,CAAC,CAAA;IACzD,MAAM,WAAW,GAAG,SAAS,EAAE,CAAA;IAC/B,UAAU,CAAC,CAAC,QAAQ,EAAE,EAAE;QACtB,aAAa,CAAC,GAAG,EAAE;;YACjB,QAAQ,CAAC;gBACP,KAAK;gBACL,aAAa;gBACb,cAAc,EAAE,MAAA,SAAS,aAAT,SAAS,uBAAT,SAAS,CAAE,cAAc,mCAAI,SAAS;gBACtD,WAAW;gBACX,OAAO,EAAE;oBACP,GAAI,KAA0C,CAAC,UAAU;oBACzD,GAAG,CAAC,KAAK,CAAC,MAAM,IAAI,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,EAAE,CAAC;oBACzD,SAAS,EAAE,QAAQ;iBACpB;aACF,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type { ErrorBoundaryFallback, ErrorBoundaryProps } from '@datadog/browser-rum-react/internal';
|
|
2
|
+
/**
|
|
3
|
+
* ErrorBoundary component to report React errors to Datadog using the Next.js error context.
|
|
4
|
+
*
|
|
5
|
+
* For more advanced error handling, you can use the {@link addNextjsError} function.
|
|
6
|
+
*
|
|
7
|
+
* @category Error
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { ErrorBoundary } from '@datadog/browser-rum-nextjs'
|
|
11
|
+
*
|
|
12
|
+
* <ErrorBoundary fallback={() => null}>
|
|
13
|
+
* <Component />
|
|
14
|
+
* </ErrorBoundary>
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare const ErrorBoundary: import("react").ComponentClass<import("@datadog/browser-rum-react/internal").ErrorBoundaryProps, any>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { createErrorBoundary } from '@datadog/browser-rum-react/internal';
|
|
3
|
+
import { addNextjsError } from './addNextjsError';
|
|
4
|
+
/**
|
|
5
|
+
* ErrorBoundary component to report React errors to Datadog using the Next.js error context.
|
|
6
|
+
*
|
|
7
|
+
* For more advanced error handling, you can use the {@link addNextjsError} function.
|
|
8
|
+
*
|
|
9
|
+
* @category Error
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { ErrorBoundary } from '@datadog/browser-rum-nextjs'
|
|
13
|
+
*
|
|
14
|
+
* <ErrorBoundary fallback={() => null}>
|
|
15
|
+
* <Component />
|
|
16
|
+
* </ErrorBoundary>
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
// eslint-disable-next-line local-rules/disallow-side-effects
|
|
20
|
+
export const ErrorBoundary = createErrorBoundary(addNextjsError);
|
|
21
|
+
//# sourceMappingURL=errorBoundary.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errorBoundary.js","sourceRoot":"","sources":["../../../src/domain/error/errorBoundary.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AACZ,OAAO,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAA;AAEzE,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AAEjD;;;;;;;;;;;;;;GAcG;AACH,6DAA6D;AAC7D,MAAM,CAAC,MAAM,aAAa,GAAG,mBAAmB,CAAC,cAAc,CAAC,CAAA"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export function computeViewNameFromParams(pathname, params) {
|
|
2
|
+
if (Object.keys(params).length === 0) {
|
|
3
|
+
return pathname;
|
|
4
|
+
}
|
|
5
|
+
const segments = pathname.split('/');
|
|
6
|
+
// Process catch-all (array) params first to prevent their segments from being
|
|
7
|
+
// consumed by regular param replacements when values overlap.
|
|
8
|
+
for (const [paramName, paramValue] of Object.entries(params)) {
|
|
9
|
+
if (!Array.isArray(paramValue) || paramValue.length === 0) {
|
|
10
|
+
continue;
|
|
11
|
+
}
|
|
12
|
+
for (let i = 0; i < segments.length; i++) {
|
|
13
|
+
if (i + paramValue.length <= segments.length && paramValue.every((v, j) => segments[i + j] === v)) {
|
|
14
|
+
segments.splice(i, paramValue.length, `[...${paramName}]`);
|
|
15
|
+
break;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
// Process regular (string) params greedily left-to-right.
|
|
20
|
+
// When multiple params share the same value, names are assigned left-to-right in param iteration order.
|
|
21
|
+
// When a param value also appears in a static segment, the leftmost occurrence is assumed to be dynamic.
|
|
22
|
+
for (const [paramName, paramValue] of Object.entries(params)) {
|
|
23
|
+
if (typeof paramValue !== 'string' || !paramValue) {
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
const index = segments.indexOf(paramValue);
|
|
27
|
+
if (index !== -1) {
|
|
28
|
+
segments[index] = `[${paramName}]`;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return segments.join('/');
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=computeViewNameFromParams.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"computeViewNameFromParams.js","sourceRoot":"","sources":["../../../src/domain/nextJSRouter/computeViewNameFromParams.ts"],"names":[],"mappings":"AAIA,MAAM,UAAU,yBAAyB,CAAC,QAAgB,EAAE,MAAc;IACxE,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACrC,OAAO,QAAQ,CAAA;IACjB,CAAC;IAED,MAAM,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAEpC,8EAA8E;IAC9E,8DAA8D;IAC9D,KAAK,MAAM,CAAC,SAAS,EAAE,UAAU,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QAC7D,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC1D,SAAQ;QACV,CAAC;QACD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACzC,IAAI,CAAC,GAAG,UAAU,CAAC,MAAM,IAAI,QAAQ,CAAC,MAAM,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;gBAClG,QAAQ,CAAC,MAAM,CAAC,CAAC,EAAE,UAAU,CAAC,MAAM,EAAE,OAAO,SAAS,GAAG,CAAC,CAAA;gBAC1D,MAAK;YACP,CAAC;QACH,CAAC;IACH,CAAC;IAED,0DAA0D;IAC1D,wGAAwG;IACxG,yGAAyG;IACzG,KAAK,MAAM,CAAC,SAAS,EAAE,UAAU,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QAC7D,IAAI,OAAO,UAAU,KAAK,QAAQ,IAAI,CAAC,UAAU,EAAE,CAAC;YAClD,SAAQ;QACV,CAAC;QACD,MAAM,KAAK,GAAG,QAAQ,CAAC,OAAO,CAAC,UAAU,CAAC,CAAA;QAC1C,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC;YACjB,QAAQ,CAAC,KAAK,CAAC,GAAG,IAAI,SAAS,GAAG,CAAA;QACpC,CAAC;IACH,CAAC;IAED,OAAO,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;AAC3B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function DatadogAppRouter(): null;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useRef } from 'react';
|
|
3
|
+
import { usePathname, useParams } from 'next/navigation';
|
|
4
|
+
import { mockable } from '@datadog/browser-core';
|
|
5
|
+
import { startNextjsView } from '../nextjsPlugin';
|
|
6
|
+
import { computeViewNameFromParams } from './computeViewNameFromParams';
|
|
7
|
+
export function DatadogAppRouter() {
|
|
8
|
+
const pathname = mockable(usePathname)();
|
|
9
|
+
const params = mockable(useParams)();
|
|
10
|
+
const previousPathname = mockable(useRef)(null);
|
|
11
|
+
if (previousPathname.current !== pathname) {
|
|
12
|
+
previousPathname.current = pathname;
|
|
13
|
+
startNextjsView(computeViewNameFromParams(pathname, params));
|
|
14
|
+
}
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=datadogAppRouter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"datadogAppRouter.js","sourceRoot":"","sources":["../../../src/domain/nextJSRouter/datadogAppRouter.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAA;AAC9B,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAA;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAA;AAEvE,MAAM,UAAU,gBAAgB;IAC9B,MAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAA;IACxC,MAAM,MAAM,GAAG,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAA;IACpC,MAAM,gBAAgB,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAgB,IAAI,CAAC,CAAA;IAE9D,IAAI,gBAAgB,CAAC,OAAO,KAAK,QAAQ,EAAE,CAAC;QAC1C,gBAAgB,CAAC,OAAO,GAAG,QAAQ,CAAA;QACnC,eAAe,CAAC,yBAAyB,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAA;IAC9D,CAAC;IAED,OAAO,IAAI,CAAA;AACb,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function DatadogPagesRouter(): null;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useRef } from 'react';
|
|
2
|
+
import { useRouter } from 'next/router';
|
|
3
|
+
import { mockable } from '@datadog/browser-core';
|
|
4
|
+
import { startNextjsView } from '../nextjsPlugin';
|
|
5
|
+
export function DatadogPagesRouter() {
|
|
6
|
+
const router = mockable(useRouter)();
|
|
7
|
+
const previousPath = mockable(useRef)(null);
|
|
8
|
+
if (!router.isReady) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
// Extract the path portion of asPath (without query params or hash) to detect navigations.
|
|
12
|
+
const path = router.asPath.split(/[?#]/)[0];
|
|
13
|
+
if (previousPath.current !== path) {
|
|
14
|
+
// router.pathname is the route pattern (e.g., "/user/[id]") — used as the view name
|
|
15
|
+
// router.asPath is the actual URL (e.g., "/user/42") — used to detect navigations between
|
|
16
|
+
// different concrete URLs of the same dynamic route (e.g., /user/42 → /user/43)
|
|
17
|
+
previousPath.current = path;
|
|
18
|
+
startNextjsView(router.pathname);
|
|
19
|
+
}
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=datadogPagesRouter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"datadogPagesRouter.js","sourceRoot":"","sources":["../../../src/domain/nextJSRouter/datadogPagesRouter.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAA;AAC9B,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAA;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAEjD,MAAM,UAAU,kBAAkB;IAChC,MAAM,MAAM,GAAG,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAA;IACpC,MAAM,YAAY,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAgB,IAAI,CAAC,CAAA;IAE1D,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QACpB,OAAO,IAAI,CAAA;IACb,CAAC;IAED,2FAA2F;IAC3F,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAA;IAE3C,IAAI,YAAY,CAAC,OAAO,KAAK,IAAI,EAAE,CAAC;QAClC,oFAAoF;QACpF,0FAA0F;QAC1F,gFAAgF;QAChF,YAAY,CAAC,OAAO,GAAG,IAAI,CAAA;QAC3B,eAAe,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;IAClC,CAAC;IAED,OAAO,IAAI,CAAA;AACb,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { RumPlugin, RumPublicApi, StartRumResult } from '@datadog/browser-rum-core';
|
|
2
|
+
export type NextjsPlugin = Pick<Required<RumPlugin>, 'name' | 'onInit' | 'onRumStart'>;
|
|
3
|
+
type InitSubscriber = (rumPublicApi: RumPublicApi) => void;
|
|
4
|
+
type StartSubscriber = (addError: StartRumResult['addError']) => void;
|
|
5
|
+
export declare function nextjsPlugin(): NextjsPlugin;
|
|
6
|
+
export declare function startNextjsView(viewName: string): void;
|
|
7
|
+
export declare function onRouterTransitionStart(url: string): void;
|
|
8
|
+
export declare function onRumInit(callback: InitSubscriber): void;
|
|
9
|
+
export declare function onRumStart(callback: StartSubscriber): void;
|
|
10
|
+
export declare function resetNextjsPlugin(): void;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { buildUrl } from '@datadog/browser-core';
|
|
2
|
+
let globalPublicApi;
|
|
3
|
+
let globalAddError;
|
|
4
|
+
let lastNavigationUrl;
|
|
5
|
+
const onRumInitSubscribers = [];
|
|
6
|
+
const onRumStartSubscribers = [];
|
|
7
|
+
export function nextjsPlugin() {
|
|
8
|
+
return {
|
|
9
|
+
name: 'nextjs',
|
|
10
|
+
onInit({ publicApi, initConfiguration }) {
|
|
11
|
+
globalPublicApi = publicApi;
|
|
12
|
+
initConfiguration.trackViewsManually = true;
|
|
13
|
+
for (const subscriber of onRumInitSubscribers) {
|
|
14
|
+
subscriber(publicApi);
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
onRumStart({ addError }) {
|
|
18
|
+
globalAddError = addError;
|
|
19
|
+
if (addError) {
|
|
20
|
+
for (const subscriber of onRumStartSubscribers) {
|
|
21
|
+
subscriber(addError);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export function startNextjsView(viewName) {
|
|
28
|
+
if (globalPublicApi) {
|
|
29
|
+
// Use the URL captured by onRouterTransitionStart if available, since React renders before pushState updates window.location
|
|
30
|
+
const url = lastNavigationUrl ? buildUrl(lastNavigationUrl, window.location.origin).href : undefined;
|
|
31
|
+
lastNavigationUrl = undefined;
|
|
32
|
+
globalPublicApi.startView({ name: viewName, url });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
// Must be re-exported from the user's instrumentation-client.ts so we can capture the URL before React renders
|
|
36
|
+
export function onRouterTransitionStart(url) {
|
|
37
|
+
lastNavigationUrl = url;
|
|
38
|
+
}
|
|
39
|
+
export function onRumInit(callback) {
|
|
40
|
+
if (globalPublicApi) {
|
|
41
|
+
callback(globalPublicApi);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
onRumInitSubscribers.push(callback);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export function onRumStart(callback) {
|
|
48
|
+
if (globalAddError) {
|
|
49
|
+
callback(globalAddError);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
onRumStartSubscribers.push(callback);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export function resetNextjsPlugin() {
|
|
56
|
+
globalPublicApi = undefined;
|
|
57
|
+
globalAddError = undefined;
|
|
58
|
+
onRumInitSubscribers.length = 0;
|
|
59
|
+
onRumStartSubscribers.length = 0;
|
|
60
|
+
lastNavigationUrl = undefined;
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=nextjsPlugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nextjsPlugin.js","sourceRoot":"","sources":["../../src/domain/nextjsPlugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAA;AAQhD,IAAI,eAAyC,CAAA;AAC7C,IAAI,cAAsD,CAAA;AAC1D,IAAI,iBAAqC,CAAA;AAEzC,MAAM,oBAAoB,GAAqB,EAAE,CAAA;AACjD,MAAM,qBAAqB,GAAsB,EAAE,CAAA;AAEnD,MAAM,UAAU,YAAY;IAC1B,OAAO;QACL,IAAI,EAAE,QAAQ;QACd,MAAM,CAAC,EAAE,SAAS,EAAE,iBAAiB,EAAE;YACrC,eAAe,GAAG,SAAS,CAAA;YAC3B,iBAAiB,CAAC,kBAAkB,GAAG,IAAI,CAAA;YAE3C,KAAK,MAAM,UAAU,IAAI,oBAAoB,EAAE,CAAC;gBAC9C,UAAU,CAAC,SAAS,CAAC,CAAA;YACvB,CAAC;QACH,CAAC;QACD,UAAU,CAAC,EAAE,QAAQ,EAAE;YACrB,cAAc,GAAG,QAAQ,CAAA;YACzB,IAAI,QAAQ,EAAE,CAAC;gBACb,KAAK,MAAM,UAAU,IAAI,qBAAqB,EAAE,CAAC;oBAC/C,UAAU,CAAC,QAAQ,CAAC,CAAA;gBACtB,CAAC;YACH,CAAC;QACH,CAAC;KACkB,CAAA;AACvB,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,QAAgB;IAC9C,IAAI,eAAe,EAAE,CAAC;QACpB,6HAA6H;QAC7H,MAAM,GAAG,GAAG,iBAAiB,CAAC,CAAC,CAAC,QAAQ,CAAC,iBAAiB,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAA;QACpG,iBAAiB,GAAG,SAAS,CAAA;QAC7B,eAAe,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,CAAC,CAAA;IACpD,CAAC;AACH,CAAC;AAED,+GAA+G;AAC/G,MAAM,UAAU,uBAAuB,CAAC,GAAW;IACjD,iBAAiB,GAAG,GAAG,CAAA;AACzB,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,QAAwB;IAChD,IAAI,eAAe,EAAE,CAAC;QACpB,QAAQ,CAAC,eAAe,CAAC,CAAA;IAC3B,CAAC;SAAM,CAAC;QACN,oBAAoB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IACrC,CAAC;AACH,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,QAAyB;IAClD,IAAI,cAAc,EAAE,CAAC;QACnB,QAAQ,CAAC,cAAc,CAAC,CAAA;IAC1B,CAAC;SAAM,CAAC;QACN,qBAAqB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IACtC,CAAC;AACH,CAAC;AAED,MAAM,UAAU,iBAAiB;IAC/B,eAAe,GAAG,SAAS,CAAA;IAC3B,cAAc,GAAG,SAAS,CAAA;IAC1B,oBAAoB,CAAC,MAAM,GAAG,CAAC,CAAA;IAC/B,qBAAqB,CAAC,MAAM,GAAG,CAAC,CAAA;IAChC,iBAAiB,GAAG,SAAS,CAAA;AAC/B,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { nextjsPlugin, onRouterTransitionStart } from '../domain/nextjsPlugin';
|
|
2
|
+
export type { NextjsPlugin } from '../domain/nextjsPlugin';
|
|
3
|
+
export { DatadogAppRouter } from '../domain/nextJSRouter/datadogAppRouter';
|
|
4
|
+
export { DatadogPagesRouter } from '../domain/nextJSRouter/datadogPagesRouter';
|
|
5
|
+
export { addNextjsError } from '../domain/error/addNextjsError';
|
|
6
|
+
export { ErrorBoundary } from '../domain/error/errorBoundary';
|
|
7
|
+
export type { ErrorBoundaryFallback, ErrorBoundaryProps } from '../domain/error/errorBoundary';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { nextjsPlugin, onRouterTransitionStart } from '../domain/nextjsPlugin';
|
|
2
|
+
export { DatadogAppRouter } from '../domain/nextJSRouter/datadogAppRouter';
|
|
3
|
+
export { DatadogPagesRouter } from '../domain/nextJSRouter/datadogPagesRouter';
|
|
4
|
+
export { addNextjsError } from '../domain/error/addNextjsError';
|
|
5
|
+
export { ErrorBoundary } from '../domain/error/errorBoundary';
|
|
6
|
+
//# sourceMappingURL=main.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.js","sourceRoot":"","sources":["../../src/entries/main.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAA;AAE9E,OAAO,EAAE,gBAAgB,EAAE,MAAM,yCAAyC,CAAA;AAC1E,OAAO,EAAE,kBAAkB,EAAE,MAAM,2CAA2C,CAAA;AAC9E,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAA;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@datadog/browser-rum-nextjs",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
|
|
3
|
+
"version": "6.32.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"main": "cjs/entries/main.js",
|
|
6
|
+
"module": "esm/entries/main.js",
|
|
7
|
+
"types": "cjs/entries/main.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"cjs",
|
|
10
|
+
"esm",
|
|
11
|
+
"src",
|
|
12
|
+
"!src/**/*.spec.*",
|
|
13
|
+
"!src/**/*.specHelper.*"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "node ../../scripts/build/build-package.ts --modules",
|
|
17
|
+
"prepack": "yarn build"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@datadog/browser-core": "6.32.0",
|
|
21
|
+
"@datadog/browser-rum-core": "6.32.0",
|
|
22
|
+
"@datadog/browser-rum-react": "6.32.0"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"next": ">=13.0.0",
|
|
26
|
+
"react": ">=18.0.0"
|
|
27
|
+
},
|
|
28
|
+
"peerDependenciesMeta": {
|
|
29
|
+
"next": {
|
|
30
|
+
"optional": true
|
|
31
|
+
},
|
|
32
|
+
"react": {
|
|
33
|
+
"optional": true
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "https://github.com/DataDog/browser-sdk.git",
|
|
39
|
+
"directory": "packages/rum-nextjs"
|
|
40
|
+
},
|
|
41
|
+
"volta": {
|
|
42
|
+
"extends": "../../package.json"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/react": "19.2.11",
|
|
46
|
+
"next": "15.5.10",
|
|
47
|
+
"react": "19.2.4",
|
|
48
|
+
"react-dom": "19.2.4"
|
|
6
49
|
}
|
|
7
50
|
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { callMonitored, clocksNow, createHandlingStack } from '@datadog/browser-core'
|
|
2
|
+
import type { Context } from '@datadog/browser-core'
|
|
3
|
+
import type { ErrorInfo } from 'react'
|
|
4
|
+
import { onRumStart } from '../nextjsPlugin'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Add a Next.js error to the RUM session.
|
|
8
|
+
*
|
|
9
|
+
* @category Error
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* // app/error.tsx (or app/global-error.tsx)
|
|
13
|
+
* 'use client'
|
|
14
|
+
* import { useEffect } from 'react'
|
|
15
|
+
* import { addNextjsError } from '@datadog/browser-rum-nextjs'
|
|
16
|
+
*
|
|
17
|
+
* export default function Error({ error }: { error: Error & { digest?: string } }) {
|
|
18
|
+
* useEffect(() => {
|
|
19
|
+
* addNextjsError(error)
|
|
20
|
+
* }, [error])
|
|
21
|
+
* return <div>Something went wrong</div>
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function addNextjsError(error: Error & { digest?: string }, errorInfo?: ErrorInfo) {
|
|
26
|
+
const handlingStack = createHandlingStack('nextjs error')
|
|
27
|
+
const startClocks = clocksNow()
|
|
28
|
+
onRumStart((addError) => {
|
|
29
|
+
callMonitored(() => {
|
|
30
|
+
addError({
|
|
31
|
+
error,
|
|
32
|
+
handlingStack,
|
|
33
|
+
componentStack: errorInfo?.componentStack ?? undefined,
|
|
34
|
+
startClocks,
|
|
35
|
+
context: {
|
|
36
|
+
...(error as Error & { dd_context?: Context }).dd_context,
|
|
37
|
+
...(error.digest && { nextjs: { digest: error.digest } }),
|
|
38
|
+
framework: 'nextjs',
|
|
39
|
+
},
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
})
|
|
43
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { createErrorBoundary } from '@datadog/browser-rum-react/internal'
|
|
3
|
+
export type { ErrorBoundaryFallback, ErrorBoundaryProps } from '@datadog/browser-rum-react/internal'
|
|
4
|
+
import { addNextjsError } from './addNextjsError'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* ErrorBoundary component to report React errors to Datadog using the Next.js error context.
|
|
8
|
+
*
|
|
9
|
+
* For more advanced error handling, you can use the {@link addNextjsError} function.
|
|
10
|
+
*
|
|
11
|
+
* @category Error
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { ErrorBoundary } from '@datadog/browser-rum-nextjs'
|
|
15
|
+
*
|
|
16
|
+
* <ErrorBoundary fallback={() => null}>
|
|
17
|
+
* <Component />
|
|
18
|
+
* </ErrorBoundary>
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
// eslint-disable-next-line local-rules/disallow-side-effects
|
|
22
|
+
export const ErrorBoundary = createErrorBoundary(addNextjsError)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { useParams } from 'next/navigation'
|
|
2
|
+
|
|
3
|
+
type Params = ReturnType<typeof useParams>
|
|
4
|
+
|
|
5
|
+
export function computeViewNameFromParams(pathname: string, params: Params): string {
|
|
6
|
+
if (Object.keys(params).length === 0) {
|
|
7
|
+
return pathname
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const segments = pathname.split('/')
|
|
11
|
+
|
|
12
|
+
// Process catch-all (array) params first to prevent their segments from being
|
|
13
|
+
// consumed by regular param replacements when values overlap.
|
|
14
|
+
for (const [paramName, paramValue] of Object.entries(params)) {
|
|
15
|
+
if (!Array.isArray(paramValue) || paramValue.length === 0) {
|
|
16
|
+
continue
|
|
17
|
+
}
|
|
18
|
+
for (let i = 0; i < segments.length; i++) {
|
|
19
|
+
if (i + paramValue.length <= segments.length && paramValue.every((v, j) => segments[i + j] === v)) {
|
|
20
|
+
segments.splice(i, paramValue.length, `[...${paramName}]`)
|
|
21
|
+
break
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Process regular (string) params greedily left-to-right.
|
|
27
|
+
// When multiple params share the same value, names are assigned left-to-right in param iteration order.
|
|
28
|
+
// When a param value also appears in a static segment, the leftmost occurrence is assumed to be dynamic.
|
|
29
|
+
for (const [paramName, paramValue] of Object.entries(params)) {
|
|
30
|
+
if (typeof paramValue !== 'string' || !paramValue) {
|
|
31
|
+
continue
|
|
32
|
+
}
|
|
33
|
+
const index = segments.indexOf(paramValue)
|
|
34
|
+
if (index !== -1) {
|
|
35
|
+
segments[index] = `[${paramName}]`
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return segments.join('/')
|
|
40
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useRef } from 'react'
|
|
4
|
+
import { usePathname, useParams } from 'next/navigation'
|
|
5
|
+
import { mockable } from '@datadog/browser-core'
|
|
6
|
+
import { startNextjsView } from '../nextjsPlugin'
|
|
7
|
+
import { computeViewNameFromParams } from './computeViewNameFromParams'
|
|
8
|
+
|
|
9
|
+
export function DatadogAppRouter() {
|
|
10
|
+
const pathname = mockable(usePathname)()
|
|
11
|
+
const params = mockable(useParams)()
|
|
12
|
+
const previousPathname = mockable(useRef)<string | null>(null)
|
|
13
|
+
|
|
14
|
+
if (previousPathname.current !== pathname) {
|
|
15
|
+
previousPathname.current = pathname
|
|
16
|
+
startNextjsView(computeViewNameFromParams(pathname, params))
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return null
|
|
20
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useRef } from 'react'
|
|
2
|
+
import { useRouter } from 'next/router'
|
|
3
|
+
import { mockable } from '@datadog/browser-core'
|
|
4
|
+
import { startNextjsView } from '../nextjsPlugin'
|
|
5
|
+
|
|
6
|
+
export function DatadogPagesRouter() {
|
|
7
|
+
const router = mockable(useRouter)()
|
|
8
|
+
const previousPath = mockable(useRef)<string | null>(null)
|
|
9
|
+
|
|
10
|
+
if (!router.isReady) {
|
|
11
|
+
return null
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Extract the path portion of asPath (without query params or hash) to detect navigations.
|
|
15
|
+
const path = router.asPath.split(/[?#]/)[0]
|
|
16
|
+
|
|
17
|
+
if (previousPath.current !== path) {
|
|
18
|
+
// router.pathname is the route pattern (e.g., "/user/[id]") — used as the view name
|
|
19
|
+
// router.asPath is the actual URL (e.g., "/user/42") — used to detect navigations between
|
|
20
|
+
// different concrete URLs of the same dynamic route (e.g., /user/42 → /user/43)
|
|
21
|
+
previousPath.current = path
|
|
22
|
+
startNextjsView(router.pathname)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return null
|
|
26
|
+
}
|