[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.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末隔披,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子蹂风,更是在濱河造成了極大的恐慌忆嗜,老刑警劉巖珠闰,帶你破解...
    沈念sama閱讀 216,919評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件肌割,死亡現(xiàn)場(chǎng)離奇詭異矢棚,居然都是意外死亡徙鱼,警方通過查閱死者的電腦和手機(jī)苍日,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門惭聂,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人相恃,你說我怎么就攤上這事辜纲。” “怎么了拦耐?”我有些...
    開封第一講書人閱讀 163,316評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵耕腾,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我杀糯,道長(zhǎng)扫俺,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,294評(píng)論 1 292
  • 正文 為了忘掉前任固翰,我火速辦了婚禮狼纬,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘骂际。我一直安慰自己疗琉,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,318評(píng)論 6 390
  • 文/花漫 我一把揭開白布歉铝。 她就那樣靜靜地躺著盈简,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上柠贤,一...
    開封第一講書人閱讀 51,245評(píng)論 1 299
  • 那天香浩,我揣著相機(jī)與錄音,去河邊找鬼种吸。 笑死弃衍,一個(gè)胖子當(dāng)著我的面吹牛呀非,可吹牛的內(nèi)容都是我干的坚俗。 我是一名探鬼主播,決...
    沈念sama閱讀 40,120評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼岸裙,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼猖败!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起降允,我...
    開封第一講書人閱讀 38,964評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤恩闻,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后剧董,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體幢尚,經(jīng)...
    沈念sama閱讀 45,376評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,592評(píng)論 2 333
  • 正文 我和宋清朗相戀三年翅楼,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了尉剩。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,764評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡毅臊,死狀恐怖理茎,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情管嬉,我是刑警寧澤皂林,帶...
    沈念sama閱讀 35,460評(píng)論 5 344
  • 正文 年R本政府宣布,位于F島的核電站蚯撩,受9級(jí)特大地震影響础倍,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜胎挎,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,070評(píng)論 3 327
  • 文/蒙蒙 一著隆、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧呀癣,春花似錦美浦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春流酬,著一層夾襖步出監(jiān)牢的瞬間币厕,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工芽腾, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留旦装,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,819評(píng)論 2 370
  • 正文 我出身青樓摊滔,卻偏偏與公主長(zhǎng)得像阴绢,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子艰躺,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,665評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,489評(píng)論 0 23
  • Awesome Ruby Toolbox Awesome A collection of awesome Ruby...
    debbbbie閱讀 2,863評(píng)論 0 3
  • 作為一個(gè)不是處女座的強(qiáng)迫癥患者呻袭,從四字變成三字是需要勇氣的。 剛才刪掉了一些內(nèi)容腺兴,看著曾經(jīng)犯過的傻左电, 傻氣中的自己...
    brekker閱讀 140評(píng)論 0 0
  • 小米的MIUI系統(tǒng)一直是國(guó)內(nèi)最受消費(fèi)者歡迎的手機(jī)系統(tǒng)之一篓足,用戶群體也是中國(guó)移動(dòng)互聯(lián)網(wǎng)中最為活躍的用戶人群,而經(jīng)過五...
    科技達(dá)人15閱讀 593評(píng)論 0 1
  • “博學(xué)之闰蚕,審問之栈拖,慎思之,明辨之陪腌,篤行之辱魁。 有弗學(xué),學(xué)之弗能诗鸭,弗措也染簇。有弗問,問之弗知强岸,弗措也锻弓。...
    錢江潮369閱讀 1,783評(píng)論 3 4