Skip to main content

Router API

lua_spa.router.Router

Resolves URL paths to cascaded component trees.

Class diagram

Router.from_config(config)

Create a Router from a config dict (same shape as the "router" key in spa.config.json):

from lua_spa.router import Router

router = Router.from_config({
"routes": [
{"path": "/", "component": "Home"},
{"path": "/about", "component": "About"},
{"path": "/users/:id", "component": "UserDetail"},
]
})

Router.resolve(path) → RouteMatch

Match a URL path against the route tree.

match = router.resolve("/users/42")
# match.path == "/users/42"
# match.params == {"id": "42"}
# match.components == [RouteComponent(name="UserDetail", props={})]

Raises: KeyError if no route matches the path.

Router.render(match) → str

Render a RouteMatch into cascaded HTML component tags:

html = router.render(match)
# "<UserDetail id=\"42\" />"

For nested routes, produces a stack:

# Route: /dashboard/settings
# "<Dashboard><Settings /></Dashboard>"

RouteNode fields

FieldTypeDefaultDescription
pathstr | NoneNoneURL segment (supports :param and * wildcard)
componentstr | NoneNoneComponent name to render
propsdict{}Static props passed to component
childrenlist[RouteNode][]Nested route definitions
indexboolFalseIndex route (matches parent path exactly)
guardstr | NoneNoneGuard name for future middleware
metadict{}Arbitrary metadata

Path matching rules

  • /users/:id matches /users/42params = {"id": "42"}
  • * matches any remaining segments
  • Static segments must match exactly (case-insensitive)