|
| 1 | +# network recorder and replayer |
| 2 | + |
| 3 | +Starting from v2.0.0, we add the plugin to record network output. |
| 4 | +This feature aims to provide developers with more information about the bug scene. There are some options for recording and replaying network output. |
| 5 | + |
| 6 | +### Enable recording network |
| 7 | + |
| 8 | +You can enable using default option like this: |
| 9 | + |
| 10 | +```js |
| 11 | +rrweb.record({ |
| 12 | + emit: function emit(event) { |
| 13 | + events.push(event); |
| 14 | + }, |
| 15 | + // to use default record option |
| 16 | + plugins: [rrweb.getRecordNetworkPlugin()], |
| 17 | +}); |
| 18 | +``` |
| 19 | + |
| 20 | +You can also customize the behavior of logger like this: |
| 21 | + |
| 22 | +```js |
| 23 | +rrweb.record({ |
| 24 | + emit: function emit(event) { |
| 25 | + fetch('https://api.my-server.com/events', { |
| 26 | + method: 'POST', |
| 27 | + headers: { |
| 28 | + 'Content-Type': 'application/json', |
| 29 | + }, |
| 30 | + body: JSON.stringify({ |
| 31 | + events: [event], |
| 32 | + }), |
| 33 | + }); |
| 34 | + }, |
| 35 | + // customized record options |
| 36 | + plugins: [ |
| 37 | + rrweb.getRecordConsolePlugin({ |
| 38 | + initiatorTypes: ['fetch', 'xmlhttprequest'], |
| 39 | + // block recording event for request to upload events to server |
| 40 | + ignoreRequestFn: ({ performanceEntry }) => { |
| 41 | + if (performanceEntry.name === 'https://api.my-server.com/events') { |
| 42 | + return true; |
| 43 | + } |
| 44 | + return false; |
| 45 | + }, |
| 46 | + recordHeaders: true, |
| 47 | + recordBody: true, |
| 48 | + recordInitialRequests: false, |
| 49 | + }), |
| 50 | + ], |
| 51 | +}); |
| 52 | +``` |
| 53 | + |
| 54 | +**alert**: If you are uploading events to a server, you should always use `ignoreRequestFn` to block recording events for these requests or else you will cause a nasty loop. |
| 55 | + |
| 56 | +All options are described below: |
| 57 | +| key | default | description | |
| 58 | +| ---------------- | --------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 59 | +| initiatorTypes | ['fetch','xmlhttprequest','img',...] | Default value contains names of all [initiator types](https://developer.mozilla.org/en-US/docs/Web/API/PerformanceResourceTiming/initiatorType). You can override it by setting the types you need. | |
| 60 | +| ignoreRequestFn | () => false | Block recording events for specific requests | |
| 61 | +| recordHeaders | false | Record the request & response headers for `fetch` and `xmlhttprequest` requests | |
| 62 | +| recordBody | false | Record the request & response bodies for `fetch` and `xmlhttprequest` requests | |
| 63 | +| recordInitialRequests | false | Record an event for all requests prior to rrweb.record() being called | |
| 64 | + |
| 65 | +## replay network |
| 66 | + |
| 67 | +It is up to you to decide how to best replay your network events using the `onNetworkData` callback. |
| 68 | + |
| 69 | +```js |
| 70 | +const replayer = new rrweb.Replayer(events, { |
| 71 | + plugins: [ |
| 72 | + rrweb.getReplayNetworkPlugin({ |
| 73 | + onNetworkData: ({ requests }) => { |
| 74 | + for (const request of requests) { |
| 75 | + const url = request.performanceEntry.name; |
| 76 | + const method = request.responseMethod; |
| 77 | + const status = request.responseStatus; |
| 78 | + console.log(`${method} ${url} ${status}`); |
| 79 | + } |
| 80 | + }, |
| 81 | + }), |
| 82 | + ], |
| 83 | +}); |
| 84 | +replayer.play(); |
| 85 | +``` |
| 86 | + |
| 87 | +Description of replay option is as follows: |
| 88 | + |
| 89 | +| key | default | description | |
| 90 | +| ------------- | --------- | ------------------------------------------------------------------------------------------ | |
| 91 | +| onNetworkData | undefined | You could use this interface to replay the network requests in a simulated browser console | |
| 92 | + |
| 93 | +## technical implementation |
| 94 | + |
| 95 | +This implementation records [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) and [`XMLHttpRequest`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) by patching their object & methods. We record document navigation using [`PerformanceNavigationTiming`](https://developer.mozilla.org/en-US/docs/Web/API/PerformanceNavigationTiming) and we use [`PerformanceResourceTiming`](https://developer.mozilla.org/en-US/docs/Web/API/PerformanceResourceTiming) for recording everything else (script, img, link etc.) via [`PerformanceObserver`](https://developer.mozilla.org/en-US/docs/Web/API/PerformanceObserver) API. |
| 96 | + |
| 97 | +For more information please see [[network-plugin] Feat: Capture network events #1105](https://github.com/rrweb-io/rrweb/pull/1105) PR. |
0 commit comments