forked from metaskills/757studio
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c1b096
commit fdbe2c6
Showing
4 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
= Sass on Heroku | ||
|
||
Rails plugin to compile Sass files to tmp/ and serve from there, avoiding trouble with read-only filesystems. | ||
|
||
It will add a Rack middleware on top of your Rails app that quickly detects requests for Sass CSS files, and serve them with caching headers. | ||
|
||
If you have issues please tell us on support.heroku.com -- or just fork it :) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
if RAILS_ENV == 'production' | ||
ActionController::Dispatcher.middleware.use SassOnHeroku | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
class SassOnHeroku | ||
def self.original_css_location | ||
@@original_css_location ||= nil | ||
end | ||
|
||
def self.original_css_location=(location) | ||
@@original_css_location = location | ||
end | ||
|
||
def initialize(app) | ||
@app = app | ||
initialize_sass unless self.class.original_css_location | ||
end | ||
|
||
def initialize_sass | ||
# get the original location of compiled css files (ie: stylesheets/compiled) | ||
# and replace it to a location in tmp | ||
if Sass::Plugin.options[:template_location].is_a?(Hash) | ||
self.class.original_css_location = Sass::Plugin.options[:template_location].values.first | ||
Sass::Plugin.options[:template_location].keys.each do |k| | ||
Sass::Plugin.options[:template_location][k] = css_location_on_heroku | ||
end | ||
else | ||
self.class.original_css_location = Sass::Plugin.options[:css_location] | ||
Sass::Plugin.options[:css_location] = css_location_on_heroku | ||
end | ||
|
||
# compile Sass once, and set it to not compile again | ||
Sass::Plugin.options[:always_update] = true | ||
Sass::Plugin.options[:never_update] = false | ||
Sass::Plugin.update_stylesheets | ||
Sass::Plugin.options[:never_update] = true | ||
end | ||
|
||
def call(env) | ||
if !stylesheets.empty? && env['REQUEST_PATH'] =~ css_request_regexp | ||
return render_sass($1) | ||
end | ||
@app.call(env) | ||
end | ||
|
||
def render_sass(name) | ||
css_file = "#{css_location_on_heroku}/#{name}" | ||
[ | ||
200, | ||
{ | ||
'Cache-Control' => 'public, max-age=86400', | ||
'Content-Length' => File.size(css_file).to_s, | ||
'Content-Type' => 'text/css' | ||
}, | ||
File.read(css_file) | ||
] | ||
end | ||
|
||
def css_location_on_heroku | ||
"#{RAILS_ROOT}/tmp/sass-output" | ||
end | ||
|
||
def stylesheets | ||
@stylesheets ||= Dir[css_location_on_heroku + '/*.css'].map { |f| f.split('/').last } | ||
end | ||
|
||
def css_request_regexp | ||
@css_request_regexp ||= build_regexp | ||
end | ||
|
||
# builds a regexp that matches requests for Sass stylesheets | ||
# ie : \/stylesheets\/sass\/(file1\.css|file2\.css) | ||
def build_regexp | ||
files = stylesheets.map { |f| Regexp.escape(f) } | ||
path = Regexp.escape(self.class.original_css_location.gsub("#{RAILS_ROOT}/public", '') + '/') | ||
regexp = Regexp.new("^#{path}(" + files.join('|') + ')(\?.*)?$') | ||
end | ||
end |