SlideShare a Scribd company logo
Asset Pipeline (2)
ROR lab. DD-1
- The 9th round -
July 31, 2013
Hyoseong Choi
Assets
• javascripts
• stylesheets
• images
• fonts
• videos
• audios
Application
Rails
Pipeline
Web Server
Nginx
Apache
Middleware
Sprockets
Three pipelines
• app/assets
• lib/assets
• vendor/assets
• public/assets
since
Rails 3.1
finger
printing static assets
rakeass
ets:precompile
Sprockets
• concatenate
• minimize or compress
• using high-level
languages
for Assets
Assets-writing
Languages
• CoffeeScript
• Sass
• ERB
• others
javascript
stylesheet
ruby code
*.js.coffee
*.css.scss
*.js.coffee.erb
*.css.scss.erb
If you don’t want
• in Application.rb
config.assets.enabled = false
$ rails new appname --skip-sprockets
• on creating a new application
레일스
4%에서는?
Cache Busting
• MD5 fingerprinting
global-908e25f4bf641868d8683022a5b62f54.css
a hash of the content
CSS and Sass
body {
background: asset-url('image.jpg');
background: image-url('image.jpg');
background: url(asset-path('image.jpg'));
background: url(image-path('image.jpg'));
}
*.scss on `sass-rails` gem
asset-url, in sass => asset_url, in ruby
image-url, in sass => image_url in ruby
asset-path, in sass => asset_path in ruby
image-path, in sass=> image_path in ruby
hyphenated in sass, underscored in ruby
CSS & ERB
using data URI
#logo { background: url(<%= asset_data_uri 'logo.png' %>) }
a method of embedding the image data directly into the CSS file
#logo{background:url(data:image/png;base64,%2F9j
%2F4AAQSkZJRgABAQEASABIAADzUQB7i1uNbtRBPry3Xge2E42x6dsEAQBAcH
xTC2z8QxVrmj3C6Yp58%2FCycDDHHyc3un0Czur64ZXK
%2FQ79JZt0M83ulvfYru6EA%2B7yd%2BBx%2B74eo5fRddVivr6u
....
%2B8eY39V5Wo9mzh5qN16fsPNHjdHMXClp60mWncWTMOcA4c0j8QV46nKmfoY
ySe6Oz4Iv1VdbfUw151T0bgx0521gjIz%2FMMb)}
To add pipelines
config.assets.paths
<< Rails.root.join("lib", "videoplayer", "flash")
in config/application.rb
lib/videoplayer/flash/abc.css.scss
*= require abc (in application.css)
lib/videoplayer/flash/abc.js.coffee
/= require abc (in application.js)
production.rb
• config.assets.enabled = true
• config.assets.precompile += %w(*.png
*.jpg *.jpeg *.gif)
• config.assets.compress = true
• config.assets.js_compressor = :uglifier
• config.assets.css_compressor = :sass
• config.serve_static_assets = true
• config.assets.digest = true
• config.assets.version = '1.0'
Controller-specific
Assets
*.js.coffee
and
*.css.scss
a javascript
and
a stylesheet
a scaffold
or
a controller
Generator
<%= javascript_include_tag params[:controller] %>
<%= stylesheet_link_tag params[:controller] %>
Precompile Assets
# Do not fallback to assets pipeline if a precompiled
asset is missed.
config.assets.compile = false
$ RAILS_ENV=production rake assets:precompile
in config/environments/production.rb
in terminal console
on deployment with Capistrano
$ cap deploy:assets:precompile
Compile Assets
# Do not fallback to assets pipeline if a precompiled
asset is missed.
config.assets.compile = true
$ RAILS_ENV=production rake assets:precompile
in config/environments/production.rb
in terminal console
on deployment with Capistrano
$ cap deploy:assets:precompile
?
`Manifest` File
• to make a whitelist of assets included and
served
• to build a single CSS or javascript file
• Directives :
★ `require`
★ `require_tree` (with recursion)
★ `require_directory` (without recursion)
Server -e option
$ rails s (-e development)
$ rails s -e production
Environments
In development mode
In production mode
Before Deploy
• For the simulation of Production
Environment in local machine
1. Check config/production.rb
config.serve_static_assets = false true
로컬머신에서 production mode로 웹서버를 실행하면 어플리케
이션 서버(webrick, puma, thin...)가 static assets을 인식 못함.
따라서 레일스가 이를 대신할 수 있도록 true로 지정해 둠.
Before Deploy
• For the simulation of Production
Environment in local machine
2. Precompile assets
$ RAILS_ENV=production rake assets:precompile
결과로, public/assets 디렉토리가 생성되고 컴파일된 자원들이
fingerprinting되어 위치하게 됨.
Before Deploy
• For the simulation of Production
Environment in local machine
3. Run local Web Server in production mode
$ rails s -e production
Before Deploy
• For the simulation of Production
Environment in local machine
4. Revert config/production.rb to
config.serve_static_assets = true false
production mode로 시뮬레이션 작업이 끝나고 실제 원격 운영
서버로 배포하기 전에 false 값으로 되돌려 놓음. 이유는,Apache
나 Nginx 웹서버가 컴파일된 파일들을 로드하기 때문.
Deploy after Local
Precompilation
• not have write access to your production file system
• deploying to more than one server
• doing frequent deploys that do not include asset changes
Indications :
Deploy after Local
Precompilation
• not run the Capistrano deployment task that
precompiles assets.
• change the following two application configuration
settings
Treatments :
config.assets.prefix = "/dev-assets"
config.assets.initialize_on_precompile = false
config/environments/development.rb
config/application.rb
ROR Lab.
감사합니다.

