对fetch response的处理会影响chrome dev tools里网络抓包?

287 阅读1分钟

你可曾想到你写的代码会影响到chrome dev tools里的抓包数据预览功能?

我一直认为chrome dev tools的网络抓包功能是独立于网页存在的,类似fiddler或者Charlie,你是无法用代码影响它的工作的。

直到今天...

//  服务端
const Koa = require('koa');
const app = new Koa();

app.use(async ctx => {
  ctx.body = '{"a": 1}';
});

app.listen(3002);

// 前端
fetch("http://localhost:3002/").then(res => res.blob());

结果: image.png

前端稍作修改:

fetch("http://localhost:3002/").then(res => res.text());

image.png

dev tools里的数据预览是根据你对response的处理方式而展示的。