class Rack::TryStatic

The Rack::TryStatic middleware delegates requests to Rack::Static middleware trying to match a static file

Examples

use Rack::TryStatic,

:root => "public",  # static files root dir
:urls => %w[/],     # match all requests
:try => ['.html', 'index.html', '/index.html'] # try these postfixes sequentially

uses same options as Rack::Static with extra :try option which is an array
of postfixes to find desired file

Public Class Methods

new(app, options) click to toggle source
   # File lib/rack/contrib/try_static.rb
20 def initialize(app, options)
21   @app = app
22   @try = ['', *options[:try]]
23   @static = ::Rack::Static.new(
24     lambda { |_| [404, {}, []] },
25     options)
26 end

Public Instance Methods

call(env) click to toggle source
   # File lib/rack/contrib/try_static.rb
28 def call(env)
29   orig_path = env['PATH_INFO']
30   found = nil
31   @try.each do |path|
32     resp = @static.call(env.merge!({'PATH_INFO' => orig_path + path}))
33     break if !(403..405).include?(resp[0]) && found = resp
34   end
35   found or @app.call(env.merge!('PATH_INFO' => orig_path))
36 end