More Related Content

PDF
Why learn jenkins via nomad_ci (nomad/consul/docker/jenkins) 
PDF
GitHub + Mapping Apps in 100 lines or less using the ArcGIS API for JavaScript
PDF
Rupy2012 ArangoDB Workshop Part2
PPTX
Building Docker Containers @ Scale
PDF
Sheep it
PPTX
Writing New Relic Plugins: NSQ
PPT
Big Data DC - BenchPress
KEY
大規模環境でRailsと4年間付き合ってきて@ クックパッド * 食べログ合同勉強会
Why learn jenkins via nomad_ci (nomad/consul/docker/jenkins) 
GitHub + Mapping Apps in 100 lines or less using the ArcGIS API for JavaScript
Rupy2012 ArangoDB Workshop Part2
Building Docker Containers @ Scale
Sheep it
Writing New Relic Plugins: NSQ
Big Data DC - BenchPress
大規模環境でRailsと4年間付き合ってきて@ クックパッド * 食べログ合同勉強会

What's hot (20)

PDF
mruby で mackerel のプラグインを作るはなし
PDF
Ruby on Rails & PostgreSQL - v2
PDF
Rack
PDF
Altitude SF 2017: Stories from TED
PDF
Harnessing Spark and Cassandra with Groovy
PDF
Pie on AWS
PDF
Nomad Multi-Cloud
PPT
Jasig rubyon rails
PDF
How to Begin Developing Ruby Core
PDF
The secret of programming language development and future
PDF
Altitude SF 2017: Nomad and next-gen application architectures
PPTX
Gobblin on-aws
PDF
Rails engines
PDF
CliqueSquare processing
PDF
There and Back Again: How We Drank the Chef Kool-Aid, Sobered Up, and Learned...
PPTX
Effective terraform
PDF
Consuming REST services with ActiveResource
PDF
Building a Better Mousetrap for the Cloud
PDF
Application devevelopment with open source libraries
PPTX
Embracing Serverless Ops (Lightning Talk)
mruby で mackerel のプラグインを作るはなし
Ruby on Rails & PostgreSQL - v2
Rack
Altitude SF 2017: Stories from TED
Harnessing Spark and Cassandra with Groovy
Pie on AWS
Nomad Multi-Cloud
Jasig rubyon rails
How to Begin Developing Ruby Core
The secret of programming language development and future
Altitude SF 2017: Nomad and next-gen application architectures
Gobblin on-aws
Rails engines
CliqueSquare processing
There and Back Again: How We Drank the Chef Kool-Aid, Sobered Up, and Learned...
Effective terraform
Consuming REST services with ActiveResource
Building a Better Mousetrap for the Cloud
Application devevelopment with open source libraries
Embracing Serverless Ops (Lightning Talk)
Ad

