how to avoid or handle request for `favicon.ico` from the browser for a servlet?

by Aakash Verma   Last Updated October 20, 2019 05:26 AM

UPDATE: Even though another request is being sent for favicon.ico and function is getting executed again (two console.log(data) outputs), why isn't the response object being sent twice?

Also, is there a way to not send a request for favcion.ico as I wouldn't want my function to uselessly run twice?


I have created a very simple servlet to accept a GET (default) and return the contents to the browser after reading a file

var http = require("http");
var fs = require("fs");
var num=0;

http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
var data = '';
// Create a readable stream
var readerStream = fs.createReadStream('input.txt');

// Set the encoding to be utf8. 
readerStream.setEncoding('UTF8');

// Handle stream events --> data, end, and error
readerStream.on('data', function(chunk) {
   data += chunk;
});

readerStream.on('end',function(){
console.log(data); //To print same file content in the console
response.end(data);
});

readerStream.on('error', function(err){
   console.log(err.stack);
   data+="\n\n"+err.stack;
});


}).listen(8081);

// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');

But every time I run this servlet and make a request at http://localhost:/8081, I get the content of the file printed twice on the console but only once at the browser window (this is the expected behaviour).

What is the cause for the console behaviour?

Also, I modified the program by adding a counter to count how many times the function is run for every request (appended the value as data+num for both console and response) and it was found to be two but the response from the first execution of the function was the one received by the browser, not the second one(browser window printed data+1 for the first ever request).



Answers 3


Most browsers make a call to grab /favicon.ico for example.

Try to log the url and you can see what's being called.

console.log(req.url);
sForSujit
sForSujit
July 05, 2017 06:40 AM

As pointed out by @sForSujit, there are apparently two requests made for every page that the browser wants as a response - the other being a file called favicon.ico which is the icon file next to the title of your page displayed on a browser tab.

That leads to two requests also being caught by the server which again makes the file read operation and prints it out on the console the second time. The response object also made the second time is rendered useless at the client side as it is looking for your favicon.ico file as the response object.

How is it fetched?

The browser tries to obtain that icon by first requesting for "favicon.ico" from the directory of your web page. If it cannot find such a file, it will try to obtain it from the root directory of your website, failing which, it will simply use a default icon.

Chrome searches for a favicon.ico file in the root directory.

Firefox needs this on each page:

<link rel="icon" type="image/png" href="/favicon.png" />

Most, if not all, modern browsers also have the ability to use the favicon.ico file. In fact, many of them also accept the file in other graphics formats, such as the PNG format.

But, this again, needs to be set for every webpage of your website where you'd want your custom favicon to be displayed. To know how to take care of that, read the SO answer here


How to avoid the favico.ico request?

From vog's answer,

The following solution is very short, valid HTML5 and does not incur any quirks from IE 8 and older.

Just add the following line to the section of your HTML file:

<link rel="icon" href="data:,">
Aakash Verma
Aakash Verma
July 23, 2017 06:40 AM

I found this effective URL link to get rid of favicon errors.

<link rel="icon" href="data:;base64,iVBORw0KGgo=">

Hope you find it useful.

Raaj Kanchan
Raaj Kanchan
October 20, 2019 05:23 AM

Related Questions


Updated March 17, 2017 09:26 AM

Updated August 06, 2017 08:26 AM

Updated March 26, 2017 12:26 PM

Updated June 01, 2017 07:26 AM

Updated September 21, 2017 09:26 AM