Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I control caching for static assets when using Sinatra? [closed]

I'm using the latest Sinatra and I'm hosting on Heroku. Is there a way I can set the caching headers for my static assets served from the /public directory?

Sinatra serves files from the /public directory before checking any routes that have been defined, so I can’t just the cache_control method inside a route.

The /public directory contains the CSS and JavaScript of my app. I don’t want the browser to download those files every single time, since they won't change often.

like image 476
js-coder Avatar asked Oct 10 '12 18:10

js-coder


1 Answers

You can use the static_cache_control setting to set the Cache-Control header for static files served by Sinatra:

set :static_cache_control, [:public, max_age: 60 * 60 * 24 * 365]

Note you need to use an explicit array [...].

Also this will apply to all files in the public directory, i.e. you can’t specify different headers for css and javascript files.

(If you’re not using Heroku and are using Apache or Nginx to serve static files then this won’t work, in that case you’d need to configure your webserver separately).

like image 174
matt Avatar answered Nov 09 '22 02:11

matt