Class: Yarrow::Server::ResourceRewriter

Inherits:
Object
  • Object
show all
Defined in:
lib/yarrow/server.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ResourceRewriter

Returns a new instance of ResourceRewriter.



51
52
53
54
# File 'lib/yarrow/server.rb', line 51

def initialize(app)
  # No options enabled
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object



56
57
58
# File 'lib/yarrow/server.rb', line 56

def should_try_rewrite(request_path)
  !request_path.end_with?(".html") || !request_path.end_with?("/")
end