Updating asset files after launch

Updating asset files after launch, a forum discussion on Jojo CMS. Join us for more discussions on Updating asset files after launch on our Administration (backend and configuration) forum.

Back to Forum Index : Back to Administration (backend and configuration)   RSS
Rick Rick

20 Dec 2011
Posts: 336

Hi guys,

How do you push updates to the asset files (CSS/JS) after launching a site? Eg once everyone has cached copies of these files with distant expire headers.

Does Jojo have a native way to do this that I've simply overlooked?
Jaijaz Jaijaz

20 Dec 2011
Posts: 215

The full proof way Harvey showed me was to put a local copy of the head.tpl into the theme and then add a get variable after it.

i.e. style.css?v=1

Then each time you push a change you can up the number. The new URL means that the old cache is ignored.

Also there is a cache figure in the options that you can make a really low number for a few days.

To be honest, most of the time I just educate the client about Crtl+F5.

Hope that helps.

p.s. one of these days I want to set up a way to make Jojo up the number automatically
If you not living on the edge you taking up too much space.
Rick Rick

21 Dec 2011
Posts: 336

Thanks Jai,

During production using CTRL+F5 is fine but once it goes public it's not an option.

I thought about the "?v" variable and making it semi automated. Eg when a file (eg styles.css) is built the most recent modified time from the source files is applied to the cached file. Then when displaying the links in head.tpl I thought about using hooks or whatever (I'll have to add support for additional vars to template hooks) to add the "?v=" then the modified timestamp for each asset file but only if it's more recent than the current date minus cache length.

This would let us make changes, hit CTRL+F5 and everyone would get the updates. The path would be changed to include "?v=" but would eventually drop back to just the clean file path.

Argh, when I have time.

On a side note, with Smarty you could run a hook from within a template and assign more stuff to Smarty then use it after the hook. You can't seem to do that with Dwoo. This was handy for putting the code to fetch sidebar content inside a hook so it was only fetched/assigned if the template triggered it.
Rick Rick

7 Jan 2012
Posts: 336

I messed round for a couple of hours and came up with this. It's a little messy because of the various caching methods in Jojo, but it's functional.

Files (CSS in particular) are cached to cache/public/<md5hash>.css instead of cache/css/styles.css. I'm not sure why this is, but the patch makes CSS files cache to the main directory like most other files. It still caches to the public cache dir for now, just like the JS one.

The plugin wraps a small hook around asset urls in head.tpl

<link rel="stylesheet" type="text/css" href="{cycle values=$NEXTASSET}{jojoAsset file="css/styles.css"}" />

this hook...
  • finds the cached file
  • gets its modified time
  • compares the modified time to the cache length setting and 28800 (default hardcoded content expiry for the asset files)
  • adds "?v=timestamp" to the path if the file has been modified more recently than the cache length
  • or adds "?r=randomnumber" to the path if _DEBUG is on

I've also made the stitcher try and apply a modified time when it generates cache files based on the most recently modified source file.


After updating an asset file you need to CTRL+F5 to generate a new cache file. After that all other visitors should get the new file, regardless of their expires headers.

You can CTRL+F5 as much as you please, the "version number" won't update until the file is actually changed.

You don't actually need the "file=" in there, but it's there to be consistent with jojoHook.

I haven't taken into account the database cache yet.

I run almost entirely in Linux, so haven't tested this on a Windows server. "touch" should work, but like I said, I haven't tested it on Windows.

I thought about doing more thorough checks in the hook to do away with the need to CTRL+F5. This would be useful for updating plugin/core assets and not having to refresh caches on lots of sites. But this would happen very rarely and isn't worth the overhead of checking modified times on every source file for the stitched assets. It'd also mean duplicating the list/looping of asset source files and it could break if something new (just like the modenizer files) were added to one but not the other. Since (in my use-cases at least) it's usually site specific files that are updated after launch, CTRL+F5 seems reasonable for triggering the updates.


I'm going to test this more thoroughly and see how it runs. If I'm heading off in the opposite direction to everyone else, let me know. Suggestions are most welcome.

Attached Files

Rick Rick

9 Jan 2012
Posts: 336

I've been hammering it all weekend with nothing but excellent results, but still would like to clean up the issue with CSS files being cached differently to JS files before I put this in the repo.

Is it intentional? Or a lapse? I can clean it all up but I don't want to go backwards on someone elses code.

Edit: FYI I've been testing it on a few sites but mainly over here along with Gravatar support.
Rick Rick

4 Apr 2012
Posts: 336

I've expanded this to all the files referenced in head.tpl (the patch above only did a few for testing) and committed it to the 1.0 branch.


Untested on a Windows server. This change relies on PHP being able to update the modified time of a file. This should work, but is untested.

Known Limitations

This won't trigger a new path (and therefore won't update users that haven't force refreshed) if the CSS has been updated using the Options table. It will only update if you've updated the CSS/JS file on the server.

I might update it later to be triggered by updating the CSS options in the Options table, but that will require either adding a "modified" field to the Options table (possibly overkill but would be useful for reference) or storing a hash of the content of each referenced option for comparison (an ugly hack).



5 Apr 2012
Posts: 379

Hey Rick - just trying it out now - it falls over for me unless I change the function to
runSmartyAssetHook($params, $smarty)
(ie not &$smarty)
I think that's a php5.3 thing?
Jaijaz Jaijaz

5 Apr 2012
Posts: 215

Hey Rick. Thanks for committing this.

I'm using the latest SVN server for local dev and everything broke after updating this commit. I'm on a tight deadline so haven't been able to check it out properly but basically the {jojoAsset file="css/jpop.css"} appear to not populate anything.

I've rolled back the head.tpl so I can keep pushing forward.

If I get time later I'll have a better look at it.

Note: I'm using PHP 5.3 on my MAMP stack.
If you not living on the edge you taking up too much space.
Rick Rick

5 Apr 2012
Posts: 336

Sorry guys, I've commited Tom's fix and have a PHP 5.3.6 server I'm in the process of setting up. I'll test this code on there tonight.
Back to Forum Index : Back to Administration (backend and configuration)   RSS
You must be logged in to post a reply

You need to Register or Log In before posting on these forums.