Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Implement configurable throttling on mutation emission using the new …
…`sampling.mutation` ms setting

 - can prevent overly frequent (javascript based) animation per second on the same attribute; the mutation buffer will emit a single change at the end if throttled changes are against the same element
  • Loading branch information
eoghanmurray committed May 24, 2025
commit e1ad4a4849c83c7305d00a090a6c9b82ca1834e0
4 changes: 3 additions & 1 deletion docs/recipes/optimize-storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Some common patterns may emit lots of events are:

## Sampling

Use the sampling config in the recording can reduce the storage size by dropping some events:
Use the sampling config in the recording can reduce the storage size by dropping or merging some events:

**Scenario 1**

Expand All @@ -35,6 +35,8 @@ rrweb.record({
mouseInteraction: false
// set the interval of scrolling event
scroll: 150 // do not emit twice in 150ms
// set the interval of mutation events (page changes)
mutation: 50 // do not emit more than 20 mutations per second
// set the interval of media interaction event
media: 800
// set the timing of record input
Expand Down
7 changes: 7 additions & 0 deletions packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
inDom,
getShadowHost,
closestElementOfNode,
throttle,
} from '../utils';
import dom from '@rrweb/utils';

Expand Down Expand Up @@ -184,6 +185,7 @@ export default class MutationBuffer {
private recordCanvas: observerParam['recordCanvas'];
private inlineImages: observerParam['inlineImages'];
private slimDOMOptions: observerParam['slimDOMOptions'];
private sampling: observerParam['sampling'];
private dataURLOptions: observerParam['dataURLOptions'];
private doc: observerParam['doc'];
private mirror: observerParam['mirror'];
Expand All @@ -210,6 +212,7 @@ export default class MutationBuffer {
'recordCanvas',
'inlineImages',
'slimDOMOptions',
'sampling',
'dataURLOptions',
'doc',
'mirror',
Expand All @@ -223,6 +226,10 @@ export default class MutationBuffer {
// just a type trick, the runtime result is correct
this[key] = options[key] as never;
});

if (this.sampling.mutation) {
this.emit = throttle(this.emit, this.sampling.mutation);
}
}

public freeze() {
Expand Down
1 change: 1 addition & 0 deletions packages/rrweb/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export type MutationBufferParam = Pick<
| 'recordCanvas'
| 'inlineImages'
| 'slimDOMOptions'
| 'sampling'
| 'dataURLOptions'
| 'doc'
| 'mirror'
Expand Down
8 changes: 7 additions & 1 deletion packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,15 @@ export type SamplingStrategy = Partial<{
*/
mouseInteraction: boolean | Record<string, boolean | undefined>;
/**
* number is the throttle threshold of recording scroll
* number is the throttle threshold of recording scroll in ms
* default: 100
*/
scroll: number;
/**
* number is the throttle threshold of mutation emission in ms
* off by default
*/
mutation: number;
/**
* number is the throttle threshold of recording media interactions
*/
Expand Down
Loading