Skip to content
Norcel
Back to blog
Demo · · 2 min read · Norcel

Demo Blog 4: Scaling Edge Middleware & Background Jobs

A deep dive into building low-latency edge middlewares, handling async workloads, and keeping background workers rock solid in Next.js.

Welcome to Demo Blog 4. Once you have authentication, database migrations, and payments working smoothly, the next hurdle every growing SaaS faces is latency and background work: keeping edge requests under 50ms while running heavy background tasks without timeouts.

Here is how we design low-overhead edge middleware and reliable background jobs in production.

Edge Middleware: Fast, Minimal, and Resilient

Next.js middleware runs on edge runtime environments (V8 isolates). That means no Node.js built-ins like node:crypto or heavyweight ORMs like Prisma.

The edge layer should do exactly three things:

  1. Route protection: Verify session validity with lightweight cryptographic checks or edge-compatible tokens.
  2. Geo-routing & redirects: Redirect users to locale/region endpoints before page rendering.
  3. Security headers: Inject CSP, HSTS, and X-Frame-Options headers on every outbound response.
// middleware.ts
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/request';

export function middleware(req: NextRequest) {
  const sessionToken = req.cookies.get('__Secure-authjs.session-token');
  const isProtectedPath = req.nextUrl.pathname.startsWith('/dashboard');

  if (isProtectedPath && !sessionToken) {
    const loginUrl = new URL('/login', req.url);
    loginUrl.searchParams.set('callbackUrl', req.nextUrl.pathname);
    return NextResponse.redirect(loginUrl);
  }

  const response = NextResponse.next();
  response.headers.set('X-Content-Type-Options', 'nosniff');
  response.headers.set('X-Frame-Options', 'DENY');
  return response;
}

export const config = {
  matcher: ['/dashboard/:path*', '/settings/:path*'],
};

Keep edge middleware under 1MB bundle size and under 10ms execution time. Anything heavier belongs in Route Handlers or Server Actions.

Background Jobs: Surviving Serverless Timeouts

Serverless functions have execution limits. When a user exports 50,000 records or triggers AI generation, processing it in the HTTP request will fail with a 504 Gateway Timeout.

Instead, use an asynchronous queue pattern:

PatternBest ForTypical Latency
Inngest / Trigger.devComplex multi-step workflows, retriesSub-second dispatch
BullMQ + RedisHigh-volume message processingReal-time queueing
Postgres SKIP LOCKEDLightweight queues without extra infra1-5 second polling

Example: Async Job Dispatch with Webhooks

// app/api/export/route.ts
import { NextResponse } from 'next/server';
import { inngest } from '@/lib/inngest';
import { auth } from '@/lib/auth';

export async function POST(req: Request) {
  const session = await auth();
  if (!session?.user?.id) {
    return new NextResponse('Unauthorized', { status: 401 });
  }

  // Enqueue job immediately — return 202 Accepted
  const event = await inngest.send({
    name: 'user/export.requested',
    data: { userId: session.user.id },
  });

  return NextResponse.json({ jobId: event.ids[0] }, { status: 202 });
}

Key Takeaways

  1. Keep middleware lean: Don’t import heavy database clients at the edge.
  2. Offload long tasks: Return 202 Accepted immediately and process work asynchronously.
  3. Notify via WebSockets or Webhooks: Let the client poll or listen for job completion.

That’s the architecture that keeps your SaaS feeling instantaneous even under heavy traffic.

Keep reading

Demo ·

Demo Blog: A Walkthrough of the Norcel Stack

A hands-on demo showing how Norcel's Next.js SaaS starter handles auth, database, email, and billing — from zero to production in a weekend.

Architecture ·

Norcel OS: Treating Your SaaS Starter as an Operating System

A deep dive into the Norcel architecture philosophy — how treating your Next.js SaaS starter as an OS-level foundation changes the way you ship products.

Next.js ·

How to Build a Production-Ready SaaS with Next.js

A practical walkthrough of the architecture, security, and infrastructure decisions that turn a Next.js prototype into a SaaS you can charge for.