3

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.

3
  • 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, 2012 at 10:29
  • If I save the template as a file, does EE use the file instead? Nov 29, 2012 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, 2012 at 20:20

3 Answers 3

15

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.

1
  • 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, 2012 at 12:38
1

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.

2
  • 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, 2012 at 3:07
  • Right, this won't be good for caching. Nov 30, 2012 at 0:19
1

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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