[RoR]Asset Pipeline

  • app/assets
  • vendor/assets
  • lib/assets

Manifest files
  • app/assets/javascripts/application.js
  • app/assets/stylesheets/application.css
  • require
  • include
  • require_self
  • require_directory
  • require_tree
  • depend_on
Search path
  • config.assets.paths << Rails.root.join("app", "flash", "assets")
  • // this will load the app/assets/javascripts/library/foo.js //= require 'library/foo'
Gemified assets
Index files
Format handlers
Custom format handlers

Below is the handler class from the >Rabl gem, used to generate JSON using templates.
module ActionView module Template::Handlers class Rabl class_attribute :default_format self.default_format = Mime::JSON def self.call( template ) ommitted for clarity... end end end end


Post-Processing

In addition to pre-processing various formats into JavaScripts and stylesheets, the asset pipeline can also post-process the results. By default post-processing compressors are available for both stylesheets and JavaScripts.

Stylesheets

By default stylesheets are compressed using the YUI Compressor. You can control it by changing the config.assets.css_compressor configuration option, that is set to yui by default. When using Sass in a Rails project, one could set the CSS compressor to use Sass's standard compressor with the config.assets.css_compressor = :sass option.

Javascripts

There are several Javascript compression options available: :closure, :uglifier, and :yui, provided by closure-compiler, uglifier or yui-compressor gems respectively. The :uglifier option is the default, you can control it by changing the config.assets.js_compressor configuration option.

Custom Compressor

You can use a custom post-processor by defining a class with a compress method that accepts a string and assigning an instance of it to one of the configuration options above, like this:
class MyProcessor def compress(string) do something end end

config.assets.css_compressor = MyProcessor.new


Helpers

<%= stylesheet_link_tag "application" %> <%= javascript_include_tag "application" %>
By default, Rails only seeks to precompile assets named "application". If you have a good reason to break off additional bundles of assets, like for the admin section of your app, tell the pipeline to precompile those bundles by adding the names of the manifest files to the config.precompile array in config/application.rb.
config.assets.precompile += %w(admin.js optional.js}

Images

The venerable image_tag helper has been updated so that it knows to search asset/images and not just the public folder. It will also search through the paths specified in the config.assets.paths setting and any additional paths added by gems. If you’re passing user-supplied data to the image_tag helper, note that a blank or non-existant path will raise a server exception during processing of the template.

Getting the URL of an asset file

The asset_path and asset_url helpers can be used if you need to generate the URL of an asset. But you'd need to make sure to include the .erb file extension at the right-most position. For example, consider the following snippet of Javascript taken from a file named *transitions.js.erb * contains the line:
this.loadImage('<%= asset_path "noise.jpg" %>')
The asset pipeline runs the source through ERB processing first, and interpolates in the correct path to the desired JPG file.

Built-in SASS asset path helpers

Reusing a familiar pattern, image-url("rails.png") becomes url(/assets/rails.png) and image-path("rails.png") becomes "/assets/rails.png". The more generic form can also be used but the asset path and class must both be specified: asset-url("rails.png", image) becomes url(/assets/rails.png) and asset-path("rails.png",image) becomes "/assets/rails.png".

Data URIs

asset_data_uri


Fingerprinting

Fingerprinting makes the file name dependent on the files’ content, so that the filename only ever changes when the actual file content is changed.


Serving the files

To take full advantage of asset fingerprinting provided by the asset pipeline, you should configure your web server to set headers on your precompiled assets to a far-future expiration date. With cache headers in place,a client will only request an asset once until either the filename changes, or the cache has expired.
Below is one for Nginx:
location ~ ^/assets/ { expires 1y; add_header Cache-Control public; add_header Last-Modified ""; add_header ETag ""; break; }
The fingerprinting feature is controller by config.assets.digest Rails setting. By default it is only set in production environment.
Note that the asset pipeline always makes copies of non-fingerprinted asset file available in the same /assets directory.
While you’re thinking about how your asset files are being served, it’s worth investigating the possibility of seriously improving app performance by having your web server serve asset files directly instead of involving the Rails stack. Apache and Nginx support this option out of the box, and you enable it by turning on right option in production.rb:
config.action_dispatch.x_sendfile_head = 'X-Accel-Redirect' # for Nginx


Rake Tasks

When in production mode, Rails expects all manifests and asset files to be pre-compiled on disk and available to be served up by your web server out of the location specified in config.assets.prefix setting, which defaults to public/assets. Compiled asset files should not be versioned in source control, and the default .gitignore file for Rails includes a line for public/assets/*.
As part of deploying your application to production, you’ll call the following rake task to create compiled versions of your assets directly on the server:
$ RAILS_ENV=production bundle exec rake assets:precompile
Also note that local pre-compilation will result in a bunch of unwanted files in your /public/assets directory that will be served up instead of the originals. You’ll be scratching your head wondering why changes to your JS and CSS files are not being reflected in your browser. If that happens, you need to delete the compiled assets. Use the rake assets:clobber task to get rid of them.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,185评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,445评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,684评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,564评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,681评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,874评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,025评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,761评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,217评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,545评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,694评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,351评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,988评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,778评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,007评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,427评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,580评论 2 349

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,437评论 0 23
  • Awesome Ruby Toolbox Awesome A collection of awesome Ruby...
    debbbbie阅读 2,814评论 0 3
  • 作为一个不是处女座的强迫症患者,从四字变成三字是需要勇气的。 刚才删掉了一些内容,看着曾经犯过的傻, 傻气中的自己...
    brekker阅读 135评论 0 0
  • 小米的MIUI系统一直是国内最受消费者欢迎的手机系统之一,用户群体也是中国移动互联网中最为活跃的用户人群,而经过五...
    科技达人15阅读 591评论 0 1
  • “博学之,审问之,慎思之,明辨之,笃行之。 有弗学,学之弗能,弗措也。有弗问,问之弗知,弗措也。...
    钱江潮369阅读 1,780评论 3 4