Skip to content

Commit

Permalink
Add warning for large number of routes (#27214)
Browse files Browse the repository at this point in the history
This adds a warning when more than 1024 routes are added since it can have performance impacts and includes a document that we can add suggestions to reduce the number of routes being added. 

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes
  • Loading branch information
ijjk authored Jul 16, 2021
1 parent dd029f5 commit 14dd7c2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions errors/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,10 @@
"title": "import-esm-externals",
"path": "/errors/import-esm-externals.md"
},
{
"title": "max-custom-routes-reached",
"path": "max-custom-routes-reached.md"
},
{
"title": "static-page-generation-timeout",
"path": "/errors/static-page-generation-timeout.md"
Expand Down
17 changes: 17 additions & 0 deletions errors/max-custom-routes-reached.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Max Custom Routes Reached

#### Why This Error Occurred

The number of combined routes from `headers`, `redirects`, and `rewrites` exceeds 1000. This can impact performance because each request will iterate over all routes to check for a match in the worst case.

#### Possible Ways to Fix It

- Leverage dynamic routes inside of the `pages` folder to reduce the number of rewrites needed
- Combine headers routes into dynamic matches e.g. `/first-header-route` `/second-header-route` -> `/(first-header-route$|second-header-route$)`

### Useful Links

- [Dynamic Routes documentation](https://nextjs.org/docs/routing/dynamic-routes)
- [Rewrites documentation](https://nextjs.org/docs/api-reference/next.config.js/rewrites)
- [Redirects documentation](https://nextjs.org/docs/api-reference/next.config.js/redirects)
- [Headers documentation](https://nextjs.org/docs/api-reference/next.config.js/headers)
19 changes: 19 additions & 0 deletions packages/next/lib/load-custom-routes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import chalk from 'chalk'
import { parse as parseUrl } from 'url'
import { NextConfig } from '../server/config'
import * as pathToRegexp from 'next/dist/compiled/path-to-regexp'
Expand Down Expand Up @@ -621,6 +622,24 @@ export default async function loadCustomRoutes(
loadRedirects(config),
])

const totalRewrites =
rewrites.beforeFiles.length +
rewrites.afterFiles.length +
rewrites.fallback.length

const totalRoutes = headers.length + redirects.length + totalRewrites

if (totalRoutes > 1000) {
console.warn(
chalk.bold.yellow(`Warning: `) +
`total number of custom routes exceeds 1000, this can reduce performance. Route counts:\n` +
`headers: ${headers.length}\n` +
`rewrites: ${totalRewrites}\n` +
`redirects: ${redirects.length}\n` +
`See more info: https://nextjs.org/docs/messages/max-custom-routes-reached`
)
}

if (config.trailingSlash) {
redirects.unshift(
{
Expand Down

0 comments on commit 14dd7c2

Please sign in to comment.