Pages and Layouts
Learn how to structure static, protected, and paid pages and layouts in your Next.js application.
In the Next.js App Router, page.tsx
files define the unique UI for a route, while layout.tsx
files define UI that is shared across multiple routes within a segment. Layouts are particularly useful for enforcing access control.
Static Page
These pages are publicly accessible and don't require any authentication or special access. They are typically used for landing pages, about pages, etc.
Example:
Static Page Seo
Here is basic overview of static page. The libs/seo
folder helps you set SEO
tags and structure data to better rank on Google. Also add this tatic page to
the app/sitemap.ts
. Make sure to customize SEO tags.
Protected Page / Layout
These routes require the user to be authenticated. You can enforce this either at the layout level (protecting all pages in a segment) or at the individual page level.
Layout Enforces Protection
The layout.tsx
file checks for an authenticated user. If the user is not
logged in, it redirects them (e.g., to the login page defined in
appConfig.auth.login
) before any child page.tsx
components in that segment
are rendered. Child pages within this layout can assume authentication.
Example Layout (app/protected/layout.tsx
):
Paid Page / Layout
These routes require the user to be both authenticated AND have specific access rights (e.g., an active subscription confirmed by the hasAccess
flag). Similar to protected routes, you can enforce this at the layout or page level.
Layout Enforces Auth + Access
The layout first checks for authentication, redirecting to login if necessary.
Then, it checks for the required access flag (user.hasAccess
). If the user
lacks access, it redirects them (e.g., to the billing page defined in
appConfig.lemonsqueezy.billingRoute
) before rendering child components.
Child pages can assume authentication and access.
Example Layout (app/premium-feature/layout.tsx
):