Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/core/src/tools/globalObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ export function getGlobalObject<T = typeof globalThis>(): T {
// eslint-disable-next-line local-rules/disallow-side-effects
export const globalObject = getGlobalObject<GlobalObject>()

export const isBrowserEnvironment = 'document' in globalObject
export const isWorkerEnvironment = 'WorkerGlobalScope' in globalObject
export const isNodeEnvironment =
// @ts-expect-error for Node.js-specific globals that are not present in browser environments
typeof process !== 'undefined' && process.versions !== null && !!process.versions.node
5 changes: 5 additions & 0 deletions packages/logs/src/boot/preStartLogs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TrackingConsentState } from '@datadog/browser-core'
import {
isNodeEnvironment,
createBoundedBuffer,
canUseEventBridge,
display,
Expand Down Expand Up @@ -55,6 +56,10 @@ export function createPreStartStrategy(

return {
init(initConfiguration, errorStack) {
if (isNodeEnvironment) {
return
}

if (!initConfiguration) {
display.error('Missing configuration')
return
Expand Down
4 changes: 2 additions & 2 deletions packages/logs/src/boot/startLogs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TrackingConsentState, BufferedObservable, BufferedData, PageMayExitEvent } from '@datadog/browser-core'
import {
isBrowserEnvironment,
Observable,
sendToExtension,
createPageMayExitObservable,
Expand All @@ -11,7 +12,6 @@ import {
TelemetryService,
createIdentityEncoder,
startUserContext,
isWorkerEnvironment,
} from '@datadog/browser-core'
import { startLogsSessionManager, startLogsSessionManagerStub } from '../domain/logsSessionManager'
import type { LogsConfiguration } from '../domain/configuration'
Expand Down Expand Up @@ -55,7 +55,7 @@ export function startLogs(

const reportError = startReportError(lifeCycle)
// Page exit is not observable in worker environments (no window/document events)
const pageMayExitObservable = isWorkerEnvironment
const pageMayExitObservable = !isBrowserEnvironment
? new Observable<PageMayExitEvent>()
: createPageMayExitObservable(configuration)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FetchResolveContext, XhrCompleteContext } from '@datadog/browser-core'
import {
isWorkerEnvironment,
isBrowserEnvironment,
Observable,
ErrorSource,
initXhrObservable,
Expand Down Expand Up @@ -28,7 +28,7 @@ export function startNetworkErrorCollection(configuration: LogsConfiguration, li

// XHR is not available in web workers, so we use an empty observable that never emits
const xhrSubscription = (
isWorkerEnvironment ? new Observable<XhrCompleteContext>() : initXhrObservable(configuration)
!isBrowserEnvironment ? new Observable<XhrCompleteContext>() : initXhrObservable(configuration)
).subscribe((context) => {
if (context.state === 'complete') {
handleResponse(RequestType.XHR, context)
Expand Down
5 changes: 5 additions & 0 deletions packages/rum-core/src/boot/preStartRum.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TrackingConsentState, DeflateWorker, Context, ContextManager, BoundedBuffer } from '@datadog/browser-core'
import {
isNodeEnvironment,
createBoundedBuffer,
display,
canUseEventBridge,
Expand Down Expand Up @@ -100,6 +101,10 @@ export function createPreStartStrategy(
}

function doInit(initConfiguration: RumInitConfiguration, errorStack?: string) {
if (isNodeEnvironment) {
return
}
Comment on lines +104 to +106
Copy link
Collaborator

Choose a reason for hiding this comment

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

💬 suggestion: ‏what about doing this check earlier in the init (like for logs)? a lot can happen before doInit is called


const eventBridgeAvailable = canUseEventBridge()
if (eventBridgeAvailable) {
initConfiguration = overrideInitConfigurationForBridge(initConfiguration)
Expand Down
19 changes: 17 additions & 2 deletions test/apps/vanilla/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,23 @@ if (typeof window !== 'undefined') {
window.RUM_INIT()
}
} else {
// Document is always generated by the SSR environment
// @ts-ignore If document is mocked, the SDK executes further enough to throw an error
globalThis.document = {}

// Check if the SDK sent any events
;(globalThis as any).__ddBrowserSdkExtensionCallback = () => {
throw new Error('the SDK should not send events')
}

// compat test
datadogLogs.init({ clientToken: 'xxx', beforeSend: undefined })
datadogRum.init({ clientToken: 'xxx', applicationId: 'xxx', beforeSend: undefined })

datadogLogs.init({ clientToken: 'xxx', beforeSend: undefined, telemetrySampleRate: 100 })
datadogRum.init({ clientToken: 'xxx', applicationId: 'xxx', beforeSend: undefined, telemetrySampleRate: 100 })
datadogRum.setUser({ id: undefined })

// Check the SDK is not started
if (datadogLogs.getInternalContext() || datadogRum.getInternalContext()) {
throw new Error('SDK should not start on SSR environments')
}
}