Klenodexample

Docs

Router Plugin

RouterPlugin is an optional, framework-agnostic plugin that discovers filesystem routes and generates a virtual router module.

Configure The Router

Add RouterPlugin to the build context and import its virtual module.

router_plugin = Klenod::Build::Plugins::RouterPlugin.new(
  specifier: "virtual:router",
  pages_dir: "routes"
)

context = Klenod::Build::Context.new(
  source_dir: "src",
  plugins: [
    *Klenod::Build::Context.default_plugins,
    router_plugin
  ]
)

router = context.entry("virtual:router").exports::Default
match = router.match("/blog/hello")

In an application config, either append router_plugin to Context.default_plugins or provide a full ordered plugin list.

Supported Files And Segments

The router discovers +page.rb, +page.haml, +route.rb, +layout.rb, +layout.haml, +error.rb, +error.haml, +not-found.rb, and +not-found.haml.

Supported segment forms include:

  • [id] dynamic segments.
  • [...slug] catch-all segments.
  • [[...slug]] optional catch-all segments.
  • (marketing) route groups.
  • @modal parallel route slots.
  • (.)photo, (..)profile, and (...)login intercepted route segments.

Match Results

A route match exposes the selected modules and structural metadata:

match.page
match.handler
match.layouts
match.slots
match.params
match.route

Generated router modules use lazy_import so matching a route can load only the selected page, handler, layouts, and slots.

When no page route matches, the router returns the closest not-found module for that URL path. Frameworks can use the closest error module when rendering raises.

Framework Boundary

RouterPlugin does not decide request dispatch or rendering policy. Hybrid directories can contain both a page and +route.rb; the router exposes both and the consuming framework decides which one to use for a request.