Search code examples
httppuppeteerhttpresponse

Invalid HTTP-response date using Puppeteer


I am currently trying to optimize the time to refresh a webpage and therefore analyzed the request/response times: I noticed that with this code sample the following issue occurs:

If I refresh the Website at a predefined time the first response the web-browser receives was created before triggering the refresh. So for example if I trigger the refresh at 8:00:00 the first response was created at 7:59:59 but it should be a time after the refresh.

To understand the issue a bit better I created this code sample:

const browser = await puppeteer.launch(
{
    headless: false,
    defaultViewport: {
        width: 1400,
        height: 2000,
        deviceScaleFactor: 1
    }
});
const page = await browser.newPage();
await page.goto('https://www.google.at/');

//wait two seconds to load page
await page.waitForTimeout(2000);

let firstResponse = true;

page.on('response', response => {
    if (firstResponse){

        const delay = Date.now() - starttime.getTime();

        const head = response.headers();
        console.log(head.date);

        console.log("first response happened:");
        console.log("delay between refresh and first response: " + delay + "ms");

        firstResponse = false;
    }
});

//predefining the time to refresh the page
const starttime = new Date(2021, 11, 27, 0, 7, 0, 0);
console.log(starttime);
const waitingtime = starttime.getTime() - Date.now();

await page.waitForTimeout(waitingtime);

console.log("reload starts");

await page.reload();

await browser.close();

This is the output on the console: (first line is the time when puppeteer refreshes the page and third line is the response date)

2021-12-26T23:07:00.000Z
reload starts
Sun, 26 Dec 2021 23:06:59 GMT
first response happened:
delay between refresh and first response: 151ms

So according to this example the first response should be created between 23:07:00:000 and 23:07:00:151 (hh:mm:ss:ms) an not before.

Am I overseeing something here? I just can't figure out why this happens!


Solution

  • I tested it on my end and I'm not getting the same results.

    enter image description here

    I'm pretty sure you're actually not resetting the response timestamp on reload and you're actually using the timestamp from the initial request which would explain why the response timestamp is outdated.

    The firstResponse variable isn't getting reseted properly. On first load we have firstResponse = false but it isn't reseted to true on reload.

    let puppeteer = require(`puppeteer`);
    (async () => {
        //start...
        console.log(`\u001b[1;32m` + `Start @: ${new Date(Date.now())}` + `\u001b[0m`);
        let browser = await puppeteer.launch({
            headless: false,
        });
        let page = await browser.newPage(); (await browser.pages())[0].close();
        //request...
        let req = 0;
        page.on(`request`, (page => {
            req++;
            if (req === 1)
                console.log(`\u001b[1;35m` + `Request @: ${new Date(Date.now())}` + `\u001b[0m`);
        }));
        //response...
        let res = 0;
        page.on(`response`, (page => {
            res++;
            if (res === 1)
                console.log(`\u001b[1;36m` + `Response @: ${new Date(Date.now())}` + `\u001b[0m`);
        }));
        await page.goto('https://www.google.at/');
        //reset...
        req = 0,
        res = 0;
        //reload...
        console.log(`\u001b[1;33m` + `Reload @: ${new Date(Date.now())}` + `\u001b[0m`);
        await page.reload();
        //close...
        console.log(`\u001b[1;31m` + `Close @: ${new Date(Date.now())}` + `\u001b[0m`);
        await browser.close();
    })();