class StaticRails::StaticMiddleware
Constants
- PROBABLY_SHOULDNT_CACHE_MIME_TYPES
Public Class Methods
new(app)
click to toggle source
# File lib/static-rails/static_middleware.rb, line 8 def initialize(app) @matches_request_to_static_site = MatchesRequestToStaticSite.new @app = app @file_handlers = {} end
Public Instance Methods
call(env)
click to toggle source
# File lib/static-rails/static_middleware.rb, line 14 def call(env) return @app.call(env) unless StaticRails.config.serve_compiled_assets req = Rack::Request.new env if (req.get? || req.head?) && (site = @matches_request_to_static_site.call(req)) file_handler = file_handler_for(site) path = req.path_info.gsub(/^#{site.url_root_path}/, "").chomp("/") if (result = serve_file_for(file_handler, site, path, req)) return result end end @app.call(req.env) end
Private Instance Methods
file_handler_for(site)
click to toggle source
The same file handler used by Rails when serving up files from /public
See: actionpack/lib/action_dispatch/middleware/static.rb
# File lib/static-rails/static_middleware.rb, line 33 def file_handler_for(site) @file_handlers[site] ||= FileHandler.new( StaticRails.config.app.root.join(site.compile_dir).to_s, headers: { "cache-control" => "public; max-age=31536000" }, compressible_content_types: /^text\/|[\/+](javascript|json|text|xml|css|yaml)$/i ) end
override_cache_control_if_we_probably_shouldnt_cache!(file, headers = {})
click to toggle source
# File lib/static-rails/static_middleware.rb, line 63 def override_cache_control_if_we_probably_shouldnt_cache!(file, headers = {}) mime_type = file[1]["Content-Type"] if PROBABLY_SHOULDNT_CACHE_MIME_TYPES.include?(mime_type) headers["cache-control"] = "no-cache, no-store" end end
serve_file(file_handler, file, req, status_override = nil)
click to toggle source
# File lib/static-rails/static_middleware.rb, line 52 def serve_file(file_handler, file, req, status_override = nil) return unless file file_handler.serve(req, *file).tap do |result| result[0] = status_override unless status_override.nil? override_cache_control_if_we_probably_shouldnt_cache!(file, result[1]) end end
serve_file_for(file_handler, site, path, req)
click to toggle source
# File lib/static-rails/static_middleware.rb, line 43 def serve_file_for(file_handler, site, path, req) if (found = file_handler.find_file(path, accept_encoding: req.accept_encoding)) serve_file(file_handler, found, req) elsif site.compile_404_file_path.present? found = file_handler.find_file(site.compile_404_file_path, accept_encoding: req.accept_encoding) serve_file(file_handler, found, req, 404) end end