Skip to content

Instantly share code, notes, and snippets.

@hughevans
Created May 7, 2012 08:39
Show Gist options
  • Save hughevans/2626687 to your computer and use it in GitHub Desktop.
Save hughevans/2626687 to your computer and use it in GitHub Desktop.
A rake task to bake out a 404.html and 500.html into your public directory when doing a `rake assets:precompile`.
<!-- this goes in app/assets/html/404.html.erb -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<%= stylesheet_link_tag 'application', :media => 'all' %>
<%= javascript_include_tag 'modernizr' %>
</head>
<body>
<section id="error">
<h1>Page not found</h1>
<a href="/">&larr; Return to Homepage</a>
</section>
<%= javascript_include_tag 'application' %>
</body>
</html>
require 'fileutils'
Rake::Task["assets:precompile"].enhance do
Rake::Task["assets:precompile_static_html"].invoke
end
namespace :assets do
desc 'Compile the static 404 and 500 html template with the asset paths.'
task :precompile_static_html do
invoke_or_reboot_rake_task 'assets:precompile_static_html:all'
end
namespace :precompile_static_html do
def internal_precompile_static_html
# Ensure that action view is loaded and the appropriate
# sprockets hooks get executed
_ = ActionView::Base
config = Rails.application.config
config.assets.compile = true
config.assets.digest = true
env = Rails.application.assets
target = Rails.public_path
compiler = Sprockets::StaticCompiler.new(
env,
target,
['404.html', '500.html'],
:manifest_path => config.assets.manifest,
:digest => false,
:manifest => false
)
compiler.compile
end
task :all do
ruby_rake_task('assets:precompile_static_html:primary', false)
end
task :primary => ['assets:environment', 'tmp:cache:clear'] do
internal_precompile_static_html
end
end
end
@hughevans
Copy link
Author

The only thing that slightly irks me still is that because of their location in app/html the two templates get manifested and compiled into the normal app/assets pipeline.

Not sure how to solve that.

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