Performance: Matching paths with config
MEDIUM IMPACT
This affects how quickly Next.js can resolve routes and serve pages, impacting initial load and navigation speed.
const routes = [ { path: '/posts/[id]', page: 'PostPage' }, { path: '/posts/[slug]', page: 'PostSlugPage' } ]; function matchRoute(path) { for (const route of routes) { if (path.startsWith(route.path.replace('[id]', '')) || path.startsWith(route.path.replace('[slug]', ''))) { return route.page; } } return 'CatchAllPage'; }
const routes = [ { path: '/posts/:id(\\d+)', page: 'PostPage' }, { path: '/posts/:slug([a-z-]+)', page: 'PostSlugPage' }, { path: '/posts/:any*', page: 'CatchAllPage' } ]; function matchRoute(path) { for (const route of routes) { const regex = new RegExp('^' + route.path.replace(/:[^/]+/g, '[^/]+') + '$'); if (regex.test(path)) return route.page; } return null; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Complex regex matching for paths | 0 | 0 | Blocks rendering 10-20ms | [X] Bad |
| Simple string prefix matching | 0 | 0 | Blocks rendering <1ms | [OK] Good |