Best Practices for Performance with 1000+ Redirects (Regex, Wildcard, Path Matching) #84951
Replies: 1 comment
-
Hi @burakozdemir32 👋 TL;DR: Treat redirects in tiers. Exact 1:1 → use redirects() in next.config.js (compiled to a manifest and matched before your app code) or handle at the CDN/reverse-proxy. Param / wildcard / regex → use Middleware with a precompiled matcher (no per-request regex creation), indexed by prefix so you don’t iterate 1k rules every request. Avoid remote lookups on the hot path. 1) Exact 1:1 (fastest path) next.config.js **2) Param, wildcard, and regex (use Middleware with an indexed matcher) Idea: Build a prefix index → only check a small subset of rules. Precompile patterns once at module scope.** // Example rules (could be imported as JSON at build time) type Compiled = { // Precompile once export function middleware(req: Request) { for (const c of candidates) { return NextResponse.next(); export const config = {
3) Data-structure tips Prefer path-to-regexp (precompiled) over ad-hoc RegExp strings. Index by first static segment (or longest static prefix) → O(1) bucket lookup. If patterns share long prefixes (e.g., /docs/...), build a radix trie; libraries like find-my-way (pure JS) do this well. 4) What not to do Don’t evaluate a thousand regexes every request. Don’t fetch rules from a remote KV/DB on each hit—hydrate at build time or cache in module scope. Bloom filters help exact matches, but not wildcards/regex—use them only for tier-1 lookups (or skip; the manifest/CDN is already O(1)). 5) When to use Middleware vs next.config.js Use redirects() for exact routes (fastest, zero code). Use Middleware only for parameterized/wildcard/regex or when redirect logic depends on request context (headers, A/B, geo). Bottom line: Keep exact redirects in redirects()/CDN. Handle complex patterns in Middleware with a precompiled, prefix-indexed matcher. Avoid per-request iteration and any network I/O. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Hi everyone,
We're scaling up our Next.js application and will soon need to manage a large list of over 1,000 redirects. This list is a mix of different types:
My main concern is the performance impact on our server response time.
For the simple 1:1 redirects, I was considering using a Bloom filter as a quick, probabilistic check to see if a path exists in our redirect map before doing a full lookup. This seems very efficient for exact matches.
However, a Bloom filter doesn't work for path matching, wildcards, or regex patterns.
My questions are:
What is the recommended, high-performance approach for handling a large number of wildcard and regex-based redirects in Next.js?
Is the built-in next.config.js redirects array optimized to handle this scale without a significant performance hit?
Should I move this logic into middleware?
If using middleware, what's the best strategy to avoid iterating through a thousand regexes on every request? Are there specific data structures or optimized regex engines that the community recommends for this use case?
I'm trying to build a solution that is both scalable and keeps our TTFB (Time to First Byte) as low as possible. Any insights or examples would be greatly appreciated.
Thanks!
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions