ChatGPT解决这个技术问题 Extra ChatGPT

How do I force a favicon refresh?

I have a Grails application running locally using its own tomcat and I have just changed the favicon for a new one. Problem is that I can not see it in any browser. The old favicon shows up or I get no favicon at all, but not my new one. I do not think this is a Grails issue per se, more an issue with favicons.

What is supposed to happen with favicons? How are they supposed to work? I have numerous bookmarks in my browser which have the wrong icons and they never seem to get refreshed. How do I force the server/browser to stop caching them? It seems pretty silly to always cache them given they are normally only 16x16. Why not just upload them with every visit to the page? It is not exactly a huge overhead.

The accepted and highly upvoted solution is NOT the real solution. The actual reason why refresh won't work can be found in this question's solution: stackoverflow.com/questions/8616016/… --- the icon cache is in a sqlite DB file, independent of the browser cache!
The accepted and highly upvoted solution is solution for production, or do shall I write tutorial for users how to clear their favicon cache when ever we update favicon for production?
Is not a general solution. What if you are using favicon default location without touching your code? That is, a favicon.ico file in your document root.
If anyone's wondering: Chromium have decided not to fix this one, and instead require that related history entries and bookmarks are removed: bugs.chromium.org/p/chromium/issues/detail?id=440322

M
Mahozad

To refresh your site's favicon you can force browsers to download a new version using the link tag and a query string on your filename. This is especially helpful in production environments to make sure your users get the update.

<link rel="icon" href="http://www.yoursite.com/favicon.ico?v=2" />

This did the trick for me. I had to remove type="image/x-icon" from my code, contrary to many sources. I used a file modified timestamp for the v?=2 part of the query.
I have found that in some browsers the favicon did no tupdate no matter what unless I deleted the history. Then it will save the new favicon.
Unfortunately you need the fully-qualified absolute URL for IE7 (&IE8?) - see jeffcode.blogspot.com.au/2007/12/…
agree with the above comments, but this works for me in all current browsers:
It makes sense to cache favicons because they sometimes push 2-3 kilobytes in size. You don't want to keep transferring all that data. (yes, sarcasm)
K
KyleMit

Adapted from lineofbird's answer beloew, you can do the following:

Go directly to the favicon url in the address bar by typing in it's address e.g. www.yoursite.com/favicon.ico www.yoursite.com/apple-touch-icon.png etc. Navigate to the url by pressing Enter Refresh with Ctrl+F5 Restart your browser (e.g. Chrome, Firefox)


Using this method you probably would overcome the caching, but your existing users cannot. Hence, I find the other answer more valuable.
Worth noting that this doesn't work in Firefox 33. First answer is a better choice.
I had to close dev tools to make this work in Chrome.
I didn't have to restart the browser, but I did have to navigate to my website in a tab without devtools open. For some reason, Ctrl+F5'ing in a non-devtools tab did it. I could Ctrl+F5 all I wanted and navigate all over the internet and back to my site as much as I wanted in the devtools tab and it wouldn't ever update the favicon.
The "restart browser" part was the only thing that actually made it work for me. All the other fixes work in theory, but don't actually change the favicon pixels displayed on the browser tab. +1 this answer
G
Garry Polley

This answer has not been given yet so I thought I'd post it. I looked all around the web, and didn't find a good answer for testing favicons in local development.

In current version of chrome (on OSX) if you do the following you will get an instant favicon refresh:

Hover over tab Right Click Select reload Your favicon should now be refreshed

This is the easiest way I've found to refresh the favicon locally.


Unfortunately this does not work in IE or even Chrome on Windows so it seems that you should go with the accepted answer unless you are only using Chrome on OSX.
Works in OS X Sierra with Chrome 53.0.2785.143 (64-bit)
works for me: Chrome 58.0.3029.110, Windows 10, localhost
Worked on Chrome 63.0.3239.84 for Windows 10!
Works in Chrome 67.0 on OSX. Strange it's not working with CMD+Shift+R
M
MultiplyByZer0

