🛣️ File-System Routing

Learn how routes map to files in the src/app directory.

Routes are automatically generated based on the file structure inside the src/app directory. This makes it intuitive to structure your application without having to write complex configuration files.

Basic Routes

  • src/app/page.nim /
  • src/app/docs/page.nim /docs

Dynamic Routes

Crown 0.6 supports legacy p_ segments and Next-style brackets:

  • src/app/blog/p_id.nim /blog/{id:str}
  • src/app/users/[id:int]/page.nim /users/{id:int}
  • src/app/(admin)/... URL から除外(route group)

パラメータは req.param("id", int)req.search(MyInput) で読めます。ルート横の error.nim / loading.nim はそれぞれエラー境界と {path}/__loading フラグメントになります。

Typed Search Params

クエリ文字列は query マクロでコンパイル時に型付けします。欠落時はフィールドのデフォルト、Optionnonechoice(asc, desc) はインライン enum になります。

query ProductsQuery: page: int = 1 sort: choice(asc, desc) = desc filter: Option[string] proc page*(req: Request): string = let q = req.search(ProductsQuery) # q.page は int。URL の抽出と変換はマクロがゼロコスト生成

Typed Links

ルートマニフェストから hrefUsers(id: int) のようなヘルパーが生成されます。クエリ付き URL は href("/products", q) / withQuery です。文字列結合によるリンク切れはコンパイルエラーになります。