Performance: Matching paths with config
This affects how quickly Next.js can resolve routes and serve pages, impacting initial load and navigation speed.
Jump into concepts and practice - no test required
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 |
/dashboard in Next.js config?{ matchers: ["/blog/:slug"] }matchers: ["/profile/:id"]
/profile. Why?/blog/post-1 and /blog/post-1/comments. Which matcher config is correct?