By destroying the file your browser uses to store old favicons, you can force new ones to be loaded.

Close your browser. Make sure there are no longer browser processes running (e.g. check Task Manager for chrome.exe or firefox.exe). Navigate to where your browser stores user files: For Chrome, go to the Chrome data directory. For Firefox, go to the Firefox profile folder. Delete the favicon cache. For Chrome, remove Favicons and Favicons-journal For Firefox, remove favicons.sqlite

This will almost definitely work. If not:

Possibility 1: An update to your browser has changed how the favicon cache works. Please edit this answer with new instructions.

Possibility 2: Your favicon problem has nothing to do with overaggressive caching. It may instead be a resource-loading problem – using Developer Tools, make sure the new favicon is downloading properly.


This did the trick. For me, this was a Google Chrome application favicon issue. Not a DOM favicon issue.
This is a good answer that meets my needs for testing. There is no one real answer for this question because it depends on who needs to refresh it and what they are willing and able to reasonably do.
Just a little improvement: that same path for any username is %appData%/../Local/Google/Chrome/User Data/Default
An easier solution in Chrome is to right click and select Inspect (or CTRL+SHIFT+I), then click and hold the refresh button. A menu will appear with the option to Refresh, Hard Reload, or Empty Cache and Hard Reload. Usually Hard Reload will do it, but if it's stubborn for some reason, Empty Cache and Hard Reload definitely will.
On Linux you can find these files at: ~/.config/google-chrome/Default
K
Klaus Byskov Pedersen

Rename the favicon file and add an html header with the new name, such as:

<link rel="SHORTCUT ICON" href="http://www.yoursite.com/favicon2.ico" />

I would assume browser can see if favicon name is the same. If it is it won't request its download again and will use one already cached, but if you change the name it will assume it has changed thus requesting it from the server.
I had the problem with the IE8. It didn't use the favicon.ico placed in a subfolder. Adding the rel-attribute 'shortcut' fixed it.
Thats not refreshing at all, its changing it to a new one completely.
please note that you have to rename your actual favicon.ico as well (it will be in your build folder or the directory from which the webapp is being served)
F
Felix Wienberg

If you use PHP you could also use the MD5-Hash of the favicon as a query-string:

<link rel="shortcut icon" href="favicon.ico?v=<?php echo md5_file('favicon.ico') ?>" />

This way the Favicon will always refresh when it has been changed.

As pointed out in the comments you can also use the last modified date instead of the MD5-Hash to achieve the same thing and save a bit on server performance:

<link rel="shortcut icon" href="favicon.ico?v=<?php echo filemtime('favicon.ico') ?>" />

Nice, this avoids the need to change the href everytime i update the favicon, this answer should have more votes !
The problem with this answer is that you are calculating that hash on every request, and that's something unnecessary and might hurt the sites performance (if lots of users).
It's not that big of a deal in both sense... a favicon is only a couple of kilobytes... I mean, it won't be less stress on the server than having to server the favicon each time...
I still don't see any real reason to calculate the MD5. It's easier and just as effective to use the file's last modified date. I see no advantage of using the MD5 over the last modified stamp.
How often are you really updating your favicon? This seems like overkill. Just use the manual query string when you make a new icon.
M
Maxim Mazin

In Chrome on Mac OS X one can remove file with favicon cache

${user.home}/Library/Application Support/Google/Chrome/Default/Favicons 

