Why does a browser send two requests for the same

2019-02-19 01:43发布

I've created a simple Node.js app which logs to console the current request.url for every incoming HTTP request. When I refresh the page in Chrome on Mac OS X ML, I receive two requests for the same page. Why?

In comparison, when I use curl and request the same page, I receive only one request. Same one request if I refresh the page in Safari.

Why would Chrome send two requests for the same page?

Edit: I've looked into the request.headers and found one difference: the accept header.

Here is the header for the first request. Notice, it accepts a list of specific mimes.

{ host: 'www.pulsr.local:1337',
  connection: 'keep-alive',
  'cache-control': 'max-age=0',
  accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17',
  'accept-encoding': 'gzip,deflate,sdch',
  'accept-language': 'en-US,en;q=0.8',
  'accept-charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
  cookie: 'PULSRSESSID=BBtDAWMVgbQZ8lXA6wv4Wg/vwwI=; PULSRSESSID=dsGxP494UxJueit2/u79AFiM5fw=' }

Here is the second request. This time it accepts everything.

{ host: 'www.pulsr.local:1337',
  connection: 'keep-alive',
  'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17',
  accept: '*/*',
  'accept-encoding': 'gzip,deflate,sdch',
  'accept-language': 'en-US,en;q=0.8',
  'accept-charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
  cookie: 'PULSRSESSID=VGWRSG9zIokHjA2vLa1b+/fUqu8=; PULSRSESSID=dsGxP494UxJueit2/u79AFiM5fw=' }

Edit 2: I've checked and see that both requests are GET requests.

2条回答
▲ chillily
2楼-- · 2019-02-19 01:52

I think it's to do with this bug in Chrome where it's sending a request for the favicon on every request. Firefox and other browsers do this on their first request but cache it.

查看更多
\"骚年 ilove
3楼-- · 2019-02-19 02:12

As found in another answer, it can be caused by empty src attributes on images or iframes. I had the same problem : 4 requests on the exact same page — not on the favicon — from Chromium, when doing only 1 page refresh. In my case, I found out it was due to empty href attributes on link tags :

<link rel="shortcut icon" href="" />
<link rel="icon" type="image/x-icon" href="" />
<link rel="icon" type="image/png" href="" />

So 1 request for my page reload, and 1 request for each of the 3 above links.

查看更多
登录 后发表回答