Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix URLs in "middleman serve"
  • Loading branch information
nex3 committed Dec 29, 2018
1 parent e993e6b commit d41c63c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config.rb
@@ -1,5 +1,9 @@
require 'middleman-syntax'

require_relative 'lib/rack/try_dynamic'

use Rack::TryDynamic, try: ['.html', '/index.html']

activate :automatic_image_sizes
activate :autoprefixer do |config|
config.browsers = ['last 2 versions']
Expand Down
17 changes: 17 additions & 0 deletions lib/rack/try_dynamic.rb
@@ -0,0 +1,17 @@
# Like Rack::TryStatic, but tries each possible URL with the app instead.
class Rack::TryDynamic
def initialize(app, options)
@app = app
@try = ['', *options[:try]]
end

def call(env)
orig_path = env['PATH_INFO']
found = nil
@try.each do |path|
resp = @app.call(env.merge!({'PATH_INFO' => orig_path + path}))
break if !(403..405).include?(resp[0]) && found = resp
end
found or @app.call(env.merge!('PATH_INFO' => orig_path))
end
end

0 comments on commit d41c63c

Please sign in to comment.