This solution worked for me. For some reason, it was not refreshing the favicon with above mentioned methods with far more votes. So thank you, Maxim Mazin. I only wanted to get rid of an old favicon that I knew couldn't exist anymore on my new host.
This approach also worked for me on Windows, where I had removed the favicon from my app, but chrome was still showing it. Close Chrome, delete "C:\Users\{user}\AppData\Local\Google\Chrome\User Data\Default\Favicons", then restart Chome. The old unwanted favicon is gone.
I had nothing under "Default" but eventually I found this variation: ${user.home}/Library/Application Support/Google/Chrome/Profile 1/Favicons
@mdahlman This didn't initially work for me after trying all the other methods. However I got this one to work after cmd+qing chrome, going to the menu bar, force quitting chrome again and waiting 2 minutes to open the browser and navigate back to my site.
A
Anonymous

Depending on the browser they are handled differently, but typically I find that going to the default page of the site, and doing a hard refresh. CTRL + F5 (or ⌘ Command + SHIFT + F5 on macOS), will typically get it to update.


Mac user: CMD SHIFT 5
a
artlung

Well, overhead is overhead, but yes, not too big.

Also, browsers are sometimes "greedy" about cached files. You could clear cache and/or restart your browser and may see the change. If that fails though...

My cheapo solution is to:

Visit your file at http://example.com/favicon.ico in your browser. Delete the favicon.ico from your webroot. Visit http://example.com/favicon.ico again in a browser, verify it's missing. Upload new one to your webroot. Visit http://example.com/favicon.ico again in a browser, verify it's the new one.

If that sequence doesn't work, then something else is going on.


This worked for me, but sadly we can't ask every visitor to do this.
M
Mwiza

ON MAC: ⌘ + Shift-R or hold down Ctrl and click the reload button in the browser.


This doesn't force reload of favicons.
This did reload my favicon / fix the problem
Works for ubuntu 16.04
n
nickhar

For Internet Explorer, there is another solution:

Open internet explorer. Click menu > tools > internet options. Click general > temporary internet files > "settings" button. Click "view files" button. Find your old favicon.ico file and delete it. Restart browser(internet explorer).


N
Nathan Hughes

More than likely a web browser issue. You will have to delete your cache from your browser, close your browser and reopen it. That should fix it.

I don't believe your favicons will get refreshed on your favorites until you revisit that page, and assuming that you had previously cleared your browsers cache.

Your web browser will not go out to the internet to check for a new favicon on its own... thank goodness.


"Your web browser will not go out to the internet to check for a new favicon on it's own...thank goodness." Why not? Why not refresh it eveyr time a page loads? It's a 2k image, which is peanuts. It should never be out of date. The whole caching idea is just silly in the grand scheme of things.
developer.yahoo.com/performance/rules.html mentions issues related to downloading favicon.ico that you may not have thought of. Good tips there all around. Caching is a valuable tool for making sure your site runs as fast as is practical. Caching is also useful for making your browser feel as fast as possible.
@Simon, caching is silly?!? I hope for your clients sakes that you've learned the importance of better web development since 2010. What's "silly" is NOT caching and making unnecessary requests to your server for what amounts to be a static file that rarely, if ever, changes.
O
Old McStopher

Try Opening In a New Tab

I tried many of the things above (resetting cache, refreshing, using the link tag, etc), I even checked my .htaccess file and reset the ExpiresByType variable.

But this is what finally worked for me in both Chrome (25.0.x) and Safari (6.0.1):

Flushing cache Hard-linking the favicon with the tag Navigating to mysite.com/favicon.ico Opening mysite.com in a new tab

(Up until step 3, refreshing in the same tab kept reproducing the old icon.)


M
MrUpsidown

For Chrome on macOS, if you don't want to delete the entire Chrome favicon database as suggested already here, you can delete only the conflicting icons:

Quit Chrome Load the favicons database (using sqlite here):

sqlite3 ~/Library/Application\ Support/Google/Chrome/Default/Favicons

Search for the file that is causing issues

select * from favicons where url = 'http://mysite.dev/favicon.ico';

If you are happy with the output:

delete from favicons where url = 'http://mysite.dev/favicon.ico';

Alternatively, you can search for a pattern that you can reuse to delete multiple entries:

Search for multiple files that are causing issues

