Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent caching of .css files in UIWebView loaded from disk

In the iPad project I'm working on I've got a UIWebView inside of the app which displays a .html file which links to a .css file, i.e.

<link rel="stylesheet" href="style.css" type="text/css">

These files are stored locally on the iPad but are fetched from a remote server. I've noticed that UIWebView caches the .css file more or less indefinitely, and refuses to load the new file whenever I change it. I've once changed the name of the file just to get it to reset, but that's unacceptable in the long run.

Is there a way to prevent caching of the CSS file in a UIWebView? Or even better, is there a way to say when to cache and when not to?

like image 204
Kalle Avatar asked Aug 23 '10 16:08

Kalle


People also ask

Can we prevent CSS caching?

To disable CSS cache behavior for a given stylesheet means that every user's browser must load that file from scratch, on every page load. This removes the speed benefits of browser caching, but it makes sure that every user is seeing the most updated version of the stylesheet on each and every page load.

Do CSS files get cached?

When the browser parses this HTML, it identifies that a CSS resource needs to load from https://www.example.com/app.css. The browser issues a request to the server for this file, the server returns the file and also tells the browser to cache it for 30 days.

How do I clear cache in iOS WebView?

How do I clear cache in iOS WebView? Open Settings. Swipe down and tap Safari. Swipe down again and tap Clear History and Website Data, tap it once again to confirm.


1 Answers

Yes.

Change your line to this:

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

Every time that you update the stylesheet, change the version. The browser will think that it is a different page because of the query string, and your server will ignore it.

If you are using a server side language such as PHP, you can also do the following:

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

This will change the version every time you refresh, thus stopping all caching.

like image 119
riwalk Avatar answered Oct 01 '22 18:10

riwalk