@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,74 @@
|
|
|
1
|
+
import { buildUrl } from '@datadog/browser-core'
|
|
2
|
+
import type { RumPlugin, RumPublicApi, StartRumResult } from '@datadog/browser-rum-core'
|
|
3
|
+
|
|
4
|
+
export type NextjsPlugin = Pick<Required<RumPlugin>, 'name' | 'onInit' | 'onRumStart'>
|
|
5
|
+
|
|
6
|
+
type InitSubscriber = (rumPublicApi: RumPublicApi) => void
|
|
7
|
+
type StartSubscriber = (addError: StartRumResult['addError']) => void
|
|
8
|
+
|
|
9
|
+
let globalPublicApi: RumPublicApi | undefined
|
|
10
|
+
let globalAddError: StartRumResult['addError'] | undefined
|
|
11
|
+
let lastNavigationUrl: string | undefined
|
|
12
|
+
|
|
13
|
+
const onRumInitSubscribers: InitSubscriber[] = []
|
|
14
|
+
const onRumStartSubscribers: StartSubscriber[] = []
|
|
15
|
+
|
|
16
|
+
export function nextjsPlugin(): NextjsPlugin {
|
|
17
|
+
return {
|
|
18
|
+
name: 'nextjs',
|
|
19
|
+
onInit({ publicApi, initConfiguration }) {
|
|
20
|
+
globalPublicApi = publicApi
|
|
21
|
+
initConfiguration.trackViewsManually = true
|
|
22
|
+
|
|
23
|
+
for (const subscriber of onRumInitSubscribers) {
|
|
24
|
+
subscriber(publicApi)
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
onRumStart({ addError }) {
|
|
28
|
+
globalAddError = addError
|
|
29
|
+
if (addError) {
|
|
30
|
+
for (const subscriber of onRumStartSubscribers) {
|
|
31
|
+
subscriber(addError)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
} satisfies RumPlugin
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function startNextjsView(viewName: string) {
|
|
39
|
+
if (globalPublicApi) {
|
|
40
|
+
// Use the URL captured by onRouterTransitionStart if available, since React renders before pushState updates window.location
|
|
41
|
+
const url = lastNavigationUrl ? buildUrl(lastNavigationUrl, window.location.origin).href : undefined
|
|
42
|
+
lastNavigationUrl = undefined
|
|
43
|
+
globalPublicApi.startView({ name: viewName, url })
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Must be re-exported from the user's instrumentation-client.ts so we can capture the URL before React renders
|
|
48
|
+
export function onRouterTransitionStart(url: string) {
|
|
49
|
+
lastNavigationUrl = url
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function onRumInit(callback: InitSubscriber) {
|
|
53
|
+
if (globalPublicApi) {
|
|
54
|
+
callback(globalPublicApi)
|
|
55
|
+
} else {
|
|
56
|
+
onRumInitSubscribers.push(callback)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function onRumStart(callback: StartSubscriber) {
|
|
61
|
+
if (globalAddError) {
|
|
62
|
+
callback(globalAddError)
|
|
63
|
+
} else {
|
|
64
|
+
onRumStartSubscribers.push(callback)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function resetNextjsPlugin() {
|
|
69
|
+
globalPublicApi = undefined
|
|
70
|
+
globalAddError = undefined
|
|
71
|
+
onRumInitSubscribers.length = 0
|
|
72
|
+
onRumStartSubscribers.length = 0
|
|
73
|
+
lastNavigationUrl = undefined
|
|
74
|
+
}
|
|
@@ -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'
|