class Rack::Static
Experimental RBS support (using type sampling data from the type_fusion
project).
# sig/rack/static.rbs class Rack::Static def applicable_rules: (String path) -> untyped end
]
[:fonts, {‘Access-Control-Allow-Origin’ => ‘*’}]
# Firefox requires this when serving assets using a Content Delivery Network
# Provide web fonts with cross-origin access-control-headers
[:all, {‘Cache-Control’ => ‘public, max-age=31536000’}],
# as well as in the browser
# Cache all static files in public caches (e.g. Rack::Cache)
:header_rules => [
use Rack::Static, :root => ‘public’,
Complete example use case including HTTP header rules:
List rather general rules above special ones.
Rules are applied in the order that they are provided.
Rule Ordering:
:fonts => Uses the Regexp rule stated right above to match all common web font endings
Provide the :fonts symbol
5) Font Shortcut
Note: This Regexp is available as a shortcut, using the :fonts rule
the most common web font formats (.eot, .ttf, .otf, .woff2, .woff, .svg)
/.(?:eot|ttf|otf|woff2|woff|svg)z/ => Matches files ending in
%r{.(?:css|js)z} => Matches files ending in .css or .js
Provide a regular expression
4) Regular Expressions / Regexp
[‘css’, ‘js’] or %w(css js) => Matches files ending in .css or .js
Provide the file extensions as an array
3) File Extensions
’/folder’ or ‘/folder/subfolder’ => Matches files in a certain folder
Provide the folder path as a string
2) Folders
:all => Matches every file
Provide the :all symbol
1) All files
Rules for selecting files:
]
[rule, {header_field => content}]
[rule, {header_field => content, header_field => content}],
:header_rules => [
use Rack::Static, :root => ‘public’,
Set custom HTTP Headers for based on rules:
‘index.html’
use Rack::Static, :urls => [“”], :root => ‘public’, :index =>
directory but uses index.html as default route for “/”
Serve all requests normally from the folder “public” in the current
use Rack::Static, :urls => {“/” => ‘index.html’}, :root => ‘public’
current directory (ie public/index.html):
Serve all requests to / with “index.html” from the folder “public” in the
use Rack::Static, :urls => [“/css”, “/images”], :root => “public”
in the current directory (ie public/css/* and public/images/*):
Serve all requests beginning with /css or /images from the folder “public”
use Rack::Static, :urls => [“/media”], :cascade => true
/media, call the next middleware:
Same as previous, but instead of returning 404 for missing files under
use Rack::Static, :urls => [“/media”]
in the current directory (ie media/*):
Serve all requests beginning with /media from the “media” folder located
Examples:
object. This allows a Rack stack to serve both static and dynamic content.
route mappings passed in the options, and serves them using a Rack::Files
(javascript files, images, stylesheets, etc) based on the url prefixes or
The Rack::Static middleware intercepts requests for static files
def add_index_root?(path)
def add_index_root?(path) @index && route_file(path) && path.end_with?('/') end
def applicable_rules(path)
Experimental RBS support (using type sampling data from the type_fusion
project).
def applicable_rules: (String path) -> untyped
This signature was generated using 1 sample from 1 application.
def applicable_rules(path) @header_rules.find_all do |rule, new_headers| case rule when :all true when :fonts /\.(?:ttf|otf|eot|woff2|woff|svg)\z/.match?(path) when String path = ::Rack::Utils.unescape(path) path.start_with?(rule) || path.start_with?('/' + rule) when Array /\.(#{rule.join('|')})\z/.match?(path) when Regexp rule.match?(path) else false end end end
def call(env)
def call(env) path = env[PATH_INFO] if can_serve(path) if overwrite_file_path(path) env[PATH_INFO] = (add_index_root?(path) ? path + @index : @urls[path]) elsif @gzip && env['HTTP_ACCEPT_ENCODING'] && /\bgzip\b/.match?(env['HTTP_ACCEPT_ENCODING']) path = env[PATH_INFO] env[PATH_INFO] += '.gz' response = @file_server.call(env) env[PATH_INFO] = path if response[0] == 404 response = nil elsif response[0] == 304 # Do nothing, leave headers as is else if mime_type = Mime.mime_type(::File.extname(path), 'text/plain') response[1][CONTENT_TYPE] = mime_type end response[1]['Content-Encoding'] = 'gzip' end end path = env[PATH_INFO] response ||= @file_server.call(env) if @cascade && response[0] == 404 return @app.call(env) end headers = response[1] applicable_rules(path).each do |rule, new_headers| new_headers.each { |field, content| headers[field] = content } end response else @app.call(env) end end
def can_serve(path)
def can_serve(path) route_file(path) || overwrite_file_path(path) end
def initialize(app, options = {})
def initialize(app, options = {}) @app = app @urls = options[:urls] || ["/favicon.ico"] @index = options[:index] @gzip = options[:gzip] @cascade = options[:cascade] root = options[:root] || Dir.pwd # HTTP Headers @header_rules = options[:header_rules] || [] # Allow for legacy :cache_control option while prioritizing global header_rules setting @header_rules.unshift([:all, { CACHE_CONTROL => options[:cache_control] }]) if options[:cache_control] @file_server = Rack::Files.new(root) end
def overwrite_file_path(path)
def overwrite_file_path(path) @urls.kind_of?(Hash) && @urls.key?(path) || add_index_root?(path) end
def route_file(path)
def route_file(path) @urls.kind_of?(Array) && @urls.any? { |url| path.index(url) == 0 } end