Viewers also liked (11)

PDF
ActiveRecord Query Interface (2), Season 2
PDF
Self join in active record association
KEY
Getting started with Rails (2), Season 2
KEY
Active Record Validations, Season 1
KEY
Getting started with Rails (3), Season 2
PDF
Getting Started with Rails (2)
KEY
ActiveRecord Association (1), Season 2
KEY
Active Record Query Interface (1), Season 2
PDF
Spring batch overivew
PPT
Ruby on Rails testing with Rspec
PDF
Spring Batch Workshop
ActiveRecord Query Interface (2), Season 2
Self join in active record association
Getting started with Rails (2), Season 2
Active Record Validations, Season 1
Getting started with Rails (3), Season 2
Getting Started with Rails (2)
ActiveRecord Association (1), Season 2
Active Record Query Interface (1), Season 2
Spring batch overivew
Ruby on Rails testing with Rspec
Spring Batch Workshop
Ad

Similar to Asset Pipeline in Ruby on Rails (20)

PDF
Padrino - the Godfather of Sinatra
PDF
Intro to sbt-web
ODP
Rails 3 Asset Pipeline
PDF
Getting Started with Rails on GlassFish (Hands-on Lab) - Spark IT 2010
PDF
Demystifying Ruby on Rails
PDF
Rails 3.1 Asset Pipeline
PDF
Understanding asset pipeline plugin
PDF
Extending Build to the Client: A Maven User's Guide to Grunt.js
PDF
(2018) Webpack Encore - Asset Management for the rest of us
PDF
AngularJS meets Rails
PDF
ScyllaDB V Developer Deep Dive Series: Rust-Based Drivers and UDFs with WebAs...
PDF
OpenStack Deployments with Chef
PDF
Australian OpenStack User Group August 2012: Chef for OpenStack
KEY
Ruby on Rails survival guide of an aged Java developer
PPTX
Rails Asset Pipeline - What, Why, Tips, Do's and Dont's
PDF
Achieving Infrastructure Portability with Chef
PPTX
Building assets on the fly with Node.js
KEY
Supa fast Ruby + Rails
PDF
Chef for OpenStack- Fall 2012.pdf
PDF
Chef for OpenStack - OpenStack Fall 2012 Summit
Padrino - the Godfather of Sinatra
Intro to sbt-web
Rails 3 Asset Pipeline
Getting Started with Rails on GlassFish (Hands-on Lab) - Spark IT 2010
Demystifying Ruby on Rails
Rails 3.1 Asset Pipeline
Understanding asset pipeline plugin
Extending Build to the Client: A Maven User's Guide to Grunt.js
(2018) Webpack Encore - Asset Management for the rest of us
AngularJS meets Rails
ScyllaDB V Developer Deep Dive Series: Rust-Based Drivers and UDFs with WebAs...
OpenStack Deployments with Chef
Australian OpenStack User Group August 2012: Chef for OpenStack
Ruby on Rails survival guide of an aged Java developer
Rails Asset Pipeline - What, Why, Tips, Do's and Dont's
Achieving Infrastructure Portability with Chef
Building assets on the fly with Node.js
Supa fast Ruby + Rails
Chef for OpenStack- Fall 2012.pdf
Chef for OpenStack - OpenStack Fall 2012 Summit

More from RORLAB (20)

