Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've tried to make a simple API. If someone/something queries:

myIP:port/query

It should serve some result. However, I've noticed that (at least) when the query is made by a browser (Chrome), the server receives two requests. If the server simply is set up as follows:

http.createServer(function (request,result) {
    console.log(request.url);
    handleRequest(request,result);
}).listen(3000, '0.0.0.0');

It prints two request urls:

  • /query
  • /favicon.ico

I imagine that this means that the requesting client automatically draws an additional request hoping to load the favicon as well as the actual page.

  • Is this assumption correct?

  • Is there anything I can do when making the request to prevent this? Is it driven by chrome or would it also occur if I queried the page using ajax?

  • What is the best practice on the server side for filtering out the favicon request with minimal wasted effort by the server?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
265 views
Welcome To Ask or Share your Answers For Others

1 Answer

  1. Your assumption is correct. Most browsers, upon first visit to a website, will issue a GET /favicon.ico if there is no favicon specified as a <link rel="icon" ... /> in the head of the HTML body.
  2. No, this is a client (browser) side implemented feature. There is nothing you can do server-side to prevent this behavior. The browser will not make an additional request to /favicon.ico if the request is made via AJAX, but it is likely that the browser has already made that request by this time (you have to have loaded the page to perform an AJAX request).
  3. As far as I know there are no server-side "best practices" for reducing the number of these requests. However, you could modify the template (or HTML file) you are serving to the client by explicitly including a <link ref="icon" ... /> statement in the head of the document.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...