71b6e683c01fa0737494211c9744e2e6bf8ede66
slimDOM option to strip out unnecessary parts of the DOM (#36)
* Add a `slimDOM` option to strip out unnecessary parts of the DOM in terms of replay - <script> tags in the <head> take up unnecessary storage space and are often injected semi randomly to become a source of unnecessary variation between recordings of the same thing - comment tags can be stripped out without affecting display - future: this option could also turn on more aggressive stripping, e.g. elements that are hidden by CSS (assuming we can handle them becoming visible after mutation events) * Mark nodes ignored due to slimDOM option, so that they can also be ignored by the mutation observer in rrweb * Introducing the `ignored` attribute violates the `serializedNodeWithId` type * slimDOM: Strip out whitespace nodes from <head> element as they have no effect but take up space - these would otherwise have to be merged after <script> elements are removed; for statcounter usecase, removing <script> elements is no good if there is still a trace of their presence due to the white space (and hence a variant <head> node is still produced) - I explored a more radical stripping of all white space nodes, but there is a problem if parent node is <pre> or otherwise rendered with `white-space: pre` and similar. detecting applied styles with getComputedStyle would be very expensive (I haven't measured it though) * Export IGNORED_NODE as a constant instead of relying on the hard-to-grok `-2` * Remove <link rel=preload as=script> which are similarly as useless as <script> tags * Make slimDOM configurable with the expecations that `slimDOMOptions: true` will only enable non-destructive options (so not all options may be turned on) * Expand slimDOM to add options to remove more elements from the <head> that should not be necessary in the replayer context
rrweb-snapshot
Snapshot the DOM into a stateful and serializable data structure. Also, provide the ability to rebuild the DOM via snapshot.
API
This module export following methods:
snapshot
snapshot will traverse the DOM and return a stateful and serializable data structure which can represent the current DOM view.
There are several things will be done during snapshot:
- Inline some DOM states into HTML attributes, e.g, HTMLInputElement's value.
- Turn script tags into
noscripttags to avoid scripts being executed. - Try to inline stylesheets to make sure local stylesheets can be used.
- Make relative paths in href, src, CSS to be absolute paths.
- Give an id to each Node, and return the id node map when snapshot finished.
rebuild
rebuild will build the DOM according to the taken snapshot.
There are several things will be done during rebuild:
- Add data-rrid attribute if the Node is an Element.
- Create some extra DOM node like text node to place inline CSS and some states.
- Add data-extra-child-index attribute if Node has some extra child DOM.
serializeNodeWithId
serializeNodeWithId can serialize a node into snapshot format with id.
buildNodeWithSN
buildNodeWithSN will build DOM from serialized node and store serialized information in __sn property.
Languages
TypeScript
66.5%
JavaScript
25.8%
HTML
6.3%
Svelte
1.2%
CSS
0.2%