Maintenance in progress: we are indexing a large batch of new skills. Some pages may load slowly or briefly show no results. Nothing is lost, and everything is back to normal within the hour.

Managing Advanced Middleware

Advanced middleware logic for security and routing. Use for global rate limiting, security headers, and protection of admin routes.

majiayu000 9385c5f 2 files · 2.0 KB Updated 567 repo stars

File contents

Advanced Middleware and Security Logic

When to use this skill

  • Implementing site-wide security measures.
  • Protecting sensitive routes like /admin/* or /dashboard/*.
  • Injecting nonces or CSP headers.

Workflow

  • Create middleware.ts in the root (Next.js 15).
  • Define config.matcher to specify target routes.
  • Implement checks (Auth check, Rate limit, CSP).

Code Pattern (CSP Header)

import { NextResponse } from 'next/server';

export function middleware(request: Request) {
    const nonce = Buffer.from(crypto.randomUUID()).toString('base64');
    const csp = `default-src 'self'; script-src 'self' 'nonce-${nonce}';`;
    
    const response = NextResponse.next();
    response.headers.set('Content-Security-Policy', csp);
    return response;
}

Instructions

  • Performance: Keep middleware logic lightweight to avoid slowing down every request.
  • Auth: Appwrite sessions are stored in cookies; middleware can check for the presence of these cookies for simple protection.

majiayu000/claude-skill-registry-data/tree/main/data/managing-advanced-middleware commit 9385c5ff09

Frequently asked questions

npx skillmds add majiayu000/managing-advanced-middleware