-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[network-plugin] Feat: Capture network events #1105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jlalmes
wants to merge
43
commits into
rrweb-io:master
Choose a base branch
from
jlalmes:feat/network-plugin
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+643
−19
Open
Changes from 42 commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
7c5f909
record xhr
jlalmes ada77af
use performance
jlalmes 2b23db5
emit less events & isInitial flag
jlalmes dafbdd4
stub xhr & fetch observers
jlalmes 6e9377a
stub req headers & body
jlalmes 27f4360
small type refactor
jlalmes 7fa75b5
add NavigationTiming
jlalmes 52c58b3
rename props
jlalmes 05a45df
implement fetch
jlalmes 863717d
use stringify
jlalmes e693366
refactor options
jlalmes 78b338f
add requestMethod
jlalmes d656ac7
rename replay prop
jlalmes 1d77b6c
add ignoreRequestFn
jlalmes f3d85e2
rn initiatorTypes
jlalmes a503ca4
fix build and rename prop
jlalmes ca21bab
bug fix
jlalmes 70395af
move win.perf check up
jlalmes 0e08bc7
better last perf entry
jlalmes 9d33b47
implement xhr
jlalmes a174964
better xhr events implementation
jlalmes 7f9ba0d
skip empty request callbacks
jlalmes ad966bc
by url
jlalmes 0ae1f06
linting
jlalmes 147aba6
fix xhr
jlalmes ec4010f
better getRequestPerformanceEntry
jlalmes c5a1d14
dont filter xhr & fetch from initial
jlalmes 6566294
fix xhr
jlalmes dcf3721
add responseStatus
jlalmes a131e37
docs
jlalmes dba49bf
formatting
jlalmes 15ecfcf
slim NetworkRequest
jlalmes fc5b77a
update docs
jlalmes a0c721b
startTime & endTime
jlalmes d7db138
fix: warning from rollup
jlalmes 8fdec47
moved findLast util
jlalmes 0f30b29
any body
jlalmes 8c41efd
better patch
jlalmes cae71f7
content-type body filtering
jlalmes d860433
update docs
jlalmes 0278113
better body typing
jlalmes 6804e4b
better recordBody
jlalmes 8863576
Update docs/recipes/network.md
jlalmes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# network recorder and replayer | ||
|
||
Starting from v2.0.0, we add the plugin to record network output. | ||
This feature aims to provide developers with more information about the bug scene. There are some options for recording and replaying network output. | ||
|
||
### Enable recording network | ||
|
||
You can enable using default option like this: | ||
|
||
```js | ||
rrweb.record({ | ||
emit: function emit(event) { | ||
events.push(event); | ||
}, | ||
// to use default record option | ||
plugins: [rrweb.getRecordNetworkPlugin()], | ||
}); | ||
``` | ||
|
||
You can also customize the behavior of logger like this: | ||
|
||
```js | ||
rrweb.record({ | ||
emit: function emit(event) { | ||
fetch('https://api.my-server.com/events', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
events: [event], | ||
}), | ||
}); | ||
}, | ||
// customized record options | ||
plugins: [ | ||
rrweb.getRecordConsolePlugin({ | ||
initiatorTypes: ['fetch', 'xmlhttprequest'], | ||
// block recording event for request to upload events to server | ||
ignoreRequestFn: (request) => { | ||
if (request.url === 'https://api.my-server.com/events') { | ||
return true; | ||
} | ||
return false; | ||
}, | ||
recordHeaders: true, | ||
recordBody: true, | ||
recordInitialRequests: false, | ||
}), | ||
], | ||
}); | ||
``` | ||
|
||
**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. | ||
|
||
All options are described below: | ||
| key | default | description | | ||
| ---------------- | --------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| 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. | | ||
| ignoreRequestFn | `() => false` | Block recording events for specific requests | | ||
| recordHeaders | `false` | Record the request & response headers for `fetch` and `xmlhttprequest` requests | | ||
| recordBody | `false` | Record the request & response bodies for `fetch` and `xmlhttprequest` requests | | ||
| recordInitialRequests | `false` | Record an event for all requests prior to rrweb.record() being called | | ||
|
||
## replay network | ||
|
||
It is up to you to decide how to best replay your network events using the `onNetworkData` callback. | ||
|
||
```js | ||
const replayer = new rrweb.Replayer(events, { | ||
plugins: [ | ||
rrweb.getReplayNetworkPlugin({ | ||
onNetworkData: ({ requests }) => { | ||
for (const request of requests) { | ||
const url = request.url; | ||
const method = request.method; | ||
const status = request.status; | ||
console.log(`${method} ${url} ${status}`); | ||
} | ||
}, | ||
}), | ||
], | ||
}); | ||
replayer.play(); | ||
``` | ||
|
||
Description of replay option is as follows: | ||
|
||
| key | default | description | | ||
| ------------- | ----------- | ------------------------------------------------------------------------------------------ | | ||
| onNetworkData | `undefined` | You could use this interface to replay the network requests in a simulated browser console | | ||
|
||
## technical implementation | ||
|
||
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. | ||
|
||
For more information please see [[network-plugin] Feat: Capture network events #1105](https://github.com/rrweb-io/rrweb/pull/1105) PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.