select * from favicons where url like 'http://mysite.dev%';

And again if you are happy with what this returns:

delete from favicons where url like 'http://mysite.dev%';

Type .exit and hit return to quit sqlite Restart Chrome


D
Dean J

When you request the favicon from Google, you can take a look at the response headers.

Last-Modified: Fri, 09 Jan 2009 16:35:02 GMT
Date: Thu, 01 Dec 2010 00:00:01 GMT
Expires: Fri, 01 Dec 2011 00:00:01 GMT
Cache-Control: public, max-age=31536000
Age: 7

If you put an "Expires: " header on the response, client browsers will re-request the icon after that timestamp. While doing active development, you could set the expires timestamp to a second or two in the future, and always have it fetch this, although that's a poor longterm plan.


It also only works pre-emptively, which beats the purpose of the question.
S
Stuart P. Bentley

Chrome's favicon support is buggy - disregard this answer

I wrote this answer under the impression that this is what it took to refresh favicons in Google Chrome. However, it turns out that this only works for the first five minutes or so, until the icon gets irretrievably lost in Chrome's history synchronization.

Original answer

You don't have to clear your cache, restart your browser, or rewrite your HTML - you just need to change the icon's URL, once, so that the browser will forget the previously-cached icon.

Assuming that you've defined your icon via <link> elements in your page's <head>, you can do that by running this standard-JS one-liner in the console:

[].slice.call(document.querySelectorAll('head>link[rel$="icon"]')).map(function(ln){ln.href+='?v=2'});

For a more advanced implementation of this that can automatically do this for end users in production, see freshicon.js.


This is actually useful if you're trying to get rid of an cached icon on a server you don't control, where the icon is now missing/returning a non-200 http code. Rather than appending ?v=2 swap out the whole URL for an icon of your choice and it seems to stick around after a refresh
P
Pascal

Chrome Version: 68.0.3440.106

Just restart Chrome (in your address bar): chrome://restart


J
John Conde

I recently restored my bookmarks and was looking for a way to restore the FavIcons without visiting each page. My search brought me to this thread.

For those in a similar circumstance merely download the FAVICON RELOADER addon. Once installed you will find the "reload favorite icons" command in your BOOKMARKS dropdown menu.

https://addons.mozilla.org/en-US/firefox/addon/faviconreloader/?src=api


