File-Based Routing
URLs come from the files and folders in pb_pages/.
Pages
| File | URL |
|---|---|
index.ets |
/ |
about.ets |
/about |
docs/index.md |
/docs (and /docs/) |
docs/routing.md |
/docs/routing |
products/[id].ets |
/products/123, with ctx.params.id = "123" |
[org]/[repo]/index.ets |
/acme/web, with ctx.params = { org: "acme", repo: "web" } |
- The extension is dropped.
indexmaps to its folder. [name]matches exactly one non-empty segment. A parameter must be the whole segment:post-[id].etsis an error at startup.- Segment counts must match:
products/[id].etsdoesn't match/products/123/reviews. - A URL with a trailing slash matches the same route as without it. Pages never redirect to add or remove one.
- Matching is case-sensitive.
See Accessing Parameters.
Groups
A folder named (name) doesn't add a URL segment. Use groups to give part of the site its own layout or middleware:
pb_pages/
(site)/
+layout.ets # wraps everything in (site)
index.ets # /
about.ets # /about
xapi/
count.ets # /xapi/count, outside the (site) layout
Code: .ts files
A .ts file runs, the way a .php file does. Its path is its URL, as for a page:
| File | What it is |
|---|---|
about.ts beside about.ets |
that page's code: load feeds the template, a post handles its form. See Loading data. |
feeds/rss.ts |
an endpoint at /feeds/rss, with no template: its handlers send the whole response (JSON, a feed, a redirect). See Endpoints and JSON. |
feeds/index.ts, products/[id].ts |
the folder's URL, a [param] route, as for pages |
Only .ts runs. A .js file is a static file like any other, so browser scripts can sit anywhere in pb_pages. .d.ts files are neither served nor run.
Static files
Every other file is served as-is at its path, with groups removed: assets/app.css serves /assets/app.css. See Static content.
Which route wins
/api/...and/_/...belong to PocketBase. Pages never see them.- Among matching routes the most specific wins: segments are compared from the left, and a literal segment beats a
[param]. Soproducts/new.etsbeatsproducts/[id].etsfor/products/new. - Two files can't claim the same URL: a page and a static file, or an endpoint and a page (
feed.tsandfeed/index.ets). A.tsbeside a template of the same name is that page's code, not a second claim. These are startup errors. - A URL nothing in
pb_pagesmatches goes on topb_public, then to PocketBase.
When routes change
The route table is built when the app starts and rebuilt whenever anything in pb_pages changes. A rebuild never restarts the app or reloads pb_hooks. Startup errors (bad names, conflicts) are logged with the file path, and the previous route table keeps serving.
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.