-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(v2): warn user when there are conflicting routes (#3083)
* feat(v2): add warning for path override * feat(v2): check all routes recursively * docs(v2): add docs for conflicting routes * style(v2): improve comments in code * refactor(v2): remove unused lifecycle method from docs plugin * Revert "refactor(v2): remove unused lifecycle method from docs plugin" This reverts commit 8b2caaa. * feat(v2): add option for changing duplicate path behavior * feat(v2): decouple logging from logic and detect duplicate routes in one pass * test(v2): fix failing tests * test(v2): add tests for duplicateRoutes * test(v2): add test for handleDuplicateRoutes * style(v2): add else statement * docs(v2): modify documentation for duplicate routes * docs(v2): move doc into guides folder * fix(v2): fix broken links * docs(v2): move docs for docusaurus config into api folder * style(v2): add comments * refactor(v2): extract getFinalRoutes * refactor(v2): scope getFinalRoutes to docusaurus package * test(v2): remove obsolete snapshots * docs(v2): remove some docs * fix(v2): rerun github actions * docs(v2): change slug of docs in api folder * refactor(v2): extract out a reportMessage method * refactor(v2): extract getAllFinalRoutes * test(v2): replace snapshots with actual value * style(v2): remove unnecessary comment and change type * chore(v2): remove unused dependency * style(v2): remove unused code * Update packages/docusaurus/src/server/utils.ts * Update website/docs/guides/creating-pages.md Co-authored-by: Sébastien Lorber <[email protected]>
- Loading branch information
Showing
19 changed files
with
234 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/docusaurus/src/server/__tests__/__snapshots__/duplicateRoutes.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`duplicateRoutes getDuplicateRoutesMessage 1`] = ` | ||
"Attempting to create page at /, but a page already exists at this route | ||
Attempting to create page at /, but a page already exists at this route | ||
Attempting to create page at /blog, but a page already exists at this route | ||
Attempting to create page at /doc/search, but a page already exists at this route" | ||
`; | ||
|
||
exports[`duplicateRoutes handleDuplicateRoutes 1`] = ` | ||
"Duplicate routes found! | ||
Attempting to create page at /search, but a page already exists at this route | ||
Attempting to create page at /sameDoc, but a page already exists at this route | ||
This could lead to non-deterministic routing behavior" | ||
`; |
54 changes: 54 additions & 0 deletions
54
packages/docusaurus/src/server/__tests__/duplicateRoutes.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
import { | ||
getAllDuplicateRoutes, | ||
getDuplicateRoutesMessage, | ||
handleDuplicateRoutes, | ||
} from '../duplicateRoutes'; | ||
import {RouteConfig} from '@docusaurus/types'; | ||
|
||
const routes: RouteConfig[] = [ | ||
{ | ||
path: '/', | ||
component: '', | ||
routes: [ | ||
{path: '/search', component: ''}, | ||
{path: '/sameDoc', component: ''}, | ||
], | ||
}, | ||
{ | ||
path: '/', | ||
component: '', | ||
routes: [ | ||
{path: '/search', component: ''}, | ||
{path: '/sameDoc', component: ''}, | ||
{path: '/uniqueDoc', component: ''}, | ||
], | ||
}, | ||
]; | ||
|
||
describe('duplicateRoutes', () => { | ||
test('getDuplicateRoutesMessage', () => { | ||
const message = getDuplicateRoutesMessage([ | ||
'/', | ||
'/', | ||
'/blog', | ||
'/doc/search', | ||
]); | ||
expect(message).toMatchSnapshot(); | ||
}); | ||
|
||
test('getAllDuplicateRoutes', () => { | ||
expect(getAllDuplicateRoutes(routes)).toEqual(['/search', '/sameDoc']); | ||
}); | ||
|
||
test('handleDuplicateRoutes', () => { | ||
expect(() => { | ||
handleDuplicateRoutes(routes, 'throw'); | ||
}).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
import {RouteConfig} from '@docusaurus/types'; | ||
import {getAllFinalRoutes} from '../utils'; | ||
|
||
describe('getAllFinalRoutes', () => { | ||
test('should get final routes correctly', () => { | ||
const routes: RouteConfig[] = [ | ||
{ | ||
path: '/docs', | ||
component: '', | ||
routes: [ | ||
{path: '/docs/someDoc', component: ''}, | ||
{path: '/docs/someOtherDoc', component: ''}, | ||
], | ||
}, | ||
{ | ||
path: '/community', | ||
component: '', | ||
}, | ||
]; | ||
expect(getAllFinalRoutes(routes)).toEqual([ | ||
routes[0].routes[0], | ||
routes[0].routes[1], | ||
routes[1], | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
import {RouteConfig, ReportingSeverity} from '@docusaurus/types'; | ||
import {getAllFinalRoutes, reportMessage} from './utils'; | ||
|
||
export function getAllDuplicateRoutes( | ||
pluginsRouteConfigs: RouteConfig[], | ||
): string[] { | ||
const allRoutes: string[] = getAllFinalRoutes(pluginsRouteConfigs).map( | ||
(routeConfig) => routeConfig.path, | ||
); | ||
const seenRoutes: Record<string, any> = {}; | ||
const duplicateRoutes: string[] = allRoutes.filter(function (route) { | ||
if (seenRoutes.hasOwnProperty(route)) { | ||
return true; | ||
} else { | ||
seenRoutes[route] = true; | ||
return false; | ||
} | ||
}); | ||
return duplicateRoutes; | ||
} | ||
|
||
export function getDuplicateRoutesMessage( | ||
allDuplicateRoutes: string[], | ||
): string { | ||
const message = allDuplicateRoutes | ||
.map( | ||
(duplicateRoute) => | ||
`Attempting to create page at ${duplicateRoute}, but a page already exists at this route`, | ||
) | ||
.join('\n'); | ||
return message; | ||
} | ||
|
||
export function handleDuplicateRoutes( | ||
pluginsRouteConfigs: RouteConfig[], | ||
onDuplicateRoutes: ReportingSeverity, | ||
): void { | ||
if (onDuplicateRoutes === 'ignore') { | ||
return; | ||
} | ||
const duplicatePaths: string[] = getAllDuplicateRoutes(pluginsRouteConfigs); | ||
const message: string = getDuplicateRoutesMessage(duplicatePaths); | ||
if (message) { | ||
const finalMessage = `Duplicate routes found!\n${message}\nThis could lead to non-deterministic routing behavior`; | ||
reportMessage(finalMessage, onDuplicateRoutes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.