Skip to content
next.js questions

Next.js · 1 of 10junior~1 min

What are the special files of the App Router and how do they nest?

#routing#core

App Router file conventions

In the App Router folders define routes and special files inside a folder define the UI of that segment.

FilePurpose
page.tsxThe UI of the route; without it the folder is not publicly accessible
layout.tsxShared UI that wraps child segments and keeps its state between navigations
template.tsxLike a layout, but remounts on every navigation
loading.tsxInstant loading state, wraps the page in <Suspense>
error.tsxError boundary for the segment (must be a Client Component)
not-found.tsxUI for notFound() and unmatched URLs
route.tsAn API endpoint (Route Handler) instead of a page

How they nest

tsx
<Layout> <Template> <ErrorBoundary fallback={<Error />}> <Suspense fallback={<Loading />}> <ErrorBoundary fallback={<NotFound />}> <Page /> </ErrorBoundary> </Suspense> </ErrorBoundary> </Template> </Layout>

Child segments are rendered inside the parent layout, so layouts nest: app/layout.tsx -> app/blog/layout.tsx -> app/blog/[slug]/page.tsx.

Note: error.tsx does not catch errors of the layout.tsx in the same folder, because the boundary sits inside that layout. Errors in the root layout are handled by global-error.tsx.

updated

did you know it?

read more on the blog

view all

related questions