Skip to content

Commit

Permalink
[RUM][REPLAY] Try to reduce the size of the replay payload (#2348)
Browse files Browse the repository at this point in the history
* [RUM][REPLAY] Try to reduce the size of the replay payload

Add experimental flag to disable CSS inlining

* [RUM][REPLAY] Simplify test

* [RUM][REPLAY] Rename test label for clarity

Co-authored-by: Benoît <benoit.zugmeyer@datadoghq.com>

---------

Co-authored-by: Benoît <benoit.zugmeyer@datadoghq.com>
  • Loading branch information
ThibautGeriz and BenoitZugmeyer authored Jul 27, 2023
1 parent 4c23a3c commit e15c2fb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/core/src/tools/experimentalFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export enum ExperimentalFeature {
COLLECT_FLUSH_REASON = 'collect_flush_reason',
NO_RESOURCE_DURATION_FROZEN_STATE = 'no_resource_duration_frozen_state',
SCROLLMAP = 'scrollmap',
DISABLE_REPLAY_INLINE_CSS = 'disable_replay_inline_css',
}

const enabledExperimentalFeatures: Set<ExperimentalFeature> = new Set()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ExperimentalFeature, isExperimentalFeatureEnabled } from '@datadog/browser-core'

import { NodePrivacyLevel } from '../../../constants'
import { shouldMaskNode } from '../privacy'
import { getElementInputValue, switchToAbsoluteUrl, getValidTagName } from './serializationUtils'
Expand Down Expand Up @@ -51,7 +53,7 @@ export function serializeAttributes(
if (tagName === 'link') {
const stylesheet = Array.from(doc.styleSheets).find((s) => s.href === (element as HTMLLinkElement).href)
const cssText = getCssRulesString(stylesheet)
if (cssText && stylesheet) {
if (cssText && stylesheet && !isExperimentalFeatureEnabled(ExperimentalFeature.DISABLE_REPLAY_INLINE_CSS)) {
safeAttrs._cssText = cssText
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { isIE, noop } from '@datadog/browser-core'
import {
ExperimentalFeature,
addExperimentalFeatures,
isIE,
noop,
resetExperimentalFeatures,
} from '@datadog/browser-core'

import type { RumConfiguration } from '@datadog/browser-rum-core'
import { isAdoptedStyleSheetsSupported } from '@datadog/browser-core/test'
Expand Down Expand Up @@ -71,6 +77,7 @@ describe('serializeNodeWithId', () => {

afterEach(() => {
sandbox.remove()
resetExperimentalFeatures()
})

describe('document serialization', () => {
Expand Down Expand Up @@ -588,6 +595,24 @@ describe('serializeNodeWithId', () => {
})
})

it('does not inline external style sheets when DISABLE_REPLAY_INLINE_CSS is enabled', () => {
addExperimentalFeatures([ExperimentalFeature.DISABLE_REPLAY_INLINE_CSS])
const linkNode = document.createElement('link')
linkNode.setAttribute('rel', 'stylesheet')
linkNode.setAttribute('href', 'https://datadoghq.com/some/style.css')
isolatedDom.document.head.appendChild(linkNode)
Object.defineProperty(isolatedDom.document, 'styleSheets', {
value: [
{
href: 'https://datadoghq.com/some/style.css',
cssRules: [{ cssText: 'body { width: 100%; }' }],
},
],
})

expect((serializeNodeWithId(linkNode, DEFAULT_OPTIONS) as ElementNode).attributes._cssText).toBeUndefined()
})

it('does not serialize style node with dynamic CSS that is behind CORS', () => {
const linkNode = document.createElement('link')
linkNode.setAttribute('rel', 'stylesheet')
Expand Down

0 comments on commit e15c2fb

Please sign in to comment.