Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default [
];
",
"routesPaths": Array [
"404.html",
"/404.html",
"/blog",
],
}
Expand Down Expand Up @@ -141,7 +141,7 @@ export default [
];
",
"routesPaths": Array [
"404.html",
"/404.html",
"/docs/hello",
"docs/foo/baz",
],
Expand Down Expand Up @@ -181,7 +181,7 @@ export default [
];
",
"routesPaths": Array [
"404.html",
"/404.html",
"",
],
}
Expand Down
23 changes: 12 additions & 11 deletions packages/docusaurus/src/server/__tests__/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('loadRoutes', () => {
},
],
};
const result = await loadRoutes([nestedRouteConfig]);
const result = await loadRoutes([nestedRouteConfig], '/');
expect(result).toMatchSnapshot();
});

Expand Down Expand Up @@ -64,7 +64,7 @@ describe('loadRoutes', () => {
],
},
};
const result = await loadRoutes([flatRouteConfig]);
const result = await loadRoutes([flatRouteConfig], '/');
expect(result).toMatchSnapshot();
});

Expand All @@ -73,20 +73,21 @@ describe('loadRoutes', () => {
component: 'hello/world.js',
} as RouteConfig;

expect(loadRoutes([routeConfigWithoutPath])).rejects.toMatchInlineSnapshot(`
[Error: Invalid routeConfig (Path must be a string and component is required)
{"component":"hello/world.js"}]
`);
expect(loadRoutes([routeConfigWithoutPath], '/')).rejects
.toMatchInlineSnapshot(`
[Error: Invalid routeConfig (Path must be a string and component is required)
{"component":"hello/world.js"}]
`);

const routeConfigWithoutComponent = {
path: '/hello/world',
} as RouteConfig;

expect(loadRoutes([routeConfigWithoutComponent])).rejects
expect(loadRoutes([routeConfigWithoutComponent], '/')).rejects
.toMatchInlineSnapshot(`
[Error: Invalid routeConfig (Path must be a string and component is required)
{"path":"/hello/world"}]
`);
[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 () => {
Expand All @@ -95,7 +96,7 @@ describe('loadRoutes', () => {
component: 'hello/world.js',
} as RouteConfig;

const result = await loadRoutes([routeConfig]);
const result = await loadRoutes([routeConfig], '/');
expect(result).toMatchSnapshot();
});
});
2 changes: 1 addition & 1 deletion packages/docusaurus/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export async function load(siteDir: string): Promise<Props> {
routesChunkNames,
routesConfig,
routesPaths,
} = await loadRoutes(pluginsRouteConfigs);
} = await loadRoutes(pluginsRouteConfigs, baseUrl);

const genRegistry = generate(
generatedFilesDir,
Expand Down
9 changes: 6 additions & 3 deletions packages/docusaurus/src/server/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 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 {stringify} from 'querystring';
import {
Expand Down Expand Up @@ -38,15 +38,18 @@ function getModulePath(target: Module): string {
return `${target.path}${queryStr}`;
}

export async function loadRoutes(pluginsRouteConfigs: RouteConfig[]) {
export async function loadRoutes(
pluginsRouteConfigs: RouteConfig[],
baseUrl: string,
) {
const routesImports = [
`import React from 'react';`,
`import ComponentCreator from '@docusaurus/ComponentCreator';`,
];
const registry: {
[chunkName: string]: ChunkRegistry;
} = {};
const routesPaths: string[] = ['404.html'];
const routesPaths: string[] = [normalizeUrl([baseUrl, '404.html'])];
const routesChunkNames: {
[routePath: string]: ChunkNames;
} = {};
Expand Down