Giter VIP home page Giter VIP logo

rails_serve_static_assets's Introduction

Serve Static Assets

Rails gem to enable serving of static assets

Supports:

  • Rails 3
  • Rails 4
  • Rails 5

What

This gem enables serving assets in production, required with Rails4.

Install

In your Gemfile add:

gem 'rails_serve_static_assets'

Then run

$ bundle install

You also need the rails_stdout_logging gem. You can get both of them together by installing rails_12factor gem.

Rails 4 Serve Static Assets

In the default Rails development environment assets are served through a middleware called sprockets. In production however most one off Rails deployments will put their ruby server behind reverse HTTP proxy server such as Nginx which can load balance their sites and can serve static files directly. When Nginx sees a request for an asset such as /assets/rails.png it will grab it from disk at /public/assets/rails.png and serve it. The Rails server will never even see the request.

On a 12factor platform, Nginx is not required to run your application. Your app should be capable of handling requests directly, or through a routing layer that may handles load balancing while you scale out horizontally. The caching behavior of Nginx is not needed if your application is serving static assets through an edge caching CDN.

By default Rails4 will return a 404 if an asset is not handled via an external proxy such as Nginx. While this default behavior may help you debug your Nginx configuration, it makes a default Rails app with assets unusable on a 12factor platform. To fix this we've released a gem rails_serve_static_assets.

This gem, rails_serve_static_assets, enables your Rails server to deliver your assets instead of returning a 404. You can use this to populate an edge cache CDN, or serve files directly from your web app. This gives your app total control and allows you to do things like redirects, or setting headers in your Ruby code. To enable this behavior in your app we only need to set two configuration options through this gem:

config.serve_static_assets = true
config.action_dispatch.x_sendfile_header = nil

Note: this gem will set these values for you, you don't need to change any configuration manually.

All you need to do to get this functionality of both gems is add the rails_12factor gem to your project.

Why Didn't I need this before?

Why do you need to include this gem in Rails 4 and not Rails 3? Rails4 is getting rid of the concept of plugins. Before libraries were easily distributed as Gems and in the form of Engines, Rails had a folder vendor/plugins. Any code you put there would be initialized much like a Gem is today. This was a very simple and easy way to share and use libraries, but it wasn't very maintainable. You could use a library, and make a change locally and then deploy which makes your version incompatible from future versions. Even worse there was no concept of versioning aside from source control, so semantic versioning was out of the question. For these reasons and more Rails3 deprecated plugins. With Rails4 plugins have been removed completely. Why does this affect your app on Heroku?

In the past Heroku has used plugins as a safe way to configure your application where code was needed. While we advocate separating config from code, this was the only option if we wanted your apps to work with no changes from you. With Rails3 Heroku will add the asset serving and standardout logging plugins to your app automatically. With Rails4, Heroku needs you to add these libraries to your Gemfile.

It is important to note that unlike Gems, plugins do not have a dependency resolution phase like what happens when you run bundle install. Heroku does not and will not add anything to your Gemfile on compilation.

The Future

We will be working with Rails and the Rails core team to make future versions of Rails work on Heroku out of the box. Until then you'll need to add this gem to your project.

rails_serve_static_assets's People

Contributors

arthurnn avatar bf4 avatar bscofield avatar chiefgui avatar hone avatar jonatack avatar patrickmcgraw avatar schneems avatar svc-scm avatar wuputah avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rails_serve_static_assets's Issues

serve_static_assets is deprecated in Rails 4.2

Rails.configuration.serve_static_assets has been deprecated in Rails 4.2. This results in the following warning message being printed whenever I deploy to Heroku:

DEPRECATION WARNING: The configuration option `config.serve_static_assets` has been renamed to
`config.serve_static_files` to clarify its role (it merely enables serving everything in
the `public` folder and is unrelated to the asset pipeline). The `serve_static_assets` alias
will be removed in Rails 5.0. Please migrate your configuration files accordingly.

Reduce gem size

The gem weighs in at 25 megabytes, 99.9% of which is the rails app in the test directory. This makes it the biggest gem in my rails app and is a lot of weight to carry around.

I would suggest to remove the test directory from the gem builds. I'm not sure what the current policy regarding including tests inside gems is. The default bundle gem setup does include them, and I think there used to be a way to run tests for rubygems locally, but I'm not sure if people use this at all now that we have travis ci and all, and especially in this case of simple configuration glue, I think the tradeoff is too large.

Deprecation warning

There is a deprecation warning in Rails 4.2.1:

DEPRECATION WARNING: The configuration option config.serve_static_assets has been renamed to config.serve_static_files to clarify its role (it merely enables serving everything in the public folder and is unrelated to the asset pipeline). The serve_static_assets alias will be removed in Rails 5.0. Please migrate your configuration files accordingly. (called from block in tsort_each at /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/tsort.rb:226)

So probably
::Rails.configuration.serve_static_assets = true
should be
::Rails.configuration.serve_static_files = true

Access protection for assets

Using this gem sets config.serve_static_files = true.
Is there a way to add basic Rack Authentication for files (e.g. assets) which reside in the /public directory and are being served by Rails? I need this to provide basic authentication for assets of an Rails 4.2 app running on Heroku.

DEPRECATION WARNING on Rails 4.2.0

DEPRECATION WARNING: The configuration option config.serve_static_assets has been renamed to config.serve_static_files to clarify its role (it merely enables serving everything in the public folder and is unrelated to the asset pipeline). The serve_static_assets alias will be removed in Rails 5.0. Please migrate your configuration files accordingly.

config.serve_static_assets is deprecated and renamed to config.serve_static_files

DEPRECATION WARNING: The configuration option config.serve_static_assets has been renamed to config.serve_static_files to clarify its role (it merely enables serving everything in the public folder and is unrelated to the asset pipeline). The serve_static_assets alias will be removed in Rails 5.0. Please migrate your configuration files accordingly.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.