PDF
Getting Started with Rails (4)
PDF
Getting Started with Rails (3)
PDF
Getting Started with Rails (1)
PDF
레일스가이드 한글번역 공개프로젝트 RORLabGuides 소개
PDF
Active Support Core Extension (3)
PDF
Active Support Core Extension (2)
PDF
Active Support Core Extensions (1)
PDF
Action Controller Overview, Season 2
PDF
Action View Form Helpers - 2, Season 2
PDF
Action View Form Helpers - 1, Season 2
PDF
Layouts and Rendering in Rails, Season 2
KEY
Active Record Association (2), Season 2
KEY
ActiveRecord Callbacks & Observers, Season 2
KEY
ActiveRecord Validations, Season 2
KEY
Rails Database Migration, Season 2
KEY
Getting started with Rails (4), Season 2
KEY
Getting started with Rails (1), Season 2
KEY
Routing 2, Season 1
KEY
Routing 1, Season 1
KEY
Action Controller Overview, Season 1
Getting Started with Rails (4)
Getting Started with Rails (3)
Getting Started with Rails (1)
레일스가이드 한글번역 공개프로젝트 RORLabGuides 소개
Active Support Core Extension (3)
Active Support Core Extension (2)
Active Support Core Extensions (1)
Action Controller Overview, Season 2
Action View Form Helpers - 2, Season 2
Action View Form Helpers - 1, Season 2
Layouts and Rendering in Rails, Season 2
Active Record Association (2), Season 2
ActiveRecord Callbacks & Observers, Season 2
ActiveRecord Validations, Season 2
Rails Database Migration, Season 2
Getting started with Rails (4), Season 2
Getting started with Rails (1), Season 2
Routing 2, Season 1
Routing 1, Season 1
Action Controller Overview, Season 1

Recently uploaded (20)

PDF
Complications of Minimal Access Surgery at WLH
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Cell Structure & Organelles in detailed.
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
Complications of Minimal Access Surgery at WLH
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
human mycosis Human fungal infections are called human mycosis..pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
TR - Agricultural Crops Production NC III.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
O7-L3 Supply Chain Operations - ICLT Program
Cell Structure & Organelles in detailed.
Microbial disease of the cardiovascular and lymphatic systems
Supply Chain Operations Speaking Notes -ICLT Program
Basic Mud Logging Guide for educational purpose
Final Presentation General Medicine 03-08-2024.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Microbial diseases, their pathogenesis and prophylaxis
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
O5-L3 Freight Transport Ops (International) V1.pdf

