class Yarrow::Server::ResourceRewriter

Rack Middleware for rewriting extensionless URLs

See surge.sh rewrite rules, that seems like a good starting point for generic behaviour that can be put in place without needing large amounts of config each time.

Public Class Methods

new(app) click to toggle source
# File lib/yarrow/server.rb, line 51
def initialize(app)
  # No options enabled
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/yarrow/server.rb, line 60
def call(env)
  # TODO: document and disambiguate usage of Rack::Request vs env PATH_INFO
  request_path = env["PATH_INFO"]

  try_response = @app.call(env)

  # TODO: reproduces default Netlify behaviour—should be a 301 instead?
  if try_response[0] == 404 and should_try_rewrite(request_path)
    try_response = @app.call(env.merge!({
      "PATH_INFO" => "#{request_path}.html"
    }))
  end

  try_response
end
should_try_rewrite(request_path) click to toggle source
# File lib/yarrow/server.rb, line 56
def should_try_rewrite(request_path)
  !request_path.end_with?(".html") || !request_path.end_with?("/")
end