BastiaanWW
BastiaanWW

Reputation: 1299

How to prevent caching of website content?

I have a dynamic web page with a small part of content that changes all the time.

The content that is different for each request is composed of: Javascript and HTML

In order for the website to show properly the javascript and HTML needs to be 100% fresh from the server. I checked that page errors, which occur from time to time are a result from old/previously loaded javascript or HTML instead of fresh data from the server.

I tied the following settings in .htacces:

Header Set Cache-Control "max-age=0, no-store"

The above setting is working, however the problem is that also images need to be reloaded each time, which is un necessary and is bad for the site performance.

I tried the following settings too in .htacces:

### turn on the Expires engine
ExpiresActive On

### expires after a month in the client's cache
ExpiresByType image/gif A36000
ExpiresByType image/png A36000
ExpiresByType image/jpg A36000
ExpiresByType image/x-icon A36000
ExpiresByType application/pdf A36000
### ExpiresByType application/x-javascript A36000
### ExpiresByType text/plain A36000

However the above does not seem to be working, as I checked the content is not always fresh, resulting into page errors.

My question:

How can I properly configure the server such that always (by the php script generated) fresh HTML and javascript is used?

Upvotes: 0

Views: 175

Answers (2)

Rasmus
Rasmus

Reputation: 96

What about text/html and css files different expire time than images?

ExpiresByType text/html "access plus 5 seconds"
ExpiresByType text/css "access plus 5 seconds"
ExpiresByType image/jpg "access plus 5 minutes"
etc

Upvotes: 1

user1299518
user1299518

Reputation:

i used the following (php) in a case when my app had the same update requirements:

// disable cache (ajax useful)
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

Upvotes: 0

Related Questions