I like this feature for linking CSS within the templates:

<link href="{stylesheet='styles/main'}" rel="stylesheet">

The great thing about this is that it does versioning automatically, which means I don't have to worry about old styles being cached, or trying to rename my CSS files, etc.

Is there a way to do the same for JS files? Right now, I do this:

<script src="{path="scripts/main"}"></script>

It doesn't do the versioning thing.

  • 2
    Perhaps worth noting that unless you really need to use a stylesheet template (eg because you want to insert dynamic values into JS/CSS from EE) for performance reasons you're much better off using a flat file. – Tom Davies Nov 29 '12 at 10:29
  • If I save the template as a file, does EE use the file instead? – ExpressionEngineNewbie Nov 29 '12 at 19:03
  • Yep, but even if so that wouldn't be faster as it would still be being served by EE and passed thru the template engine. It's actually slightly faster to serve templates from the DB that from a template file anyway, though you lose the CVS benefits. However far faster again is to use a true flat file that is served by the web server without EE having to do a thing... – Tom Davies Nov 29 '12 at 20:20
up vote 15 down vote accepted

Yeah, the smart cache-busting is a useful feature of keeping your CSS in EE. But add-ons will let you do the same and keep your CSS outside of EE.

To get similar behavior for your JS files you need an an add-on.

I recommend Minimee. Not only does it combine and minify CSS and JS, creating a cached file with a unique name, it also automatically creates a new version of the file if there are changes to your CSS or JS. It has useful config overrides and a ton of other features to boot. It will also free you up to have this same smart cache-busting benefit while keeping your CSS and JS outside of EE.

  • 2
    +1 for minimee, as it creates a file with a unique name, rather than just appending a query string, which doesn't always bust caches (depending on the client/caching server) – Tom Davies Nov 29 '12 at 12:38

I generally keep my CSS & JS outside of the EE template parser. That said, you could do something like this:

<script src="{path="scripts/main"}{current_time}"></script>

on each refresh that will change.

  • This doesn't provide quite the same benefit since it will prevent the browser ever using a cached version of the JS. It will always break the cache, rather than breaking it only when needed. – Alex Kendrick Nov 29 '12 at 3:07
  • Right, this won't be good for caching. – ExpressionEngineNewbie Nov 30 '12 at 0:19

Unfortunately there's no way to version javascript templates natively.

Your best bet is to go with the add-on Alex mentioned.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.