12

I am building a static public/404.html page. Before Rails 3.1 I can reference it with the public/style.css. But now with asset pipeline I am not sure what to link to. I heard that the styles will be compiled into asset/application.css. But on production it will come with a timestamp.

What is the best approach to style the static 404.html with the normal styles I work with?

lulalala
  • 17,572
  • 15
  • 110
  • 169

2 Answers2

16

You can precompile static error pages with asset pipeline too!

Inside application.rb:

config.assets.paths << "#{Rails.root}/app/assets/html"
config.assets.precompile += %w(404.html 500.html)

Create in assets/html/ files 404.html.erb and 500.html.erb and use many helpers there, like stylesheet_link_tag, javascript_include_tag, image_tag.

Then setup your server to use precompiled public/assets/404.html and public/assets/500.html

All credits for this clever solution goes to http://neovintage.blogspot.cz/2012/02/precompile-static-html-pages-with-rails.html

Petr
  • 3,214
  • 18
  • 21
  • 2
    In Rails 4, this is also necessary: http://stackoverflow.com/questions/14284278/how-to-include-actionview-helpers-in-the-assets-pipeline – Petr Aug 12 '13 at 15:18
  • 1
    Also they MUST be erb files, not HAML http://icelab.com.au/articles/precompiled-rails-static-404-and-500-pages/ – xxjjnn Oct 14 '13 at 14:42
  • 1
    There is another nice solution for HAML error pages: http://devoh.com/blog/2012/09/asset-pipeline-error-pages – Darkside Nov 02 '13 at 18:23
6

It's true that the assets in 3.1 come with a digest in production, but you can still use the regular file, meaning that you can link to /assets/application.css and you won't have any problems (try it! :)).

Federico Builes
  • 4,939
  • 4
  • 34
  • 48
  • Thanks! I guess this means I can't check those in development environment? As in dev css files are not merged. – lulalala Dec 23 '11 at 06:39
  • You should be able to do it too. If you call `/assets/application.css` you'll see all the concatenated code. If you call it with the `?body=1` option you'll only see what's in that specific file. – Federico Builes Dec 23 '11 at 19:36
  • 4
    ...and if you're using far-future expiration headers then doesn't this solution leave you pretty screwed? What if you update your application.css and the users already have that file cached. They'll not get your updated styles. – jsharpe Feb 08 '12 at 04:36
  • Since Rails 4.0, there are no assets generated without digest. – Petr Jun 02 '14 at 13:24
  • Since Rails 4 will add a digest hash to assets, you can add a task to your deployment process that looks like this: `cp #{release_path}/public/assets/500-*.html #{release_path}/public/500.html` – Etienne Aug 22 '14 at 23:13