From ce46922b59dd0ee47f1c7e1ba6005d89252ce2b7 Mon Sep 17 00:00:00 2001 From: 0xADADA Date: Sun, 19 Feb 2023 23:18:29 -0500 Subject: [PATCH] speeds O(n^2) execution of detectConflictingPaths (#46080) ## Bug - [x] Related issues linked using `fixes #46079` https://github.com/vercel/next.js/issues/46079 - [ ] Integration tests added - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/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` - [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md) --- packages/next/src/build/utils.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/next/src/build/utils.ts b/packages/next/src/build/utils.ts index dc790b36b91d9..dfcd1d869b0d5 100644 --- a/packages/next/src/build/utils.ts +++ b/packages/next/src/build/utils.ts @@ -1554,6 +1554,17 @@ export function detectConflictingPaths( >() const dynamicSsgPages = [...ssgPages].filter((page) => isDynamicRoute(page)) + const additionalSsgPathsByPath: { + [page: string]: { [path: string]: string } + } = {} + + additionalSsgPaths.forEach((paths, pathsPage) => { + additionalSsgPathsByPath[pathsPage] ||= {} + paths.forEach((curPath) => { + const currentPath = curPath.toLowerCase() + additionalSsgPathsByPath[pathsPage][currentPath] = curPath + }) + }) additionalSsgPaths.forEach((paths, pathsPage) => { paths.forEach((curPath) => { @@ -1573,9 +1584,10 @@ export function detectConflictingPaths( conflictingPage = dynamicSsgPages.find((page) => { if (page === pathsPage) return false - conflictingPath = additionalSsgPaths - .get(page) - ?.find((compPath) => compPath.toLowerCase() === lowerPath) + conflictingPath = + additionalSsgPaths.get(page) == null + ? undefined + : additionalSsgPathsByPath[page][lowerPath] return conflictingPath })