How I structure a Next.js application
Most of my Next.js work follows the same shape, regardless of where it’s deployed:
- Route groups per audience. Public, authenticated and admin areas live in separate route groups with their own layouts, so auth checks and navigation don’t leak between them. The creator commerce storefront, for example, separates
(user)and(admin)route groups. - Server Components by default. Pages and layouts fetch data on the server. Client Components are reserved for things that need browser state: forms, filters, carts, animation.
- A typed data layer, not fetch calls in components. Data access goes through a small module (or TanStack Query hooks on the client), so components never know whether data came from GraphQL, REST or a cache.
- Validation at the edges. Form input and API responses are parsed with Zod before they reach business logic.
Rendering strategy, per route
The App Router lets each route choose how it renders. I decide per route, not per project:
| Route type | Strategy | Why |
|---|---|---|
| Marketing, docs, case studies | Static (build time) | Same HTML for everyone; cheapest and fastest to serve |
| Catalogue and listing pages | Static with periodic revalidation | Content changes, but not per request |
| Dashboards, carts, account pages | Request-time rendering or client fetching | Depends on the signed-in user |
Illustrative example: metadata per route
A pattern I use so every data-driven page has unique, explicit metadata. This is a simplified illustration, not code from a client project.
// app/products/[slug]/page.tsx
import type { Metadata } from "next";
import { notFound } from "next/navigation";
import { getProduct } from "@/lib/catalog";
export async function generateMetadata({ params }: { params: Promise<{ slug: string }> }): Promise<Metadata> {
const product = await getProduct((await params).slug);
if (!product) return {};
return {
title: product.name,
description: product.summary,
alternates: { canonical: `/products/${product.slug}` },
openGraph: { images: [product.image] },
};
}
export default async function ProductPage({ params }: { params: Promise<{ slug: string }> }) {
const product = await getProduct((await params).slug);
if (!product) notFound(); // real 404 status, not an empty page
return <ProductView product={product} />;
}
Deploying to Cloudflare Workers
Running Next.js on Workers via OpenNext puts the application on Cloudflare’s network next to the APIs it calls, which on Ekonavi are also Workers. The trade-off is that you are a step removed from the platform Next.js is primarily developed against, so I keep an end-to-end test suite running against the deployed build rather than trusting local next dev behaviour.