Asset Pipeline in Ruby on Rails

  • 1. Asset Pipeline (2) ROR lab. DD-1 - The 9th round - July 31, 2013 Hyoseong Choi
  • 2. Assets • javascripts • stylesheets • images • fonts • videos • audios
  • 3. Application Rails Pipeline Web Server Nginx Apache Middleware Sprockets Three pipelines • app/assets • lib/assets • vendor/assets • public/assets since Rails 3.1 finger printing static assets rakeass ets:precompile
  • 4. Sprockets • concatenate • minimize or compress • using high-level languages for Assets
  • 5. Assets-writing Languages • CoffeeScript • Sass • ERB • others javascript stylesheet ruby code *.js.coffee *.css.scss *.js.coffee.erb *.css.scss.erb
  • 6. If you don’t want • in Application.rb config.assets.enabled = false $ rails new appname --skip-sprockets • on creating a new application 레일스 4%에서는?
  • 7. Cache Busting • MD5 fingerprinting global-908e25f4bf641868d8683022a5b62f54.css a hash of the content
  • 8. CSS and Sass body { background: asset-url('image.jpg'); background: image-url('image.jpg'); background: url(asset-path('image.jpg')); background: url(image-path('image.jpg')); } *.scss on `sass-rails` gem asset-url, in sass => asset_url, in ruby image-url, in sass => image_url in ruby asset-path, in sass => asset_path in ruby image-path, in sass=> image_path in ruby hyphenated in sass, underscored in ruby
  • 9. CSS & ERB using data URI #logo { background: url(<%= asset_data_uri 'logo.png' %>) } a method of embedding the image data directly into the CSS file #logo{background:url(data:image/png;base64,%2F9j %2F4AAQSkZJRgABAQEASABIAADzUQB7i1uNbtRBPry3Xge2E42x6dsEAQBAcH xTC2z8QxVrmj3C6Yp58%2FCycDDHHyc3un0Czur64ZXK %2FQ79JZt0M83ulvfYru6EA%2B7yd%2BBx%2B74eo5fRddVivr6u .... %2B8eY39V5Wo9mzh5qN16fsPNHjdHMXClp60mWncWTMOcA4c0j8QV46nKmfoY ySe6Oz4Iv1VdbfUw151T0bgx0521gjIz%2FMMb)}
  • 10. To add pipelines config.assets.paths << Rails.root.join("lib", "videoplayer", "flash") in config/application.rb lib/videoplayer/flash/abc.css.scss *= require abc (in application.css) lib/videoplayer/flash/abc.js.coffee /= require abc (in application.js)
  • 11. production.rb • config.assets.enabled = true • config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif) • config.assets.compress = true • config.assets.js_compressor = :uglifier • config.assets.css_compressor = :sass • config.serve_static_assets = true • config.assets.digest = true • config.assets.version = '1.0'
  • 12. Controller-specific Assets *.js.coffee and *.css.scss a javascript and a stylesheet a scaffold or a controller Generator <%= javascript_include_tag params[:controller] %> <%= stylesheet_link_tag params[:controller] %>
  • 13. Precompile Assets # Do not fallback to assets pipeline if a precompiled asset is missed. config.assets.compile = false $ RAILS_ENV=production rake assets:precompile in config/environments/production.rb in terminal console on deployment with Capistrano $ cap deploy:assets:precompile
  • 14. Compile Assets # Do not fallback to assets pipeline if a precompiled asset is missed. config.assets.compile = true $ RAILS_ENV=production rake assets:precompile in config/environments/production.rb in terminal console on deployment with Capistrano $ cap deploy:assets:precompile ?
  • 15. `Manifest` File • to make a whitelist of assets included and served • to build a single CSS or javascript file • Directives : ★ `require` ★ `require_tree` (with recursion) ★ `require_directory` (without recursion)
  • 16. Server -e option $ rails s (-e development) $ rails s -e production Environments In development mode In production mode
  • 17. Before Deploy • For the simulation of Production Environment in local machine 1. Check config/production.rb config.serve_static_assets = false true 로컬머신에서 production mode로 웹서버를 실행하면 어플리케 이션 서버(webrick, puma, thin...)가 static assets을 인식 못함. 따라서 레일스가 이를 대신할 수 있도록 true로 지정해 둠.
  • 18. Before Deploy • For the simulation of Production Environment in local machine 2. Precompile assets $ RAILS_ENV=production rake assets:precompile 결과로, public/assets 디렉토리가 생성되고 컴파일된 자원들이 fingerprinting되어 위치하게 됨.
  • 19. Before Deploy • For the simulation of Production Environment in local machine 3. Run local Web Server in production mode $ rails s -e production
  • 20. Before Deploy • For the simulation of Production Environment in local machine 4. Revert config/production.rb to config.serve_static_assets = true false production mode로 시뮬레이션 작업이 끝나고 실제 원격 운영 서버로 배포하기 전에 false 값으로 되돌려 놓음. 이유는,Apache 나 Nginx 웹서버가 컴파일된 파일들을 로드하기 때문.
  • 21. Deploy after Local Precompilation • not have write access to your production file system • deploying to more than one server • doing frequent deploys that do not include asset changes Indications :
  • 22. Deploy after Local Precompilation • not run the Capistrano deployment task that precompiles assets. • change the following two application configuration settings Treatments : config.assets.prefix = "/dev-assets" config.assets.initialize_on_precompile = false config/environments/development.rb config/application.rb
  • 24.