Skip to Content Skip to Search

Action Dispatch FileHandler ↑

This endpoint serves static files from disk using Rack::Files.

URL paths are matched with static files according to expected conventions: path, path.html, path/index.html.

Precompressed versions of these files are checked first. Brotli (.br) and gzip (.gz) files are supported. If path.br exists, this endpoint returns that file with a content-encoding: br header.

If no matching file is found, this endpoint responds 404 Not Found.

Pass the root directory to search for matching files, an optional index: "index" to change the default path/index.html, and optional additional response headers.

Methods
A
C
N

Constants

PRECOMPRESSED = { "br" => ".br", "gzip" => ".gz", "identity" => nil }.freeze
 

Accept-Encoding value -> file extension

Class Public methods

new(root, index: "index", headers: {}, precompressed: %i[ br gzip ], compressible_content_types: /\A(?:text\/|application\/javascript|image\/svg\+xml)/)

# File actionpack/lib/action_dispatch/middleware/static.rb, line 60
def initialize(root, index: "index", headers: {}, precompressed: %i[ br gzip ], compressible_content_types: /\A(?:text\/|application\/javascript|image\/svg\+xml)/)
  @root = root.chomp("/").b
  @index = index

  @precompressed = Array(precompressed).map(&:to_s) | %w[ identity ]
  @compressible_content_types = compressible_content_types

  @file_server = ::Rack::Files.new(@root, headers)
end

Instance Public methods

attempt(env)

# File actionpack/lib/action_dispatch/middleware/static.rb, line 74
def attempt(env)
  request_method = env[::Rack::REQUEST_METHOD]

  if request_method == "GET" || request_method == "HEAD"
    if found = find_file(env[::Rack::PATH_INFO], env)
      serve Rack::Request.new(env), *found
    end
  end
end

call(env)

# File actionpack/lib/action_dispatch/middleware/static.rb, line 70
def call(env)
  attempt(env) || @file_server.call(env)
end