Skip to content
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

Only intercept preflight requests for fonts, and bonus insert_target configuration option #18

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/font_assets/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ def call(env)
@ssl_request = Rack::Request.new(env).scheme == "https"
# intercept the "preflight" request
if env["REQUEST_METHOD"] == "OPTIONS"
return [200, access_control_headers, []]
if ext = extension(env["PATH_INFO"]) and font_asset?(ext)
return [200, access_control_headers, []]
else
return @app.call(env)
end
else
code, headers, body = @app.call(env)
set_headers! headers, body, env["PATH_INFO"]
Expand Down Expand Up @@ -59,6 +63,8 @@ def allow_ssl?

def extension(path)
"." + path.split("?").first.split(".").last
rescue
"."
end

def font_asset?(path)
Expand Down
7 changes: 5 additions & 2 deletions lib/font_assets/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ class Railtie < Rails::Railtie
config.font_assets.origin ||= "*"
config.font_assets.options ||= { allow_ssl: true }

insert_target = if defined?(ActionDispatch::Static)
config.font_assets.insert_target ||= if defined?(ActionDispatch::Static)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defined?(ActionDispatch::Static) never returns false, even if serve_static_assets is false. This should probably check for its presence in app.middleware instead.

'ActionDispatch::Static'
else
'Rack::Runtime'
end

app.middleware.insert_before insert_target, FontAssets::Middleware, config.font_assets.origin, config.font_assets.options
app.middleware.insert_before config.font_assets.insert_target,
FontAssets::Middleware,
config.font_assets.origin,
config.font_assets.options
end
end
end