Search Articles

Type a keyword to find reviews, comparisons, and guides...

Back to homepage
Developer Boilerplate Tool

Interactive SaaS Boilerplate Customizer

Construct and customize your Next.js application core layer. Toggle Redis rate limiting, database adapters, cookie authentication guards, and streaming LLM chat endpoints in one place.

Interactive Developer Helper

Boilerplate Customizer & Code Generator

Select backend adapters, security layers, or API streaming, then grab the production-ready code.

Base Setup

Database Clients

Middleware & Security

AI & LLM Orchestrators

middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { NextResponse } from 'next/server';
import { Ratelimit } from '@upstash/ratelimit';
import { Redis } from '@upstash/redis';

const redis = new Redis({
  url: process.env.UPSTASH_REDIS_REST_URL!,
  token: process.env.UPSTASH_REDIS_REST_TOKEN!,
});

const ratelimit = new Ratelimit({
  redis,
  limiter: Ratelimit.slidingWindow(10, '10 s'), // 10 requests per 10 seconds
});

export async function middleware(request) {
  // 1. Rate Limiting Check
  const ip = request.ip ?? '127.0.0.1';
  const { success } = await ratelimit.limit(ip);
  if (!success) {
    return new NextResponse('Too Many Requests', { status: 429 });
  }

  return NextResponse.next();
}

export const config = {
  matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
};

Deep-Dive Guides & Setup Documentation