From 14dd7c2954c82ffab51345591f2e6f5f51e28264 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Fri, 16 Jul 2021 09:59:54 -0500 Subject: [PATCH] Add warning for large number of routes (#27214) 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 --- errors/manifest.json | 4 ++++ errors/max-custom-routes-reached.md | 17 +++++++++++++++++ packages/next/lib/load-custom-routes.ts | 19 +++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 errors/max-custom-routes-reached.md diff --git a/errors/manifest.json b/errors/manifest.json index ef11ac95b9652..1ba5b512db5f4 100644 --- a/errors/manifest.json +++ b/errors/manifest.json @@ -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" diff --git a/errors/max-custom-routes-reached.md b/errors/max-custom-routes-reached.md new file mode 100644 index 0000000000000..94398ff987774 --- /dev/null +++ b/errors/max-custom-routes-reached.md @@ -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) diff --git a/packages/next/lib/load-custom-routes.ts b/packages/next/lib/load-custom-routes.ts index ca4240bcf798b..be3fb097b4049 100644 --- a/packages/next/lib/load-custom-routes.ts +++ b/packages/next/lib/load-custom-routes.ts @@ -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' @@ -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( {