Puppeteer 模拟响应具有正确的内容长度,但 response.text() 返回空字符串

时间:2021-02-05 04:06:00

标签: puppeteer

我正在使用 Puppeteer 来模拟对网页的响应,我的逻辑是这样的:

console.log("mocking 200 ok", url, mime.lookup(url), body.length);
        return interceptedRequest.respond({
          status: 200,
          contentType: mime.lookup(url),
          body: body,
        });

生成日志(例如):

mocking 200 ok file:///_next/static/chunks/ef3be5f1bd18cb86d631d26cb7cea10532457998.f75c3c73063d661486a5.js application/javascript 49597

但是,当我得到响应时,即使标题中的内容长度正确,文本也会返回为空。

var txt = interceptedResponse.text();
        txt.then(function(t) {
          console.log(
            "response url",
            interceptedResponse.url(),
            "response status",
            interceptedResponse.status(),
            "headers",
            interceptedResponse.headers(),
            "response text",
            typeof t === "string" ? t.length : false
          );

导致

response url file:///_next/static/chunks/ef3be5f1bd18cb86d631d26cb7cea10532457998.f75c3c73063d661486a5.js response status 200 headers { 'content-length': '49597', 'content-type': 'application/javascript' } response text 0

我在嘲笑中做错了什么会导致这种结果?当我截取屏幕截图时,这些资源似乎也无法加载到页面上。这可能是超时错误还是某种竞争条件?谢谢!

0 个答案:

没有答案