fix(v2): more consistent route config generation (#1946)

* fix(v2): more consistent route config generation

* nits

* snapshot

* address review & test upd

* reduce 2xloop -> 1x loop in docs plugin

* nits
This commit is contained in:
Endi 2019-11-08 11:55:48 +07:00 committed by GitHub
parent 384e83a737
commit fc4928f7a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 17 deletions

View file

@ -2,6 +2,7 @@
## Unreleased ## Unreleased
- Ensure routes config generation to be more consistent in ordering. Nested routes should be placed last in routes.js. This will allow user to create `src/pages/docs.js` to create custom docs page for `/docs` or even `src/pages/docs/super.js` to create page for `/docs/super/`;
- Fix watcher does not trigger reload on windows. - Fix watcher does not trigger reload on windows.
- Add feed for blog posts. - Add feed for blog posts.
- **HOTFIX for 2.0.0-alpha.32** - Fix build compilation if exists only one code tab. - **HOTFIX for 2.0.0-alpha.32** - Fix build compilation if exists only one code tab.

View file

@ -55,9 +55,9 @@ Array [
Object { Object {
"component": "@theme/DocPage", "component": "@theme/DocPage",
"modules": Object { "modules": Object {
"docsMetadata": "@docusaurus-plugin-content-docs/docs-b5f.json", "docsMetadata": "@docusaurus-plugin-content-docs/docs-route-ff2.json",
}, },
"path": "/docs", "path": "/docs/:route",
"routes": Array [ "routes": Array [
Object { Object {
"component": "@theme/DocItem", "component": "@theme/DocItem",

View file

@ -9,7 +9,7 @@ import globby from 'globby';
import fs from 'fs-extra'; import fs from 'fs-extra';
import path from 'path'; import path from 'path';
import {idx, normalizeUrl, docuHash} from '@docusaurus/utils'; import {idx, normalizeUrl, docuHash} from '@docusaurus/utils';
import {LoadContext, Plugin, DocusaurusConfig} from '@docusaurus/types'; import {LoadContext, Plugin} from '@docusaurus/types';
import createOrder from './order'; import createOrder from './order';
import loadSidebars from './sidebars'; import loadSidebars from './sidebars';
@ -117,6 +117,7 @@ export default function pluginContentDocs(
// Construct docsMetadata // Construct docsMetadata
const docsMetadata: DocsMetadata = {}; const docsMetadata: DocsMetadata = {};
const permalinkToSidebar: PermalinkToSidebar = {};
Object.keys(docsMetadataRaw).forEach(currentID => { Object.keys(docsMetadataRaw).forEach(currentID => {
let previous; let previous;
let next; let next;
@ -139,10 +140,9 @@ export default function pluginContentDocs(
previous, previous,
next, next,
}; };
});
const permalinkToSidebar: PermalinkToSidebar = {}; // sourceToPermalink and permalinkToSidebar mapping
Object.values(docsMetadataRaw).forEach(({source, permalink, sidebar}) => { const {source, permalink, sidebar} = docsMetadataRaw[currentID];
sourceToPermalink[source] = permalink; sourceToPermalink[source] = permalink;
if (sidebar) { if (sidebar) {
permalinkToSidebar[permalink] = sidebar; permalinkToSidebar[permalink] = sidebar;
@ -240,8 +240,9 @@ export default function pluginContentDocs(
}; };
const docsBaseRoute = normalizeUrl([ const docsBaseRoute = normalizeUrl([
(context.siteConfig as DocusaurusConfig).baseUrl, context.baseUrl,
routeBasePath, routeBasePath,
':route',
]); ]);
const docsBaseMetadataPath = await createData( const docsBaseMetadataPath = await createData(
`${docuHash(docsBaseRoute)}.json`, `${docuHash(docsBaseRoute)}.json`,

View file

@ -80,8 +80,8 @@ Object {
"loader": "() => import(/* webpackChunkName: 'content---docs-helloaff-811' */ \\"docs/hello.md\\")", "loader": "() => import(/* webpackChunkName: 'content---docs-helloaff-811' */ \\"docs/hello.md\\")",
"modulePath": "docs/hello.md", "modulePath": "docs/hello.md",
}, },
"docsMetadata---docsf-34-2ab": Object { "docsMetadata---docs-routef-34-881": Object {
"loader": "() => import(/* webpackChunkName: 'docsMetadata---docsf-34-2ab' */ \\"docs-b5f.json\\")", "loader": "() => import(/* webpackChunkName: 'docsMetadata---docs-routef-34-881' */ \\"docs-b5f.json\\")",
"modulePath": "docs-b5f.json", "modulePath": "docs-b5f.json",
}, },
"metadata---docs-foo-baz-2-cf-fa7": Object { "metadata---docs-foo-baz-2-cf-fa7": Object {
@ -94,15 +94,15 @@ Object {
}, },
}, },
"routesChunkNames": Object { "routesChunkNames": Object {
"/docs": Object {
"component": "component---theme-doc-page-1-be-9be",
"docsMetadata": "docsMetadata---docsf-34-2ab",
},
"/docs/hello": Object { "/docs/hello": Object {
"component": "component---theme-doc-item-178-a40", "component": "component---theme-doc-item-178-a40",
"content": "content---docs-helloaff-811", "content": "content---docs-helloaff-811",
"metadata": "metadata---docs-hello-956-741", "metadata": "metadata---docs-hello-956-741",
}, },
"/docs:route": Object {
"component": "component---theme-doc-page-1-be-9be",
"docsMetadata": "docsMetadata---docs-routef-34-881",
},
"docs/foo/baz": Object { "docs/foo/baz": Object {
"component": "component---theme-doc-item-178-a40", "component": "component---theme-doc-item-178-a40",
"content": "content---docs-foo-baz-8-ce-61e", "content": "content---docs-foo-baz-8-ce-61e",
@ -116,8 +116,8 @@ import ComponentCreator from '@docusaurus/ComponentCreator';
export default [ export default [
{ {
path: '/docs', path: '/docs:route',
component: ComponentCreator('/docs'), component: ComponentCreator('/docs:route'),
routes: [ routes: [
{ {

View file

@ -5,13 +5,14 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
import {loadRoutes, RouteConfig} from '../routes'; import {loadRoutes} from '../routes';
import {RouteConfig} from '@docusaurus/types';
describe('loadRoutes', () => { describe('loadRoutes', () => {
test('nested route config', async () => { test('nested route config', async () => {
const nestedRouteConfig: RouteConfig = { const nestedRouteConfig: RouteConfig = {
component: '@theme/DocPage', component: '@theme/DocPage',
path: '/docs', path: '/docs:route',
modules: { modules: {
docsMetadata: 'docs-b5f.json', docsMetadata: 'docs-b5f.json',
}, },

View file

@ -75,6 +75,17 @@ export async function loadPlugins({
}), }),
); );
// Sort the route config. This ensures that route with nested routes is always placed last
pluginsRouteConfigs.sort((a, b) => {
if (a.routes && !b.routes) {
return 1;
}
if (!a.routes && b.routes) {
return -1;
}
return a.path > b.path ? 1 : b.path > a.path ? -1 : 0;
});
return { return {
plugins, plugins,
pluginsRouteConfigs, pluginsRouteConfigs,