Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a browser to show the latest changes to a CSS file?

Tags:

css

php

caching

I made some changes on a CSS file on a PHP site.

The changes do not show up in any browser I test it on until I hold down the CTRL key and click the browser's REFRESH button.

This means that most users are not going to seem the changes I made.

How can I force browsers to show the changes to CSS files immediately (without renaming the CSS file)?

like image 803
Edward Tanguay Avatar asked Oct 17 '09 00:10

Edward Tanguay


People also ask

How do I force the browser to update a CSS file?

Here’s one way to do it: Notice that I’m pointing to my CSS using the commonly known <link> element. But I’ve also added what’s called a query string to the end of the file name. The browser will view a file name of style.css as different from a file name of style.css?v=1.1, so it will generally force the browser to update the CSS.

How do I refresh my browser to the latest version of CSS?

Browsers cache. Anytime you make changes to CSS, JavaScript and are viewing the page you've updated - you will see this concern. You can force a refresh by pressing CTRL+F5 on the browser and that will get the latest version.

How do I force a refresh when making changes to CSS?

Anytime you make changes to CSS, JavaScript and are viewing the page you've updated - you will see this concern. You can force a refresh by pressing CTRL+F5 on the browser and that will get the latest version.

How do I update the version of my CSS?

The browser will view a file name of style.css as different from a file name of style.css?v=1.1, so it will generally force the browser to update the CSS. So, each time you update your CSS on the server, you can incrementally update your version number.


2 Answers

Add a unique string as query string when linking the stylesheet. Here's an example

<link href="style.css?<?=filemtime("style.css")?>" rel="stylesheet" type="text/css" />
like image 193
Chris Bartow Avatar answered Oct 02 '22 10:10

Chris Bartow


It depends on your web server. Apache can be set to configure the maximum cache time for a given file, and I'm pretty sure IIS can do the same.

Alternatively, a common solution to FORCE uncaching is to version your CSS file names (Rails does this internally). For example, style.css becomes style-10162009.css

like image 23
Stefan Kendall Avatar answered Oct 02 '22 08:10

Stefan Kendall