Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are best practices for preventing stale CSS and JavaScript

I'm researching this for a project and I'm wondering what other people are doing to prevent stale CSS and JavaScript files from being served with each new release. I don't want to append a timestamp or something similar which may prevent caching on every request.

I'm working with the Spring 2.5 MVC framework and I'm already using the google api's to serve prototype and scriptaculous. I'm also considering using Amazon S3 and the new Cloudfront offering to minimize network latency.

like image 872
digitalsanctum Avatar asked Dec 10 '08 15:12

digitalsanctum


People also ask

What is CSS in JS pattern?

As CSS-in-JS is essentially JavaScript code, you can apply complex logic to your style rules, such as loops, conditionals, variables, state-based styling, and more. Therefore, it's an ideal solution if you need to create a complicated UI that requires dynamic functionality.

Where should JavaScript functions be placed?

Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

How do you write a JavaScript program?

To write a JavaScript, you need a web browser and either a text editor or an HTML editor. Once you have the software in place, you can begin writing JavaScript code. To add JavaScript code to an HTML file, create or open an HTML file with your text/HTML editor.


1 Answers

I add a parameter to the request with the revision number, something like:

<script type="text/javascript" src="/path/to/script.js?ver=456"></script>

The 'ver' parameter is updated automatically with each build (read from file, which the build updates). This makes sure the scripts are cached only for the current revision.

like image 178
Eran Galperin Avatar answered Nov 15 '22 20:11

Eran Galperin