Automatic browser cache removal
Browser cache can cause a lot of trouble when we build a website and make changes on it. Browser refreshing doesn’t always help. Awareness on the subject of the browser cache is relatively low among internet users. Many people thinks that cache and cookies are the same thing. But they are not. When I was looking for a photo for this article, I used a phrase „browser cache” on a website with stock photos. In the search results there were mostly photos presenting browser cookies 馃檪
Distinction between the browser cache and cookies
Cookies are small files that store text information. They are mostly used for your browser to remember if you are logged in on some websites that require logging in. Then can also store website settings or be used for marketing purposes.聽You can learn more about cookies on Wikipedia.
Browser cache聽stores聽the content of websites and their scripts聽– images, css files, javascript files. When we visit a website for the first time, the browser saves it’s content to cache system. When we visit the same website for the second time, browser uses it’s local cache resources so the website can load faster. In simple words – website content is stored on our device (computer, smartphone, tablet).
Cache is very useful, particularly when we have poor internet connection. But it is very distrubing when we make changes on the website and ask somebody to verify them. The person who will do it will probably see not the changed version, but the version of the website stored in he’s or she’s browser cache. In such situation we can try refresh browser. When it doesn’t help we need to remove/clear cache. It can be done in the browser settings but there are more convenient ways.
Cache from client side
Warto skorzysta膰 z wtyczek do przegl膮darek, kt贸re automatycznie usuwaj膮 nam pami臋膰 podr臋czn膮. Do przegl膮darki Google Chrome najbardziej polecam t臋 wtyczk臋:
https://chrome.google.com/webstore/detail/cache-killer/jpfbieopdmepaolggioebjmedmclkbap
Wtyczka Cache Killer automatycznie usuwa nam pami臋膰 podr臋czn膮 przy ka偶dym od艣wie偶eniu. Mo偶na j膮 w ka偶dej chwili w艂膮czy膰 i wy艂膮czy膰 jednym klikni臋ciem. Gdy j膮 w艂膮czymy, przy wczytywaniu ka偶dej strony internetowej usuwana jest pami臋膰 podr臋czna. Przy od艣wie偶aniu r贸wnie偶 (gdy偶 od艣wie偶anie to nic innego jak ponowne wczytywanie).
Analogiczn膮 wtyczk膮, o takim samym dzia艂aniu dla przegl膮darki Mozilla Firefox jest Cache Disabler:
https://addons.mozilla.org/pl/firefox/addon/cache-disabler/
Tw贸rcy tej wtyczki napisali nawet w opisie, 偶e wzorowali si臋 na Cache Killer z Google Chrome.
Managing cache for webdevelopers
Webdeveloper te偶 ma mo偶liwo艣膰 wp艂ywania na to, jak zachowuje si臋 przegl膮darka klienta (czy zapisuje zawarto艣膰 strony w pami臋ci podr臋cznej, czy wczytuje za ka偶dym razem od nowa stron臋).
S膮 dwie najbardziej znane metody. Jedna to ustawienie odpowiednich parametr贸w w sekcji head strony internetowej. S膮 to nast臋puj膮ce znaczniki meta:
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
Jest to rozwi膮zanie 艂atwe w zaimplementowaniu, jednak nie jest w 100% skuteczne.
Druga opcja to dodawanie losowego ci膮gu znak贸w do zasob贸w strony, czyli arkuszy styl贸w, skrypt贸w i obrazk贸w. Np. plik script.js mo偶na pobiera膰 w ten spos贸b:
<script type="text/javascript" src="main.js?v=12392823"></script>
Obrazek za艣:
<img src="img/obrazek.jpg?9382938293" alt="Alt obrazka" />
Oczywi艣cie cz臋艣膰 „12392823” oraz „9382938293” musi by膰 losowym ci膮giem znak贸w, nie mo偶e by膰 sta艂a.
Jest to rozwi膮zanie trudniejsze do zaimplementowania, nie zawsze mo偶liwe (w zale偶no艣ci od rodzaju strony, bud偶etu na ni膮). Je艣li tylko mamy mo偶liwo艣膰 stosowania go – warto to zrobi膰.
Niestety r贸wnie偶 nie jest ono w 100% skuteczne. Widzia艂em strony, w kt贸rych oba rozwi膮zania naraz by艂y zaimplementowane, a klient i tak mia艂 problem z wczytaniem nowej zawarto艣ci strony. Tak wi臋c o ile warto stosowa膰 te metody, bo w wi臋kszo艣ci przypadk贸w s膮 skuteczne, o tyle warto informowa膰 osoby weryfikuj膮ce stron臋 internetow膮 o istnieniu wtyczek do przegl膮darek opisanych powy偶ej.
