module Roda::RodaPlugins::StaticRouting

  1. lib/roda/plugins/static_routing.rb

The static_routing plugin adds static_* routing class methods for handling static routes (i.e. routes with static paths, no placeholders). These routes are processed before the normal routing tree and designed for maximum performance. This can be substantially faster than Roda's normal tree based routing if you have large numbers of static routes, about 3-4x for 100-10000 static routes. Example:

plugin :static_routing

static_route '/foo' do |r|
  @var = :foo

  r.get do
    'Not actually reached'
  end

  r.post{'static POST /#{@var}'}
end

static_get '/foo' do |r|
  'static GET /foo'
end

route do |r|
  'Not a static route'
end

A few things to note in the above example. First, unlike most other routing methods in Roda, these take the full path of the request, and only match if r.path_info matches exactly. This is why you need to include the leading slash in the path argument.

Second, the static_* routing methods only take a single string argument for the path, they do not acccept other options, and do not handle placeholders in strings. For any routes needing placeholders, you should use Roda's routing tree.

There are separate static_* methods for each type of request method, and these request method specific routes are tried first. There is also a static_route method that will match regardless of the request method, if there is no matching request methods specific route. This is why the static_get method call takes precedence over the static_route method call for /foo. As shown above, you can use Roda's routing tree methods inside the static_route block to have shared behavior for different request methods, while still having handling the request methods differently.

Note that if you want to use the static_routing plugin and the hooks plugin at the same time, you should load the hooks plugin first.

Methods

Public Class

  1. configure

Public Class methods

configure (app)
[show source]
# File lib/roda/plugins/static_routing.rb, line 54
def self.configure(app)
  app.opts[:static_routes] = {}
end