Pages and data
Routing
How folders under app/ become URLs
URLs come from folders
Every folder below app/ is one segment of the URL. app/ itself is /.
| Folder name | URL segment |
|---|---|
orders |
the literal orders |
order+ |
the parameter {order}, read with c.Param("order") |
path++ |
the rest of the path {path...}, only as the last folder. It may be empty: files/path++ also answers /files. c.Param("path") reads it in Go, and its key in TypeScript Params is _splat |
- A literal folder name holds only ASCII letters, digits and
- . _ ~. A parameter name starts with a letter or_, followed by letters, digits or_, and appears once per URL. - The go command takes the package name from the
packageline, not from the folder. Name the package after the parameter:project+holdspackage project, and other packages import it asproject. - The go command skips folders that start with
.or_, and folders namedtestdata. Aroute.go,page.tsx,layout.tsxorlayout_.tsxinside one fails the build. Other files there, such as a_componentsfolder, are fine. - A folder name must also be valid in a Go import path, so names such as
conorx~1(Windows reserved and short names) fail the build. - A folder holds at most one parameter folder.
items/id+next toitems/key+oritems/path++fails the build, because both would read the same URL segment. - Two routes (pages or API routes) whose URLs overlap without one being more specific fail the build, as in Go's
http.ServeMux, such asa/b+next toa+/b. A literal is more specific than a parameter, soitems/newnext toitems/id+is fine. - The URL of a folder with a Layout but no page shows a 404 inside that Layout (see 404), unless a
name++folder right below it has a page, which answers that URL with an empty rest. No other page may match that URL unless it is more specific than the Layout's path:settings/layout.tsxwithout asettings/page.tsx, next touser+/page.tsx, fails the build, because/settingswould also match/{user}, butitems/newnext to a Layout initems/id+is fine.
Parameter names in Go and TypeScript
A parameter has one name in the folder and a spelling in each language. For app/projects/project+ and app/files/path++:
| Folder | Go: c.Pattern() and tifa routes |
TypeScript: Link to |
TypeScript: key of params |
|---|---|---|---|
project+ |
/projects/{project} |
/projects/$project |
project |
path++ |
/files/{path...} |
/files/$ |
_splat |
c.Param("project") and c.Param("path") read them in Go by the name of the folder.
The files of a folder
A folder has at most three convention files:
| File | What it holds |
|---|---|
route.go |
Every Go name of the folder: Layout, Load, SSR, Memo, Cache and the method handlers |
page.tsx |
The page component. A folder with page.tsx is a page |
layout.tsx |
The Layout component |
layout_.tsx |
The Layout component in place of layout.tsx, shown without the Layouts above it (see A Layout without the Layouts above) |
- The names of
route.goare read from that file only. Declaring one of them in another file of the package,layout.goincluded, fails the build. Helpers and types may go in any file. - A folder with only
route.gois an API route when it declares a method handler, and gives the pages below it their Layout data when it declaresLayout. It can do both.Loadis only allowed next to apage.tsx. tifa buildalso writes aroute.gen.tsmodule for each folder with a page, a Layout or a method handler, whichpage.tsx,layout.tsxandlayout_.tsximport (see page.tsx).