Every folder under app/ is a route segment, mapped to one part of the URL. [id] makes a segment dynamic; (group) and @slot folders add no URL part; _folder opts a folder out of routing. A segment is reachable only once it holds a page.js (UI) or route.js (a handler). Anything else can be colocated safely: a component file next to a page isn't served.
Inside one segment the special files are rendered in a fixed order, outermost first:
layout.jstemplate.jserror.js, a React error boundaryloading.js, a<Suspense>boundarynot-found.js, a boundary fornotFound()page.js, or the next segment's layout
The tree is built recursively: a child segment's whole stack is rendered where the parent's page would be, so it sits inside the parent's layout, template and boundaries. A layout receives the child stack as children, plus params and, with parallel routes, one prop per slot.
Two consequences come straight from the order:
- A segment's
error.jsandloading.jssit below its ownlayout.js. They can't catch an error thrown by that layout or show a fallback while it loads; the parent segment's files do that, and the root layout needsglobal-error.js. - The parent's boundaries do wrap the child segment's layout, so a parent
loading.jscovers a slow child layout.
Layouts and pages are Server Components by default; error.js must be a Client Component. The page is the only part that changes on every navigation within the segment: layouts persist and don't re-render, which is why they get no searchParams.