It is back there for installation and really lookes promising (you'll find the AddOn within the bookmarks menu) but it also didn't reload the icon on my firefox 34 (on linux) without restart
S
Shakeel Ahmed

If you are using PHP .. then you can also use this line.

<link rel="shortcut icon" href="http://www.yoursite.com/favicon.ico?v=<?php echo time() ?>" />

It will refresh your favicon on each page load.


That is the problem "It will refresh your favicon on each page load". How about caching? The favicon should be cached.
If you want to cache your new favicon.. just provide any new value like.. <link rel="shortcut icon" href="yoursite.com/favicon.ico?v=12345">
favicon.ico?v=
This is not a good way of solving this problem. Using time() will force it to reload the icon every single time. You want the last modified time of the favicon.ico file not the current wall-clock time.
what if you round to the nearest 24 hours, or nearest 30 days? a monthly cache reset could be useful
c
cpjolicoeur

If you are just interested in debugging it to make sure it has changed, you can just add a dummy entry to your /etc/hosts file and hit the new URL. That favicon wouldnt be cached already and you can make sure you new one is working.

Short of changing the name of the favicon, there is no way you can force your users to get a new copy


u
user1427015

This is a workaround for the chrome bug: change the rel attribute to stylesheet! Keep the original link though. Works like a charm:

I came up with this workaround because we also have a requirement to be able to update customer's sites / production code and I didn't find any of the other solutions to work.


I tried literally every suggestion (coding) solution on here & this is the only one that worked for me. Do you have any idea why?
Really "like a charm". And the simpliest way.
N
Nikolai Koudelia

This works for Chrome:

on Mac: delete file ${user.home}/Library/Application Support/Google/Chrome/Default/Favicons

on Windows: delete files C:\Users[username]\AppData\Local\Google\Chrome\User Data\Default\Favicons C:\Users[username]\AppData\Local\Google\Chrome\User Data\Default\Favicons-journal

source


r
rockmo

I know this is a really old question but it is also one that is still relevant, though these days applies only to mozilla. (I have no idea what explorer does coz we don't code for non standard browsers).

Chrome is well behaved if one includes an icon tag in the header.

Although mozilla are well aware of the issue, as usual, they never fix the annoying small stuff. Even 6 years later.

So, there are two ways of forcing an icon refresh with firefox.

Have all your clients uninstall firefox. Then re-install. Manually type just the domain in the url bar - do not use http or www just the domain (mydomain.com).

This assumes of course that your ns records include resolution for the domain name only.


M
Mwiza

Simple,

1: I don't want to fiddle around with codes (ps my site builder doesn't use codes, it uses "upload file" button and it does it itself)

2: I tried the CTRL+F5 and it doesn't work for me so....

I HAVE A SOLUTION:

IE: Clear All browser history and cookies by going to the settings cog O

Chrome: Go to the menu in the top right corner below the X that looks like a = , then go to settings, history, CLEAR BROWSING DATA and check all of the boxes that apply (I did history, cookies and empty the catche from the beginning of time)


the question relates to how favicons are cached. Clearing your local cache is going to fix the problem for you but not for anyone else.
@MarkChorley which is exactly what the original question is about. Caching of favicons on a local development environment.
A
Armin Alibasic

Just change this filename='favicon1.ico'


l
lineofbirds

Here's how I managed it with a simply animated favicon and FireFox 3.6.13 (beta version) It will probably work for other versions of FireFox as well, let me know if it doesn't. It's basically artlung's solution, but addressing the .gif file as well:

I opened by FTP program, downloaded my favicon.ico AND favicon.gif files, then DELETED them from my server's files. Then I opened them in my browser as artlung suggested: http://mysite.com/favicon.ico AND http://mysite.com/favicon.gif Once those addresses loaded and displayed 404 error pages ("page not found") I THEN uploaded both files back onto my server, and PRESTO - the correct icons were instantly displayed.


Oh - BTW to get the new ones to display I also had to delete my saved bookmark - and this same procedure worked for Google Chrome as well.
s
ssc-hrep3

Also make sure you put the full image URL not just its relative path:

http://www.example.com/images/favicon.ico

And not:

images/favicon.ico

/images/favicon.ico is the proper way to do it: it "just works" for different environments (dev/test/production), and is an absolute path - from the document base URL.
Unfortunately you need the fully-qualified URL for IE7 (&IE8?) - see jeffcode.blogspot.com.au/2007/12/…
This isn't really an answer, this is a comment.
M
Murat Yıldız

If the problem continues despite of applying some steps above try to restart the IIS Server. Hope this helps...


d
dev

Use query string at the end of the file path. Query string variable value must be different with every build.

if previous build is:

<link rel="icon" href="http://example.com/favicon.ico?v=v1" />

OR

<link rel="icon" href="http://example.com/favicon.ico?v=stringA" />

then next build should be:

<link rel="icon" href="http://example.com/favicon.ico?v=v2" />

OR

<link rel="icon" href="http://example.com/favicon.ico?v=stringB" />


u
user569825

Simon, I suppose there's a reason none of the other answers is accepted so far. Thus I believe this could be a Grails issue nevertheless - Especially if you're using the 'Resources Plugin'.

If your plugins provide a favicon (which - illogically - many do), they might override the one you desired to use - given yours is in a plugin itself.

If deleting the favicon from all your plugins temporary resolves the issue then you're very likely experiencing this:

http://jira.grails.org/browse/GPRESOURCES-134