Links

Changing a route can break links that were assembled by hand. Mountaineer generates typed link functions from controller URLs and render() signatures, so type checking can catch missing or incorrectly typed parameters.

This controller requires an ID and accepts an optional checkout flag:

class DetailController:
    url = "/detail/{detail_id}"

    def render(self, detail_id: int, checking_out: bool = False) -> MyDetailData:
        ...

The generated function requires detail_id, which appears in the URL path. checking_out is an optional query parameter because the Python argument has a default.

Access link functions through serverState.linkGenerator:

const MyHomeRoute = () => {
  const serverState = useServer();

  return (
    <a
      href={serverState.linkGenerator.detailController({
        detail_id: "9280ca4d-e607-486a-a343-7bbaae6b5a86",
      })}
    >
      Detail Link
    </a>
  )
}

TypeScript reports an error if detail_id is missing or its type doesn't match the controller's parameter.

There are two types of dynamic URL parameters:

  • Path Variables: /product/[product_id]
  • Query Variables: /posts?page=[page_num]

Both path variables and query variables are exposed as interface definitions within the link generator.

Query parameters can also be arrays of simple values. Mountaineer will serialize these as repeated key-value pairs in the URL.

class SearchController:
    url = "/search"

    def render(
        self,
        query: str,
        tags: list[str] | None = None,
    ) -> SearchRender:
        ...
const href = serverState.linkGenerator.searchController({
  query: "mountaineer",
  tags: ["docs", "plugins"],
});

// /search?query=mountaineer&tags=docs&tags=plugins