From 71283dc7df6575c344db4de58e8e4d79fdba33c1 Mon Sep 17 00:00:00 2001 From: Endi Date: Sun, 21 Jul 2019 11:14:14 +0700 Subject: [PATCH] fix(v2): docusaurus route config generation for empty path (#1683) * fix(v2): route config does not acceptempty string * test snapshot * add test * changelog --- CHANGELOG-2.x.md | 2 + .../__snapshots__/routes.test.ts.snap | 39 +++++++++++++++++++ .../src/server/__tests__/routes.test.ts | 14 ++++++- packages/docusaurus/src/server/routes.ts | 4 +- 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/CHANGELOG-2.x.md b/CHANGELOG-2.x.md index be7db8063a..b77c01686d 100644 --- a/CHANGELOG-2.x.md +++ b/CHANGELOG-2.x.md @@ -2,6 +2,8 @@ ## Unreleased +- Fix docusaurus route config generation for certain edge case + ## 2.0.0-alpha.22 - Add missing dependencies on `@docusaurus/preset-classic` diff --git a/packages/docusaurus/src/server/__tests__/__snapshots__/routes.test.ts.snap b/packages/docusaurus/src/server/__tests__/__snapshots__/routes.test.ts.snap index 6b45391c92..c69b21579b 100644 --- a/packages/docusaurus/src/server/__tests__/__snapshots__/routes.test.ts.snap +++ b/packages/docusaurus/src/server/__tests__/__snapshots__/routes.test.ts.snap @@ -147,3 +147,42 @@ export default [ ], } `; + +exports[`loadRoutes route config with empty (but valid) path string 1`] = ` +Object { + "registry": Object { + "component---hello-world-jse-0-f-b6c": Object { + "importStatement": "() => import(/* webpackChunkName: 'component---hello-world-jse-0-f-b6c' */ \\"hello/world.js\\")", + "modulePath": "hello/world.js", + }, + }, + "routesChunkNames": Object { + "": Object { + "component": "component---hello-world-jse-0-f-b6c", + }, + }, + "routesConfig": " +import React from 'react'; +import ComponentCreator from '@docusaurus/ComponentCreator'; + +export default [ + +{ + path: '', + component: ComponentCreator(''), + + +}, + + { + path: '*', + component: ComponentCreator('*') + } +]; +", + "routesPaths": Array [ + "404.html", + "", + ], +} +`; diff --git a/packages/docusaurus/src/server/__tests__/routes.test.ts b/packages/docusaurus/src/server/__tests__/routes.test.ts index db1c019b69..ee6e778931 100644 --- a/packages/docusaurus/src/server/__tests__/routes.test.ts +++ b/packages/docusaurus/src/server/__tests__/routes.test.ts @@ -73,7 +73,7 @@ describe('loadRoutes', () => { } as RouteConfig; expect(loadRoutes([routeConfigWithoutPath])).rejects.toMatchInlineSnapshot(` - [Error: Invalid routeConfig (Path and component is required) + [Error: Invalid routeConfig (Path must be a string and component is required) {"component":"hello/world.js"}] `); @@ -83,8 +83,18 @@ describe('loadRoutes', () => { expect(loadRoutes([routeConfigWithoutComponent])).rejects .toMatchInlineSnapshot(` - [Error: Invalid routeConfig (Path and component is required) + [Error: Invalid routeConfig (Path must be a string and component is required) {"path":"/hello/world"}] `); }); + + test('route config with empty (but valid) path string', async () => { + const routeConfig = { + path: '', + component: 'hello/world.js', + } as RouteConfig; + + const result = await loadRoutes([routeConfig]); + expect(result).toMatchSnapshot(); + }); }); diff --git a/packages/docusaurus/src/server/routes.ts b/packages/docusaurus/src/server/routes.ts index 58ff432532..98dd941459 100644 --- a/packages/docusaurus/src/server/routes.ts +++ b/packages/docusaurus/src/server/routes.ts @@ -41,9 +41,9 @@ export async function loadRoutes(pluginsRouteConfigs: RouteConfig[]) { exact, } = routeConfig; - if (!routePath || !component) { + if (!_.isString(routePath) || !component) { throw new Error( - `Invalid routeConfig (Path and component is required) \n${JSON.stringify( + `Invalid routeConfig (Path must be a string and component is required) \n${JSON.stringify( routeConfig, )}`, );