Skip to main content

Next.js β€” NHS Quickstart

βš›οΈ React Β· SSR/SSG/ISR Β· Route Handlers (API) Β· Auth Β· Edge/Node runtimes
Why Next.js for the NHS

When you need a fast, secure UI with server-side rendering (SSR) and built-in API routes, Next.js keeps everything in one codebase. You can fetch from internal APIs (e.g., FastAPI) server-side so secrets never reach the browser.

Great for: Developer Β· Data Engineer (ops tools) Β· IG‑safe intranet apps.


βš™οΈ 10-minute install​

npx create-next-app@latest nhs-next --ts --eslint
cd nhs-next
npm run dev

You now have the App Router (/app) structure.


πŸš€ β€œHello NHS” β€” pages + API route​

Create a route handler that returns a small JSON payload. In production, call your internal FastAPI service or read from Parquet.

app/api/kpi/route.ts
import { NextResponse } from 'next/server'

export async function GET() {
// In production, fetch from your FastAPI URL with server-side secrets.
const rows = [
{ practice_id: 'A86005', total_appointments: 42, attendance_rate: 0.92, median_wait_minutes: 12 }
]
return NextResponse.json({ rows })
}

Keep SQL connectivity in FastAPI; Next.js calls the API server-side.

app/api/kpi/route.ts
import { NextResponse } from 'next/server'

export async function GET() {
const base = process.env.API_BASE_URL!
const r = await fetch(`${base}/kpi`, { cache: 'no-store' })
const data = await r.json()
return NextResponse.json(data)
}

This keeps DB drivers off the Node side and centralises audit/validation in one service.


πŸ” Auth (Azure AD / Entra ID with next-auth)​

Install next-auth and an Azure AD provider.

npm install next-auth @auth/core

Set the following in .env.local:

AZURE_AD_TENANT_ID=YOUR_TENANT_ID
AZURE_AD_CLIENT_ID=YOUR_APP_CLIENT_ID
AZURE_AD_CLIENT_SECRET=YOUR_APP_CLIENT_SECRET
NEXTAUTH_URL=http://localhost:3000
NEXTAUTH_SECRET=change_me

πŸ“¦ Styling & NHS branding​

  • Use @nhsuk/frontend CSS via a simple import (global).
  • Or a lightweight Tailwind setup for internal tools.
  • Add a β€œData last updated” footer based on the API response timestamp.

Global CSS import example

app/layout.tsx
import "./globals.css"
import "@nhsuk/frontend/dist/nhsuk.css"

export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body className="nhsuk-width-container">{children}</body>
</html>
)
}

🐳 Docker (production-like)​

Dockerfile

# Install deps
FROM node:20-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json* pnpm-lock.yaml* yarn.lock* ./
RUN npm ci || yarn install --frozen-lockfile || pnpm install --frozen-lockfile

# Build
FROM node:20-alpine AS build
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build

# Run
FROM node:20-alpine AS run
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build /app/public ./public
COPY --from=build /app/.next ./.next
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/package.json ./package.json
EXPOSE 3000
CMD ["npm", "start"]

Build and run:

docker build -t nhs-next .
docker run -p 3000:3000 --env-file .env.local nhs-next

Deploy to AWS App Runner or Azure App Service/Container Apps; keep private networking and front with SSO/WAF as required.


πŸ”Ž Performance & caching​

  • Live KPIs: fetch(..., { cache: 'no-store' }).
  • Cached pages: export const revalidate = 300 in a page to ISR every 5 minutes.
  • Use server actions or route handlers for computations; avoid shipping secrets/client keys.

πŸ›‘ IG & safety checklist​

  • Keep secrets server-side; never expose DB creds in browser JS.
  • Prefer API β†’ SSR pattern: UI only calls your own API; API calls the DB/warehouse.
  • Add small-number suppression in the API; render read-only KPIs.
  • Log access at the proxy and API layers; keep an audit README in the repo.
  • Disable /api public access if app is intranet-only (private networking, auth).

πŸ“ Measuring impact​

  • Time to interactive (TTI) for core pages.
  • p95 API latency when fetching KPIs server-side.
  • Cache efficiency: ISR hit rate; revalidate frequency.
  • Security: endpoints behind auth; secrets in store not code.

πŸ”— See also​

See also: FastAPI Β· React Β· AWS Β· Azure Β· Secrets & .env

What’s next?

You’ve completed the Learn β€” Next.js stage. Keep momentum: