Michael
Michael

Reputation: 42050

Why is onResourceReceived invoked twice by PhantomJS?

Suppose I've got a phantomJS script like this:

var page = require('webpage').create()

page.onResourceRequested = function (req) {
    console.log('requested: ' + req.url);
};

page.onResourceReceived = function (res) {
   console.log('received: ' + res.url);
};

var url = 'http://localhost:3000/hello.html'

page.open(url, function (status) {
  console.log(page.content);
  phantom.exit();
});

When I run the script requested appears in the log just once but received appears twice. Could you explain why received appears twice ?

Upvotes: 0

Views: 233

Answers (1)

Artjom B.
Artjom B.

Reputation: 61892

Resources can be potentially very large. If they are, you will notice a time difference between the "same" two events. That's because the first is the start of the resource reception and the next is the end. You can check for the stage by checking response.stage.

Reference: page.onResourceReceived

Upvotes: 2

Related Questions