Last active
July 5, 2024 09:36
-
-
Save WattsInABox/8620783 to your computer and use it in GitHub Desktop.
Generate Static HTML Website Using Ruby on Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ignore static version of the site (used to upload error pages to S3 for Heroku errors) | |
/out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/database.yml | |
development: &development | |
adapter: postgresql | |
database: workherder_development | |
host: localhost | |
username: postgres | |
password: | |
static: | |
<<: *development |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
group :development do | |
gem 'heel' # used to server the static site from the /out directory | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/tasks/generate_static_error_pages.rb | |
# Original idea from http://blog.atlashost.eu/post/ruby-on-rails-a-static-site-generator.html | |
namespace :static do | |
desc 'Generate static site in ./out/ directory' | |
task :generate => [ | |
'assets:clean', | |
'assets:precompile', | |
:start_rails_server | |
] do | |
Dir.mkdir 'out' unless File.exist? 'out' | |
Dir.chdir 'out' do | |
`wget -mnH http://localhost:3000/` | |
end | |
`rsync -ruv --exclude=.svn/ public/ out/` | |
# stop the server when we're done | |
Rake::Task['static:stop_rails_server'].reenable | |
Rake::Task['static:stop_rails_server'].invoke | |
# don't need to keep assets since we deploy to Heroku | |
Rake::Task['assets:clobber'].reenable | |
Rake::Task['assets:clobber'].invoke | |
end | |
desc 'only generates the static error page we need for Heroku' | |
task :generate_error_page => [ | |
'assets:clean', | |
'assets:precompile', | |
:start_rails_server | |
] do | |
Dir.mkdir 'out_error' unless File.exist? 'out_error' | |
Dir.chdir 'out_error' do | |
`wget -mnH http://localhost:3000/exceptions/400.html` | |
end | |
`rsync -ruv --exclude=.svn/ public/ out_error/` | |
# stop the server when we're done | |
Rake::Task['static:stop_rails_server'].reenable | |
Rake::Task['static:stop_rails_server'].invoke | |
# don't need to keep assets since we deploy to Heroku | |
Rake::Task['assets:clobber'].reenable | |
Rake::Task['assets:clobber'].invoke | |
end | |
desc 'Start a Rails server in the static Rails.env on port 3000' | |
task :start_rails_server do | |
`RAILS_ENV=static rails s -p 3000 -d` | |
end | |
desc 'Stop Rails server' | |
task :stop_rails_server do | |
`cat tmp/pids/server.pid | xargs -I {} kill {}` | |
end | |
desc 'Start a server using heel on port 8000 that will server our static site' | |
task :start_static_server, :directory do |t, args| | |
directory = args[:directory] || 'out' | |
Dir.chdir directory do | |
`heel --daemonize --port 8000` | |
end | |
end | |
desc 'Stop the static server that is running using heel' | |
task :stop_static_server do | |
`heel --kill --port 8000` | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/environments/static.rb | |
require File.expand_path('../development', __FILE__) | |
# environment for serving static pages like error pages to upload to S3 | |
Workherder::Application.configure do | |
config.serve_static_assets = true | |
# Compress JavaScripts and CSS | |
config.assets.compress = true | |
# Don't fallback to assets pipeline if a precompiled asset is missed | |
config.assets.compile = false | |
# Generate digests for assets URLs | |
config.assets.digest = true | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment