Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stylesheet not updating when I refresh my site

I am creating a website, but when I made changes to the stylesheet on my site, and I refreshed the site, none of the changes were there.

I tried to use the view source tool to check the stylesheet.css and it isn’t updated either. But when I go to the root of my system it is.

I have to wait at least 20 minutes before I see the update on my site, can anyone tell me why I don’t see changes right away? Is something wrong with my browser, computer, or server?

I also tried deleting my cookies, cache, and history but it still didn’t work.

like image 234
Mihad Aiko Avatar asked Oct 03 '12 22:10

Mihad Aiko


People also ask

Why is my stylesheet not updating?

Most probably the file is just being cached by the server. You could either disable cache (but remember to enable it when the site goes live), or modify href of your link tag, so the server will not load it from cache.

How do you force a stylesheet to refresh?

In Chrome, you can do a hard reload by opening the console (F12 key) and then right-clicking on the reload button to get a menu with additional reload options.

Why is my style sheet not working?

Make sure you're linking to your stylesheet using a link tag in the head of the HTML document. <style type="text/css" src="path/to/style. css"> : because it's a similar syntax to the <script> tag, which would make sense, but is invalid. <link rel="stylesheet" src="path/to/style.


3 Answers

If your site is not live yet, and you just want to update the stylesheet at your pleased intervals, then use this: Ctrl + F5.

On Mac OS (in Chrome) use: Cmd + Shift + R.

This will force your browser to reload and refresh all the resources related to the website's page.

So every time you change something in your stylesheet and you wanna view the new results, use this.

like image 188
Sean Vaughn Avatar answered Oct 22 '22 08:10

Sean Vaughn


Most probably the file is just being cached by the server. You could either disable cache (but remember to enable it when the site goes live), or modify href of your link tag, so the server will not load it from cache.

If your page is created dynamically by some language like php, you could add some variable at the end of the href value, like:

<link rel="stylesheet" type="text/css" href="css/yourStyles.css?<?php echo time(); ?>" />

That will add the current timestamp on the end of a file path, so it will always be unique and never loaded from cache.

If your page is static, you have to manage those variables yourself, so use something like:

<link rel="stylesheet" type="text/css" href="css/yourStyles.css?version=1" />

after doing some changes in the file content, change version=1 to version=2 and so on.

If you wish to disable the cache from caching css files, refer to your server type documentation (it's done differently on apache, IIS, nginx etc.) or ask/search for a question on https://serverfault.com/

Assuming IIS - adding the key under <system.webServer> with the right settings in the root or the relevant folder does the trick.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <caching enabled="false" enableKernelCache="false" /> <!-- This one -->
    </system.webServer>
</configuration>

That said sometimes one still has to recycle the Application Pool to "bump" the CSS. Therefore: Disabling IIS caching alone is not a 100% guaranteed solution.

For the browser: There are some notes on fine-grain controlling the local cache on FF over on SuperUser for the interested.

like image 20
Bartosz Górski Avatar answered Oct 22 '22 07:10

Bartosz Górski


Easiest way to see if the file is being cached is to append a query string to the <link /> element so that the browser will re-load it.

To do this you can change your stylesheet reference to something like

<link rel="stylesheet" type="text/css" href="/css/stylesheet.css?v=1" />

Note the v=1 part. You can update this each time you make a new version to see if it is indeed being cached.

like image 43
Marko Avatar answered Oct 22 '22 08:10

Marko