Skip to content

Instantly share code, notes, and snippets.

@robbiejaeger
Last active June 8, 2016 16:34
Show Gist options
  • Select an option

  • Save robbiejaeger/bbc056e4701900cabf6a8514296b5709 to your computer and use it in GitHub Desktop.

Select an option

Save robbiejaeger/bbc056e4701900cabf6a8514296b5709 to your computer and use it in GitHub Desktop.

Asset Pipeline Overview

  • What are assets?
    • They are javascript files, css files, and images
  • Where do assets live? (app/assets, lib/assets, vendor/assets)
    • Assets can live in three different places: app/assets, lib/assets, and vendor/assets
  • Why do we have the asset pipeline?
    • To make the assets faster to use on the server
  • What is Sprokets?
    • Sprokets is a gem that is included in Rails by default (it concatenates, minifies, and then preprocesses JS and CSS files)

Asset Pipeline Scavenger Hunt

Start a gist with these questions:

  • What does it mean to concatenate files? Find an image of an example concatenated file. Why would we want to concatenate files?
    • Concatenating files means taking multiple files of the same type and combining them into one file. For instance, all css files will be put into one big css file. Concatenating files makes using the resource faster...
  • What does it mean to precompile files? What does this have to do with coffeescript and sass files?
    • Precompiling files takes files written in another language and translates the files into the intended base language (coffeescript gets translated to javascript, and sass gets translated into css)
  • What does it mean to minify files? Find an image of an example minified file. Why would we want to minify files?
    • Minifying files takes a cancatenated file and removes all of the whitespace and possible long variable names in javascript. It creates a file that is as small as possible.
  • Start up the server for Catch 'em All (rails s) and navigate to http://localhost:3000/assets/application.js. Then open up the code for application.js in your text editor. Why are these not the same?
  • What is a manifest (in terms of the asset pipeline)? Where can you find two manifests in Catch 'em All?
    • The manifest is the list of files required in the app/assets/javascripts/application.js and the app/assets/stylesheets/application.css files
  • In regular HTML files, we bring in css files with <link rel="stylesheet" href="application.css">. How is this done in a Rails project? Where do you see this line in Catch 'em All?
    • It is done in the application layout view using a helper link method (stylesheet_link_tag and javascript_include_tag)
  • How is a digest/fingerprint used on the assets for caching purposes? +
  • Done? Take a look at RailsGuides: The Asset Pipeline.

Running Production Locally

  • Why would we want to see if something will work in production mode without pushing to Heroku?
    • RAILS_ENV=production rails s
  • What are the development.rb, test.rb, and production.rb files?

Running Production Challenge

Catch 'em All looks fine when running in development mode (rails s). But our challenge now is to get it running locally using the production environment (RAILS_ENV=production rails s) AND for our assets to look the same. Follow the errors (if you can find them...)

  • How can we see the errors? +
  • rake secret
  • export MY_KEY=something123
  • What does this line do? config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
  • What does the 'rails_12factor' gem do?
  • What command can you run to precompile your assets locally? why do you not need to do this on Heroku?
  • Why would you need to run rake assets:clobber?
  • When is there an assets directory inside the public folder?

The steps:

  • Set the secret key base manually (rake secret to get the token, and then set the key base using export)

  • Configure the production environment to serve static assets locally. In config/environments/production.rb, temporally change:

    config.consider_all_requests_local = false to

    config.consider_all_requests_local = true. Set this back to false before pushing to Heroku

  • Remove the old compiled assets files (if any) using clobber (RAILS_ENV=production rake assets:clobber)

  • Precompile the assets (RAILS_ENV=production rake assets:precompile)

  • Add the 'rails_12factor' gem to the gemfile (ONLY IN A PRODUCTION GROUP)

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment