29.07.2016 Views

laravel-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Laravel Elixir 233<br />

Versioning / Cache Busting<br />

Many developers suffix their compiled assets with a timestamp or unique token to force browsers to<br />

load the fresh assets instead of serving stale copies of the code. Elixir can handle this for you using<br />

the version method.<br />

The version method accepts a file name relative to the public directory, and will append a unique<br />

hash to the filename, allowing for cache-busting. For example, the generated file name will look<br />

something like: all-16d570a7.css:<br />

1 elixir(function(mix) {<br />

2 A> mix.version('css/all.css');<br />

3<br />

4 });<br />

After generating the versioned file, you may use Laravel’s global elixir PHP helper function within<br />

your views to load the appropriately hashed asset. The elixir function will automatically determine<br />

the name of the hashed file:<br />

.<br />

1 <br />

Versioning Multiple Files<br />

You may pass an array to the version method to version multiple files:<br />

1 elixir(function(mix) {<br />

2 A> mix.version(['css/all.css', 'js/app.js']);<br />

3<br />

4 });<br />

Once the files have been versioned, you may use the elixir helper function to generate links to the<br />

proper hashed files. Remember, you only need to pass the name of the un-hashed file to the elixir<br />

helper function. The helper will use the un-hashed name to determine the current hashed version<br />

of the file:<br />

.<br />

1 <br />

2<br />

3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!