fix(v2): include base url in 404 route (#2237)

* fix(v2): include base url in 404 route

* Update tests
This commit is contained in:
Alexey Pyltsyn 2020-01-22 04:00:38 +03:00 committed by Yangshun Tay
parent 2d08787ed5
commit 06044cffa7
4 changed files with 22 additions and 18 deletions

View file

@ -55,7 +55,7 @@ export default [
]; ];
", ",
"routesPaths": Array [ "routesPaths": Array [
"404.html", "/404.html",
"/blog", "/blog",
], ],
} }
@ -141,7 +141,7 @@ export default [
]; ];
", ",
"routesPaths": Array [ "routesPaths": Array [
"404.html", "/404.html",
"/docs/hello", "/docs/hello",
"docs/foo/baz", "docs/foo/baz",
], ],
@ -181,7 +181,7 @@ export default [
]; ];
", ",
"routesPaths": Array [ "routesPaths": Array [
"404.html", "/404.html",
"", "",
], ],
} }

View file

@ -36,7 +36,7 @@ describe('loadRoutes', () => {
}, },
], ],
}; };
const result = await loadRoutes([nestedRouteConfig]); const result = await loadRoutes([nestedRouteConfig], '/');
expect(result).toMatchSnapshot(); expect(result).toMatchSnapshot();
}); });
@ -64,7 +64,7 @@ describe('loadRoutes', () => {
], ],
}, },
}; };
const result = await loadRoutes([flatRouteConfig]); const result = await loadRoutes([flatRouteConfig], '/');
expect(result).toMatchSnapshot(); expect(result).toMatchSnapshot();
}); });
@ -73,20 +73,21 @@ describe('loadRoutes', () => {
component: 'hello/world.js', component: 'hello/world.js',
} as RouteConfig; } as RouteConfig;
expect(loadRoutes([routeConfigWithoutPath])).rejects.toMatchInlineSnapshot(` expect(loadRoutes([routeConfigWithoutPath], '/')).rejects
[Error: Invalid routeConfig (Path must be a string and component is required) .toMatchInlineSnapshot(`
{"component":"hello/world.js"}] [Error: Invalid routeConfig (Path must be a string and component is required)
`); {"component":"hello/world.js"}]
`);
const routeConfigWithoutComponent = { const routeConfigWithoutComponent = {
path: '/hello/world', path: '/hello/world',
} as RouteConfig; } as RouteConfig;
expect(loadRoutes([routeConfigWithoutComponent])).rejects expect(loadRoutes([routeConfigWithoutComponent], '/')).rejects
.toMatchInlineSnapshot(` .toMatchInlineSnapshot(`
[Error: Invalid routeConfig (Path must be a string and component is required) [Error: Invalid routeConfig (Path must be a string and component is required)
{"path":"/hello/world"}] {"path":"/hello/world"}]
`); `);
}); });
test('route config with empty (but valid) path string', async () => { test('route config with empty (but valid) path string', async () => {
@ -95,7 +96,7 @@ describe('loadRoutes', () => {
component: 'hello/world.js', component: 'hello/world.js',
} as RouteConfig; } as RouteConfig;
const result = await loadRoutes([routeConfig]); const result = await loadRoutes([routeConfig], '/');
expect(result).toMatchSnapshot(); expect(result).toMatchSnapshot();
}); });
}); });

View file

@ -143,7 +143,7 @@ export async function load(siteDir: string): Promise<Props> {
routesChunkNames, routesChunkNames,
routesConfig, routesConfig,
routesPaths, routesPaths,
} = await loadRoutes(pluginsRouteConfigs); } = await loadRoutes(pluginsRouteConfigs, baseUrl);
const genRegistry = generate( const genRegistry = generate(
generatedFilesDir, generatedFilesDir,

View file

@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
import {genChunkName} from '@docusaurus/utils'; import {genChunkName, normalizeUrl} from '@docusaurus/utils';
import _ from 'lodash'; import _ from 'lodash';
import {stringify} from 'querystring'; import {stringify} from 'querystring';
import { import {
@ -38,7 +38,10 @@ function getModulePath(target: Module): string {
return `${target.path}${queryStr}`; return `${target.path}${queryStr}`;
} }
export async function loadRoutes(pluginsRouteConfigs: RouteConfig[]) { export async function loadRoutes(
pluginsRouteConfigs: RouteConfig[],
baseUrl: string,
) {
const routesImports = [ const routesImports = [
`import React from 'react';`, `import React from 'react';`,
`import ComponentCreator from '@docusaurus/ComponentCreator';`, `import ComponentCreator from '@docusaurus/ComponentCreator';`,
@ -46,7 +49,7 @@ export async function loadRoutes(pluginsRouteConfigs: RouteConfig[]) {
const registry: { const registry: {
[chunkName: string]: ChunkRegistry; [chunkName: string]: ChunkRegistry;
} = {}; } = {};
const routesPaths: string[] = ['404.html']; const routesPaths: string[] = [normalizeUrl([baseUrl, '404.html'])];
const routesChunkNames: { const routesChunkNames: {
[routePath: string]: ChunkNames; [routePath: string]: ChunkNames;
} = {}; } = {};