zarathustra
zarathustra

Reputation: 2080

Browsers caching HTML

I am developing a blog with a static site generator. To get a better page speed I enabled image, javascript and css caching in my nginx config file:

location ~* \.(?:ico|gif|jpe?g|png)$ {
    expires 120d;
}

location ~* \.(?:css|js)$ {
    expires 7d;
}

My page's <head> area has no caching directive whatsoever.

Now when I am publishing a new article (that means I locally generate the files and scp them on the server) and visit my site - the new article is not showing up! It only shows up when I "hard refresh" the site.

This is definitely not the desired behaviour because people might come to the site and won't see the latest articles.

Upvotes: 0

Views: 104

Answers (2)

zarathustra
zarathustra

Reputation: 2080

Although the solutions from Kevin_Kinsey may work I found a solution that also did the job quite well:

I added this to my nginx config:

location ~* \.(?:manifest|appcache|html?|xml|json)$ {
    expires -1;
}

Upvotes: 0

Kevin_Kinsey
Kevin_Kinsey

Reputation: 2300

Add a unique query string to resources, a la:

<img alt='something' src='/images/foo.jpg?20170421'>

If you have a dynamic server-side scripting language/setup, you can often have these query strings generated "on the fly".

For the HTML itself, you'll need to configure the server to serve HTML with a No-Cache instruction. In your HTML:

<meta http-equiv="Cache-control" content="no-cache">

Upvotes: 1

Related Questions