Next.js

Next.js App Router patterns including server actions, middleware, and API route handlers.

20 snippets

Showing 20 of 20 snippets

typescriptintermediate

Type-Safe API Route Handler

Next.js App Router route handler with input validation, typed responses, and proper error handling.

#api#route-handler
typescriptintermediate

Authentication Middleware Guard

Next.js middleware that checks auth tokens on protected routes and redirects unauthenticated users to login.

#middleware#authentication
typescriptintermediate

Server Action with Form Validation

Next.js Server Action handling form submissions with validation, error messages, and redirect on success.

#server-actions#forms
typescriptadvanced

Dynamic OG Image Generation

Generate Open Graph images on-the-fly using Next.js ImageResponse with custom fonts and dynamic content.

#og-image#seo
typescriptintermediate

Edge Middleware Geolocation

Use Vercel Edge geolocation headers to personalize content based on the visitor's country and city.

#edge#geolocation
typescriptadvanced

On-Demand ISR Revalidation

Trigger incremental static regeneration via API route with secret token validation for instant cache purge.

#isr#revalidation
typescriptbeginner

Protected Page with Server Redirect

Server Component that checks auth status and redirects unauthenticated users before rendering any content.

#auth#redirect
typescriptbeginner

Reusable Metadata Factory

Factory function to generate consistent SEO metadata across all pages with Open Graph and canonical URLs.

#seo#metadata
typescriptadvanced

Streaming API Response

Stream long-running API responses using ReadableStream and TransformStream for real-time data delivery.

#streaming#api
typescriptadvanced

Parallel Routes Layout

Next.js parallel routes pattern to render multiple page slots simultaneously in a shared layout.

#parallel-routes#layout
typescriptadvanced

Edge Middleware Rate Limiter

Rate limit API requests at the edge using a sliding window counter with configurable thresholds.

#middleware#rate-limiting
typescriptbeginner

Cached Fetch with Revalidation

Fetch external data in Server Components with time-based revalidation and error boundaries.

#fetch#caching
typescriptintermediate

NextAuth Session Check Pattern

Check authentication status in Server Components and redirect unauthorized users with NextAuth.js.

#authentication#nextauth
typescriptbeginner

Next.js Image Optimization Patterns

Use next/image with responsive sizes, blur placeholders, and priority loading for optimal Core Web Vitals.

#images#optimization
typescriptbeginner

Environment Variable Validation

Validate required environment variables at build time with type-safe access and descriptive errors.

#environment#validation
typescriptbeginner

Redirect Matrix in next.config

Define URL redirect rules in next.config.ts for SEO migration, vanity URLs, and path normalization.

#redirects#seo
typescriptadvanced

Partial Prerendering with Suspense

Combine static shells with streamed dynamic content using React Suspense for instant page loads.

#ppr#suspense
typescriptintermediate

Server Action with Revalidation

Mutate data with Server Actions and revalidate cached paths or tags for instant UI updates.

#server-actions#revalidation
typescriptadvanced

Intercepting Routes Modal

Show content in a modal on soft navigation while preserving the full page on hard navigation.

#routing#modals
typescriptadvanced

Multi-Zone Application Setup

Compose multiple Next.js apps under one domain using rewrites for micro-frontend architecture.

#multi-zone#micro-frontends