-
Notifications
You must be signed in to change notification settings - Fork 5
Asset pipeline
This guide covers the asset pipeline.
After reading this guide, you will know:
- What the asset pipeline is and what it does.
- How to properly organize your application assets.
- The benefits of the asset pipeline.
- How to package assets with a Laravel package.
The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages and pre-processors such as CoffeeScript, LESS, Sass(SCSS) and EJS.
The asset pipeline is based on the Node.js module Mincer and inspired by the Ruby gem Sprockets
Larasset uses by default the less
, coffee-script
, csswring
and uglify-js
modules, which are used for asset compression:
TODO: To set asset compression methods, set the appropriate configuration options
in app/config/packages/efficiently/larasset/config.php
- cssCompressor
for your CSS and
jsCompressor
for your JavaScript:
[
'assets' => [
'cssCompressor' => 'csswring',
'jsCompressor' => 'uglify-js',
// ...
],
],
NOTE: The uglify-js
and csswring
modules are used by default for JS and CSS compression if included
in package.json
.
The first feature of the pipeline is to concatenate assets, which can reduce the number of requests that a browser makes to render a web page. Web browsers are limited in the number of requests that they can make in parallel, so fewer requests can mean faster loading for your application.
Larasset concatenates all JavaScript files into one master .js
file and all
CSS files into one master .css
file. As you'll learn later in this guide, you
can customize this strategy to group files any way you like. In production,
Larasset inserts an MD5 fingerprint into each filename so that the file is cached
by the web browser. You can invalidate the cache by altering this fingerprint,
which happens automatically whenever you change the file contents.
The second feature of the asset pipeline is asset minification or compression. For CSS files, this is done by removing whitespace and comments. For JavaScript, more complex processes can be applied. You can choose from a set of built in options or specify your own.
The third feature of the asset pipeline is it allows coding assets via a higher-level language, with precompilation down to the actual assets. Supported languages include LESS for CSS, CoffeeScript for JavaScript, and EJS for both by default.
Fingerprinting is a technique that makes the name of a file dependent on the contents of the file. When the file contents change, the filename is also changed. For content that is static or infrequently changed, this provides an easy way to tell whether two versions of a file are identical, even across different servers or deployment dates.
When a filename is unique and based on its content, HTTP headers can be set to encourage caches everywhere (whether at CDNs, at ISPs, in networking equipment, or in web browsers) to keep their own copy of the content. When the content is updated, the fingerprint will change. This will cause the remote clients to request a new copy of the content. This is generally known as cache busting.
The technique larasset uses for fingerprinting is to insert a hash of the
content into the name, usually at the end. For example a CSS file global.css
global-908e25f4bf641868d8683022a5b62f54.css
This is the strategy adopted by the Larasset.
Some Web frameworks' old strategy was to append a date-based query string to every asset linked with a built-in helper. In the source the generated code looked like this:
/stylesheets/global.css?1309495796
The query string strategy has several disadvantages:
-
Not all caches will reliably cache content where the filename only differs by query parameters
Steve Souders recommends, "...avoiding a querystring for cacheable resources". He found that in this case 5-20% of requests will not be cached. Query strings in particular do not work at all with some CDNs for cache invalidation. -
The file name can change between nodes in multi-server environments.
The default query string in the web framework Ruby on Rails 2.x is based on the modification time of the files. When assets are deployed to a cluster, there is no guarantee that the timestamps will be the same, resulting in different values being used depending on which server handles the request. -
Too much cache invalidation
When static assets are deployed with each new release of code, the mtime (time of last modification) of all these files changes, forcing all remote clients to fetch them again, even when the content of those assets has not changed.
Fingerprinting fixes these problems by avoiding query strings, and by ensuring that filenames are consistent based on their content.
Fingerprinting is enabled by default for production and disabled for all other
environments. You can enable or disable it in your package configuration through the
larasset::digest
option.
More reading:
Commonly, all assets are located in subdirectories of
public
such as images
, javascripts
and stylesheets
.
With the asset pipeline, the preferred location for these assets is the app/assets
directory. Files in this directory are served by the Larasset middleware.
Assets can still be placed in the public
hierarchy. Any assets under public
will be served as static files by the application or web server. You should use
app/assets
for files that must undergo some pre-processing before they are
served.
In production, Larasset precompiles these files to public/assets
by default. The
precompiled copies are then served as static assets by the web server. The files
in app/assets
are never served directly in production.
TODO: Controller generator doesn't bundle assets yet.
When you generate a scaffold or a controller, Laravel also generates a JavaScript
file (or CoffeeScript file if the coffee-script
module is in the package.json
) and a
Cascading Style Sheet file (or LESS file if less
is in the package.json
)
for that controller. Additionally, when generating a scaffold, Laravel generates
the file scaffolds.css (or scaffolds.css.less if less
is in the
package.json
.)
For example, if you generate a ProjectsController
, Laravel will also add a new
file at app/assets/javascripts/projects.js.coffee
and another at
app/assets/stylesheets/projects.css.less
. By default these files will be ready
to use by your application immediately using the require_tree
directive. See
Manifest Files and Directives for more details
on require_tree.
You can also opt to include controller specific stylesheets and JavaScript files only in their respective controllers using the following:
{{ javascript_include_tag($params['controller']) }}
or {{ stylesheet_link_tag( $params['controller']) }}
NOTE. $params
variable must be pass by the controller to the view.
When doing this, ensure you are not using the require_tree
directive, as that
will result in your assets being included more than once.
WARNING: When using asset precompilation, you will need to ensure that your controller assets will be precompiled when loading them on a per page basis. By default .coffee and .less files will not be precompiled on their own. See Precompiling Assets for more information on how precompiling works.
Pipeline assets can be placed inside an application in one of three locations:
app/assets
, lib/assets
or provider/assets
.
-
app/assets
is for assets that are owned by the application, such as custom images, JavaScript files or stylesheets. -
lib/assets
is for your own libraries' code that doesn't really fit into the scope of the application or those libraries which are shared across applications. -
provider/assets
is for assets that are owned by outside entities, such as code for JavaScript plugins and CSS frameworks.
When a file is referenced from a manifest or a helper, Larasset searches the three default asset locations for it.
The default locations are: the images
, javascripts
and stylesheets
directories under the app/assets
folder, but these subdirectories
are not special - any path under assets/*
will be searched.
For example, these files:
app/assets/javascripts/home.js
lib/assets/javascripts/moovinator.js
provider/assets/javascripts/slider.js
provider/assets/somepackage/phonebox.js
would be referenced in a manifest like this:
//= require home
//= require moovinator
//= require slider
//= require phonebox
Assets inside subdirectories can also be accessed.
app/assets/javascripts/sub/something.js
is referenced as:
//= require sub/something
You can view the search path by inspecting
Config::get('larasset::paths');
in the Laravel console (php artisan tinker
).
Besides the standard assets/*
paths, additional (fully qualified) paths can be
added to the pipeline in app/config/packages/efficiently/larasset/config.php
. For example:
Config::set('larasset::paths', array_merge(Config::get('larasset::paths'), [base_path("lib/videoplayer/flash")]));
Paths are traversed in the order they occur in the search path. By default,
this means the files in app/assets
take precedence, and will mask
corresponding paths in lib
and provider
.
It is important to note that files you want to reference outside a manifest must be added to the precompile array or they will not be available in the production environment.
Larasset uses files named index
(with the relevant extensions) for a special
purpose.
For example, if you have a jQuery library with many modules, which is stored in
lib/assets/javascripts/library_name
, the file lib/assets/javascripts/library_name/index.js
serves as
the manifest for all files in this library. This file could include a list of
all the required files in order, or a simple require_tree
directive.
The library as a whole can be accessed in the application manifest like so:
//= require library_name
This simplifies maintenance and keeps things clean by allowing related code to be grouped before inclusion elsewhere.
Larasset add some new methods to access your assets - you can use javascript_include_tag
and stylesheet_link_tag
:
{{ stylesheet_link_tag("application", ["media" => "all"]) }}
{{ javascript_include_tag("application") }}
In regular views you can access images in the public/assets/images
directory
like this:
{{ image_tag("laravel.png") }}
Provided that the pipeline is enabled within your application (and not disabled
in the current environment context), this file is served by Larasset. If a file
exists at public/assets/laravel.png
it is served by the web server.
Alternatively, a request for a file with an MD5 hash such as
public/assets/laravel-af27b6a414e6da00003503148be9b409.png
is treated the same
way. How these hashes are generated is covered in the In
Production section later on in this guide.
Larasset will also look through the paths specified in the config option larasset::paths
,
which includes the standard application paths.
Images can also be organized into subdirectories if required, and then can be accessed by specifying the directory's name in the tag:
{{ image_tag("icons/laravel.png") }}
WARNING: If you're precompiling your assets (see In Production
below), linking to an asset that does not exist will raise an exception in the
calling page. This includes linking to a blank string. As such, be careful using
image_tag
and the other helpers with user-supplied data.
The asset pipeline automatically evaluates EJS. This means if you add an
ejs
extension to a CSS asset (for example, application.css.ejs
), then
helpers like asset_path
are available in your CSS rules:
.class { background-image: url(<%= asset_path('image.png') %>) }
This writes the path to the particular asset being referenced. In this example,
it would make sense to have an image in one of the asset load paths, such as
app/assets/images/image.png
, which would be referenced here. If this image is
already available in public/assets
as a fingerprinted file, then that path is
referenced.
If you want to use a data URI -
a method of embedding the image data directly into the CSS file - you can use
the asset_data_uri
helper.
#logo { background: url(<%= asset_data_uri('logo.png') %>) }
This inserts a correctly-formatted data URI into the CSS source.
Note that the closing tag cannot be of the style -%>
.
When using the asset pipeline, paths to assets must be re-written and
less
provides _path
helpers (underscored like in PHP and EJS)
for the following asset classes: image, font, video, audio,
JavaScript and stylesheet.
-
image_path("laravel.png")
becomes/assets/laravel.png
.
The more generic form can also be used:
-
asset_path("laravel.png")
becomes/assets/laravel.png
So you can do:
#logo {
@logo-path: asset_path('logo.png');
background: url(@logo-path);
}
-
Since Larasset v0.9.5, Sass is installed by default 😄
-
Before Larasset v0.9.5 :
To use Sass or SCSS, you need to run in a terminal, inside your Laravel application root:
npm install -g node-sass
For the rest, it's the same as CSS and EJS integration.
If you add an ejs
extension to a JavaScript asset, making it something such as
application.js.ejs
, you can then use the asset_path
helper in your
JavaScript code:
$('#logo').attr({ src: "<%= asset_path('logo.png') %>" });
This writes the path to the particular asset being referenced.
Similarly, you can use the asset_path
helper in CoffeeScript files with ejs
extension (e.g., application.js.coffee.ejs
):
$('#logo').attr src: "<%= asset_path('logo.png') %>"
TODO: Add a larasset::compress
config option to Larasset package.
Larasset uses manifest files to determine which assets to include and serve.
These manifest files contain directives - instructions that tell Larasset
which files to require in order to build a single CSS or JavaScript file. With
these directives, Larasset loads the files specified, processes them if
necessary, concatenates them into one single file and then compresses them (if config option
larasset::compress
is true). By serving one file rather
than many, the load time of pages can be greatly reduced because the browser
makes fewer requests. Compression also reduces file size, enabling the
browser to download them faster.
For example, a new Laravel application (with Larasset) includes a default
app/assets/javascripts/application.js
file containing the following lines:
// ...
//= require jquery
//= require_tree .
In JavaScript files, Larasset directives begin with //=
. In the above case,
the file is using the require
and the require_tree
directives. The require
directive is used to tell Larasset the files you wish to require. Here, you are
requiring the files jquery.js
that is available somewhere
in the search path for Larasset. You need not supply the extensions explicitly.
Larasset assumes you are requiring a .js
file when done from within a .js
file.
The require_tree
directive tells Larasset to recursively include all
JavaScript files in the specified directory into the output. These paths must be
specified relative to the manifest file. You can also use the
require_directory
directive which includes all JavaScript files only in the
directory specified, without recursion.
Directives are processed top to bottom, but the order in which files are
included by require_tree
is unspecified. You should not rely on any particular
order among those. If you need to ensure some particular JavaScript ends up
above some other in the concatenated file, require the prerequisite file first
in the manifest. Note that the family of require
directives prevents files
from being included twice in the output.
Larasset also creates a default app/assets/stylesheets/application.css
file
which contains these lines:
/* ...
*= require_self
*= require_tree .
*/
Artisan command php artisan larasset:setup
creates both app/assets/javascripts/application.js
and
app/assets/stylesheets/application.css
. This is
so you can easily add asset pipelining later if you like.
The directives that work in JavaScript files also work in stylesheets
(though obviously including stylesheets rather than JavaScript files). The
require_tree
directive in a CSS manifest works the same way as the JavaScript
one, requiring all stylesheets from the current directory.
In this example, require_self
is used. This puts the CSS contained within the
file (if any) at the precise location of the require_self
call. If
require_self
is called more than once, only the last call is respected.
NOTE. If you want to use multiple LESS files, you should generally use the LESS @import
directives
instead of these Larasset directives. When using Larasset directives, LESS files exist within
their own scope, making variables or mixins only available within the document they were defined in.
TODO: Check if we can do that with mincer
or less
modules.
You can have as many manifest files as you need. For example, the admin.css
and admin.js
manifest could contain the JS and CSS files that are used for the
admin section of an application.
The same remarks about ordering made above apply. In particular, you can specify individual files and they are compiled in the order specified. For example, you might concatenate three CSS files together this way:
/* ...
*= require reset
*= require layout
*= require chrome
*/
TODO: Laravel generators don't generate asset files yet.
The file extensions used on an asset determine what preprocessing is applied.
When a controller or a scaffold is generated with the default Larasset config, a
CoffeeScript file and a LESS file are generated in place of a regular JavaScript
and CSS file. The example used before was a controller called "projects", which
generated an app/assets/javascripts/projects.js.coffee
and an
app/assets/stylesheets/projects.css.less
file.
In development mode, or if the asset pipeline is disabled, when these files are
requested they are processed by the processors provided by the coffee-script
and less
modules and then sent back to the browser as JavaScript and CSS
respectively. When asset pipelining is enabled, these files are preprocessed and
placed in the public/assets
directory for serving by either the Laravel app or
web server.
Additional layers of preprocessing can be requested by adding other extensions,
where each extension is processed in a right-to-left manner. These should be
used in the order the processing should be applied. For example, a stylesheet
called app/assets/stylesheets/projects.css.less.ejs
is first processed as EJS,
then LESS, and finally served as CSS. The same applies to a JavaScript file -
app/assets/javascripts/projects.js.coffee.ejs
is processed as EJS, then
CoffeeScript, and served as JavaScript.
Keep in mind the order of these preprocessors is important. For example, if
you called your JavaScript file app/assets/javascripts/projects.js.ejs.coffee
then it would be processed with the CoffeeScript interpreter first, which
wouldn't understand EJS and therefore you would run into problems.
If Source Mapping is enabled on your Web Browser(Chrome 26+, Firefox 23+, Safari 6.1+, IE 11+), assets can be accessed as separate files via your Web Browser's debugger/developer tools if they are specified in the manifest file.
This manifest app/assets/javascripts/application.js
:
//= require core
//= require projects
//= require tickets
would allow you to access and debug this files:
/app/assets/javascripts/core.js
/app/assets/javascripts/projects.js
/app/assets/javascripts/tickets.js
TODO: Add a larasset::digest
config option to Larasset package.
You can turn off digests by updating the digest
option of app/config/packages/efficiently/larasset/config.php
file.
The larasset::digest
config option can also be changed on the fly:
Config::set('larasset::digest', false);
When this option is true, digests will be generated for asset URLs.
You can turn off source mapping by updating app/config/packages/efficiently/larasset/config.php
to
include this option:
'sourceMaps' => false,
When debug mode is off, Larasset concatenates and runs the necessary
preprocessors on all files. With debug mode turned off the manifest above would
generate instead:
<script src="/assets/application.js"></script>
Assets are compiled and cached on the first request after the server is started.
Larasset sets a must-revalidate
Cache-Control HTTP header to reduce request
overhead on subsequent requests - on these the browser gets a 304 (Not Modified)
response.
If any of the files in the manifest have changed between requests, the server responds with a new compiled file.
Debug mode can also be enabled in Larasset helper functions:
{{ stylesheet_link_tag("application", ["debug" => true]) }}
{{ javascript_include_tag("application", ["debug" => true]) }}
The debug
option is redundant if debug mode is already on.
You can also enable compression in development mode as a sanity check, and
disable it on-demand as required for debugging.
In the production environment Larasset uses the fingerprinting scheme outlined above. By default Laravel assumes assets have been precompiled and will be served as static assets by your web server.
During the precompilation phase an MD5 is generated from the contents of the compiled files, and inserted into the filenames as they are written to disc. These fingerprinted names are used by the Larasset helpers in place of the manifest name.
For example this:
{{ javascript_include_tag("application") }}
{{ stylesheet_link_tag("application") }}
generates something like this:
<script src="/assets/application-908e25f4bf641868d8683022a5b62f54.js"></script>
<link href="/assets/application-4dd5b109ee3439da54f5bdfd78a80473.css" media="screen"
rel="stylesheet" />
TODO: The larasset::digest
config option doesn't exist yet.
The fingerprinting behavior is controlled by the larasset::digest
Larasset config option (which defaults to true
for production and false
for
everything else).
NOTE: Under normal circumstances the default larasset::digest
option
should not be changed. If there are no digests in the filenames, and far-future
headers are set, remote clients will never know to refetch the files when their
content changes.
Larasset comes bundled with an artisan command to compile the asset manifests and other files in the pipeline.
Compiled assets are written to the location specified in larasset::prefix
config option.
By default, this is the /assets
directory.
You can call this task on the server during deployment to create compiled versions of your assets directly on the server. See the next section for information on compiling locally.
The artisan command is:
$ php artisan larasset:precompile --assets-env production
The default matcher for compiling files includes application.js
,
application.css
and all non-JS/CSS files (this will include all image assets
automatically) from app/assets
folders including your Laravel packages.
NOTE: The matcher (and other members of the precompile array; see below) is
applied to final compiled file names. This means anything that compiles to
JS/CSS is excluded, as well as raw JS/CSS files; for example, .coffee
and
.less
files are not automatically included as they compile to JS/CSS.
If you have other manifests or individual stylesheets and JavaScript files to
include, you can add them to the precompile
array in app/config/packages/efficiently/larasset/config.php
:
Config::set('larasset::precompile', array_merge(Config::get('larasset::precompile'), ['admin.js', 'admin.css', 'swfObject.js']));
TODO: Not yet working, the PHP script below need to be completed and tested.
Or, you can opt to precompile all assets with something like this:
// app/config/packages/efficiently/larasset/config.php
$assetsPrecompile = array_merge($assetsPrecompile, function($path) {
if (preg_match('/\.(css|js)\z/', $path)) {
$fullPath = Asset::resolve($path);
$appAssetsPath = base_path('app/assets');
if (starts_with($fullPath, $appAssetsPath)) {
echo "including asset: ".fullPath;
return true;
} else {
echo "excluding asset: ".fullPath;
return false;
}
} else {
return false;
}
});
NOTE. Always specify an expected compiled filename that ends with .js or .css, even if you want to add LESS or CoffeeScript files to the assetsPrecompile array.
The artisan command also generates a manifest-md5hash.json
that contains a list with
all your assets and their respective fingerprints. This is used by the Larasset
helper methods to avoid handing the mapping requests back to Larasset. A
typical manifest file looks like:
{
"assets": {
"application.js": "application-723d1be6cc741a3aabb1cec24276d681.js",
"application.css": "application-1c5752789588ac18d7e1a50b1f0fd4c2.css",
"favicon.ico": "favicon-a9c641bf2b81f0476e876f7c5e375969.ico",
"my_image.png": "my_image-231a680f23887d9dd70710ea5efd3c62.png"
},
"files": {
"application-723d1be6cc741a3aabb1cec24276d681.js": {
"logical_path": "application.js",
"mtime": "2013-07-26T22:55:03-07:00",
"size": 302506,
"digest": "723d1be6cc741a3aabb1cec24276d681"
},
"application-12b3c7dd74d2e9df37e7cbb1efa76a6d.css": {
"logical_path": "application.css",
"mtime": "2013-07-26T22:54:54-07:00",
"size": 1560,
"digest": "12b3c7dd74d2e9df37e7cbb1efa76a6d"
},
"application-1c5752789588ac18d7e1a50b1f0fd4c2.css": {
"logical_path": "application.css",
"mtime": "2013-07-26T22:56:17-07:00",
"size": 1591,
"digest": "1c5752789588ac18d7e1a50b1f0fd4c2"
},
"favicon-a9c641bf2b81f0476e876f7c5e375969.ico": {
"logical_path": "favicon.ico",
"mtime": "2013-07-26T23:00:10-07:00",
"size": 1406,
"digest": "a9c641bf2b81f0476e876f7c5e375969"
},
"my_image-231a680f23887d9dd70710ea5efd3c62.png": {
"logical_path": "my_image.png",
"mtime": "2013-07-26T23:00:27-07:00",
"size": 6646,
"digest": "231a680f23887d9dd70710ea5efd3c62"
}
}
}
The default location for the manifest is the root of the location specified in
larasset::prefix
config option ('/assets' by default).
TODO: Create an exception class in PHP, for missing asset files ?
NOTE: If there are missing precompiled files in production you will get an exception indicating the name of the missing file(s).
Precompiled assets exist on the file system and are served directly by your web server. They do not have far-future headers by default, so to get the benefit of fingerprinting you'll have to update your server configuration to add those headers.
For Apache:
# The Expires* directives requires the Apache module
# `mod_expires` to be enabled.
<Location /assets/>
# Use of ETag is discouraged when Last-Modified is present
Header unset ETag
FileETag None
# RFC says only cache for 1 year
ExpiresActive On
ExpiresDefault "access plus 1 year"
</Location>
For NGINX:
location ~ ^/assets/ {
expires 1y;
add_header Cache-Control public;
add_header ETag "";
break;
}
When files are precompiled, Larasset also creates a gzipped (.gz) version of your assets. Web servers are typically configured to use a moderate compression ratio as a compromise, but since precompilation happens once, Larasset uses the maximum compression ratio, thus reducing the size of the data transfer to the minimum. On the other hand, web servers can be configured to serve compressed content directly from disk, rather than deflating non-compressed files themselves.
NGINX is able to do this automatically enabling gzip_static
:
location ~ ^/(assets)/ {
root /path/to/public;
gzip_static on; # to serve pre-gzipped version
expires max;
add_header Cache-Control public;
}
This directive is available if the core module that provides this feature was
compiled with the web server. Ubuntu/Debian packages, even nginx-light
, have
the module compiled. Otherwise, you may need to perform a manual compilation:
./configure --with-http_gzip_static_module
A robust configuration for Apache is possible but tricky; please Google around. (Or help update this Guide if you have a good configuration example for Apache.)
If your assets are being served by a CDN, ensure they don't stick around in your cache forever. This can cause problems.
Every cache is different, so evaluate how your CDN handles caching and make sure that it plays nicely with the pipeline. You may find quirks related to your specific set up, you may not. The defaults NGINX uses, for example, should give you no problems when used as an HTTP cache.
If you want to serve only some assets from your CDN, you can use custom
host
option of asset_url
helper, which overwrites value set in
host
config option of your app/config/packages/efficiently/larasset/config.php
file.
asset_url('image.png', ['host' => 'http://cdn.example.com']);
NOTE: The larasset:host
option can also be changed on the fly:
Config::set('larasset::host', "http://cdn.example.com");
The public path that Larasset uses by default is /assets
.
This can be changed to something else:
Config::set('larasset::prefix', "/some_other_path");
Assets can also come from external sources in the form of Laravel packages.
A good example of this is the jquery-laravel
package which comes with Larasset as the
standard JavaScript library package. This jquery-laravel
package informs Larasset that its
directory may contain assets and the app/assets
, lib/assets
and
provider/assets
directories of this package are added to the search paths of
Larasset.
- Working with JavaScript and Larasset.
- Server generated JavaScript Responses with Laravel and Larasset.
-
An Introduction to Source Maps for Google Chrome and Mozilla Firefox
- Source mapping for Apple Safari 6.1+
- Source mapping even for Microsoft Internet Explorer 11
The Rails Guide Asset Pipeline.