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(core): prevent 404 when accessing /page.html #7184

Merged
merged 13 commits into from
Apr 22, 2022
1 change: 1 addition & 0 deletions jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ process.env.TZ = 'UTC';
const ignorePatterns = [
'/node_modules/',
'__fixtures__',
'__mocks__',
'/testUtils.ts',
'/packages/docusaurus/lib',
'/packages/docusaurus-logger/lib',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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.
*/

export default [
{
path: '/page.html',
exact: true,
component: '',
},
{
path: '/docs',
exact: false,
component: '',
routes: [
{
path: '/docs/installation',
exact: true,
component: '',
},
],
},
{
path: '*',
component: '',
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,33 @@ describe('normalizeLocation', () => {
});
});

it('removes html extension', () => {
expect(
normalizeLocation({
pathname: '/docs/installation.html',
}),
).toEqual({
pathname: '/docs/installation',
});
expect(
normalizeLocation({
pathname: '/docs/introduction/foo.html',
search: '',
hash: '#bar',
}),
).toEqual({
pathname: '/docs/introduction/foo',
search: '',
hash: '#bar',
});
});

it('does not strip extension if the route location has one', () => {
expect(normalizeLocation({pathname: '/page.html'})).toEqual({
pathname: '/page.html',
});
});

it('leaves pathnames untouched', () => {
const replaceMock = jest.spyOn(String.prototype, 'replace');

Expand Down Expand Up @@ -72,18 +99,6 @@ describe('normalizeLocation', () => {
});
expect(replaceMock).toBeCalledTimes(1);

expect(
normalizeLocation({
pathname: '/docs/introduction/foo.html',
search: '',
hash: '#bar',
}),
).toEqual({
pathname: '/docs/introduction/foo.html',
search: '',
hash: '#bar',
});

expect(
normalizeLocation({
pathname: '/',
Expand Down
12 changes: 11 additions & 1 deletion packages/docusaurus/src/client/normalizeLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

import {matchRoutes} from 'react-router-config';
import routes from '@generated/routes';
import type {Location} from 'history';

// Memoize previously normalized pathnames.
Expand All @@ -18,8 +20,16 @@ export default function normalizeLocation<T extends Location>(location: T): T {
};
}

// If the location was registered with an `.html` extension, we don't strip it
// away, or it will render to a 404 page.
const matchedRoutes = matchRoutes(routes, location.pathname);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also handle matchRoutes(routes, location.pathname + ".html"); ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't do that for now, because it makes less sense.

/docs/installation.html is a physical file that exists and can be served, so using this as a location makes sense. If the route is /docs/installation, we should try to normalize /docs/installation.html to that.

However, if both the route and the physical file is /docs/installation.html, then /docs/installation doesn't really make sense—it's nowhere to be found. Moreover, I'm worried that this would lead to more edge cases, especially around trailing slash and index.html.

if (matchedRoutes.some(({route}) => route.exact === true)) {
pathnames[location.pathname] = location.pathname;
return location;
}

const pathname =
location.pathname.trim().replace(/\/index\.html$/, '') || '/';
location.pathname.trim().replace(/(?:\/index)?\.html$/, '') || '/';

pathnames[location.pathname] = pathname;

Expand Down
4 changes: 4 additions & 0 deletions website/_dogfooding/_docs tests/dummy.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
---
slug: dummy.html
---

# Just a dummy page
2 changes: 1 addition & 1 deletion website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const config = {
// Dogfood both settings:
// - force trailing slashes for deploy previews
// - avoid trailing slashes in prod
trailingSlash: isDeployPreview,
trailingSlash: false,
Josh-Cena marked this conversation as resolved.
Show resolved Hide resolved
stylesheets: [
{
href: '/katex/katex.min.css',
Expand Down