rails 3.1 maintenance page assets
How do I access assets on my maintenance page in rails 3.1 with the asset pipeline enabled?
With the asset pipeline enalbed all assets have a hash in their filename. However, maintenance pages are vanilla HTML, and rails/passenger is being bypassed by the apache config, so there's no way to generate the right asset paths.
I nee开发者_JAVA百科d my application.css and an image file. It's fine if they're the old ones.
I can think of a few kludges, but they're all lame:
- On each deploy, symlink the assets I need to a generic name that I use in the maintenance file.
- Make my maintenance page dynamic, generate it, and dump the markup somewhere - then modify my maintennce 'deploy' script.
If you want to avoid a symlink or a dynamic (erb) page, then use a static template and modify this during deployment.
First create a maintenance page template.
During deployment read the mainfest.yml file created by the precompile process.
Read in the template of the maint page.
Replace any files named in the template with the hashed version from the manifest.
Write out the altered template to the file-system.
For anyone's convenience, I adopted Richard's solution in my project and created a simple ruby script that replaces asset links in the static HTML error / maintenance pages. It is deliberatery NOT a rake task so that it is as fast as possible. It has no Rails depenency anyway, other than that it has to be run from the rails root directory.
#!/usr/bin/env ruby
require 'yaml'
GLOBS = %w(public/errors_source/*.html)
MANIFEST = "public/assets/manifest.yml"
manifest = YAML::load(File.open(MANIFEST))
GLOBS.each do |glob|
Dir.glob(glob).each do |file|
next unless File.file?(file)
contents = File.read(file)
manifest.each do |asset, compiled_asset|
contents.gsub!(asset, "/assets/#{compiled_asset}")
end
File.open(file.gsub('errors_source/',''), 'w') do |outfile|
outfile.write(contents)
end
end
end
The script expects the static HTML error/maintenance pages to live under the errors_source
directory and copies them (with assets replaced for their hashed versions) to the rails root directory.
A sample maintenance page then may look like this (notice the CSS asset link and logo image - these assets are simply shared with the main rails code):
<html>
<head>
...
<link href="application.css" media="screen" rel="stylesheet" type="text/css"/>
</head>
<body>
...
<a href="/"><img src="logo.png" width="161" height="61"/></a>
...
</body>
</html>
If you are on Heroku, there's an add-on called Trackman. You can link all your assets and it deploys your pages and assets on S3. You practically have nothing to code. You can use the utilities within the gem to also make the dev a piece of cake.
http://www.trackman-addon.com
精彩评论