-
Notifications
You must be signed in to change notification settings - Fork 256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix css loading and add logging #140
Changes from all commits
356b865
a35bdd1
b5ad0b2
50bc56f
11c9769
2a06134
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,16 +3,21 @@ module Rails | |
module CSSHelper | ||
extend self | ||
|
||
@cache = {} | ||
attr :cache | ||
|
||
STRATEGIES = [ | ||
CSSLoaders::CacheLoader, | ||
CSSLoaders::FileSystemLoader, | ||
CSSLoaders::AssetPipelineLoader, | ||
CSSLoaders::NetworkLoader | ||
CSSLoaders::NetworkLoader, | ||
] | ||
|
||
def reset_cache! | ||
@cache = {} | ||
end | ||
|
||
def cache | ||
reset_cache! if @cache.nil? | ||
@cache | ||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cache was spread through two different places, the old |
||
# Returns all linked CSS files concatenated as string. | ||
def css_for_doc(doc) | ||
urls = css_urls_in_doc(doc) | ||
|
@@ -21,18 +26,40 @@ def css_for_doc(doc) | |
|
||
private | ||
|
||
def lookup_cached(url) | ||
return yield if rails_dev_env? | ||
|
||
if !cache.has_key?(url) | ||
cache[url] = yield(url) | ||
end | ||
|
||
cache[url] | ||
end | ||
|
||
def rails_dev_env? | ||
return false | ||
defined?(::Rails) && ::Rails.env.development? | ||
end | ||
|
||
def css_urls_in_doc(doc) | ||
doc.search('link[@rel="stylesheet"]').map do |link| | ||
link.attributes['href'].to_s | ||
end | ||
end | ||
|
||
def load_css(url) | ||
@cache[url] = | ||
lookup_cached(url) do |url| | ||
css = nil | ||
|
||
STRATEGIES.each do |strategy| | ||
css = strategy.load(url) | ||
break css if css | ||
if css = strategy.load(url) | ||
::Rails.logger.debug "premailer-rails: loaded asset #{url} using #{strategy}" if defined?(::Rails) | ||
break | ||
end | ||
end | ||
|
||
css | ||
end | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
require 'uri' | ||
|
||
require 'premailer/rails/css_loaders/cache_loader' | ||
require 'premailer/rails/css_loaders/file_system_loader' | ||
require 'premailer/rails/css_loaders/asset_pipeline_loader' | ||
require 'premailer/rails/css_loaders/network_loader' |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,15 @@ module FileSystemLoader | |
|
||
def load(url) | ||
path = URI(url).path | ||
file_path = "public#{path}" | ||
File.read(file_path) if File.exist?(file_path) | ||
|
||
# Remove leading slash if it exists | ||
path = path[1..-1] if path[0,1] == "/" | ||
|
||
file_path = ::Rails.root.join("public/#{path}").to_s | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The path needs to be relative to Rails root and not the current working directory. This wasn't working on some environments where the process is started outside of the app folder. |
||
|
||
if File.exist?(file_path) | ||
File.read(file_path) | ||
end | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ def initialize(html) | |
Premailer.send(:include, Adapter.find(Adapter.use)) | ||
doc = load_html(html) | ||
|
||
options = @options.merge(css_string: CSSHelper.css_for_doc(doc)) | ||
options = @options.merge(include_link_tags: false, css_string: CSSHelper.css_for_doc(doc)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are manually loading and embedding the css, we need to make sure premailer skips the link tags, otherwise it will load the assets through the network redundantly. |
||
super(html, options) | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comma left.