Baking assets

edmundask edited this page Dec 17, 2011 · 1 revision

Filters such as jsmin, cssmin and others that minify or do heavy processing cost valuable execution time on each page. Sprinkle always caches assets after they have been processed to minimize situations like this but if the content of the original asset source changes, it is then processed again. Therefore a visitor may be the "trigger" which causes one or more assets to be processed and he would have to wait until it is finished to fully load the page.

Luckily, Sprinkle has a special method bake() which you can call to bake (process) all pre-defined assets at once. For now this method works directly with disable_processing option in the configuration.

To start using asset baking, first disable automatic asset processing in the configuration:

$config['sprinkle']['disable_processing'] = TRUE;

Then you have multiple ways of using asset baking. If you have a controller (or multiple controllers) to run cronjobs, you can call it from there. On the other hand, if you have terminal/console/command prompt available for you, you can simply set up a CLI controller and run the processing task manually when needed. Here we'll use the latter one.

CLI Controller For Asset Baking

We'll name our controller tools just for the sake of this example.

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Tools extends CI_Controller 
{
	public function sprinkle_bakery()
	{
		// Load Sprinkle
		$this->load->spark('sprinkle/1.0.0');

		echo "Baking assets...\n";
		$this->benchmark->mark('code_start');
		// Bake!
		$this->sprinkle->bake(TRUE);
		$this->benchmark->mark('code_end');
		echo "Finished (". $this->benchmark->elapsed_time('code_start', 'code_end') ."s) \n";
	}
}

The code above is pretty straightforward. The only thing worth mentioning here is the optional parameter when calling bake() method. By default Sprinkle bakes assets silently. However, when executing the task via console or command prompt it is handy to see what is going on. Therefore, the optional parameter lets you enable verbose mode which will tell you which asset is being processed, how long it took to bake it etc.

To execute the command, open the terminal (Unix) or command prompt (Windows), navigate to your project and execute:

php index.php tools sprinkle_bakery

You should then see something like this (depending on what pre-defined assets you have):

Baking assets...
[1] Baking jquery (version: default)... 
Baked assets/cache/jquery-default-b861fb5c.js (2.8548s) 
[2] Baking jquery (version: 1.5.1)... 
Baked assets/cache/jquery-1.5.1-cac8193f.js (2.2423s) 
[3] Baking jquery (version: 1.7.1)... 
Baked assets/cache/jquery-1.7.1-76f81620.js (2.8048s) 
[4] Baking backbone (version: default)... 
Baked assets/cache/backbone-default-5ca06e44.js (0.3895s) 
[5] Baking reset (version: default)... 
Baked assets/cache/reset-default-072a486f.css (0.1028s) 
[6] Baking test (version: default)... 
Baked assets/cache/main-default-cdddbdc8.css (0.3165s) 
[7] Baking less-test (version: default)... 
Baked assets/cache/test-default-e07c8190.css (0.3718s) 
Finished (9.0845s) 

As you can see, Sprinkle bakes every pre-defined asset and every version of it since you may be using different asset versions on different pages.

Situations To Consider

By default Sprinkle does not flush the cache but only replaces those files with identical filenames. Clearing the cache should not be necessary but you surely can do it if you want. Below is an updated example to reflect this.

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Tools extends CI_Controller 
{
	public function sprinkle_bakery()
	{
		// Load Sprinkle
		$this->load->spark('sprinkle/1.0.0');

		echo "Baking assets...\n";
		$this->benchmark->mark('code_start');
		// Flush the cache first!
		$this->sprinkle->flush_cache();
		// Bake!
		$this->sprinkle->bake(TRUE);
		$this->benchmark->mark('code_end');
		echo "Finished (". $this->benchmark->elapsed_time('code_start', 'code_end') ."s) \n";
	}
}

Also, if you disable processing and use asset baking, you should set cache_expiration to 0. This will ensure that cached files stay there indefinitely. Set up a cronjob to flush the cache and bake the assets instead.

$config['sprinkle']['cache_expiration'] = 0;