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): normalize location for route matching #2393

Merged
merged 4 commits into from
Mar 23, 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
16 changes: 10 additions & 6 deletions packages/docusaurus/src/client/PendingNavigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import nprogress from 'nprogress';

import clientLifecyclesDispatcher from './client-lifecycles-dispatcher';
import preload from './preload';
import normalizeLocation from './normalizeLocation';

import 'nprogress/nprogress.css';

Expand All @@ -37,19 +38,20 @@ class PendingNavigation extends React.Component {
// If `routeDidChange` is true, means the router is trying to navigate to a new
// route. We will preload the new route.
if (routeDidChange) {
const nextLocation = normalizeLocation(nextProps.location);
this.startProgressBar(delay);
// Save the location first.
this.previousLocation = this.props.location;
this.previousLocation = normalizeLocation(this.props.location);
this.setState({
nextRouteHasLoaded: false,
});

// Load data while the old screen remains.
preload(routes, nextProps.location.pathname)
preload(routes, nextLocation.pathname)
.then(() => {
clientLifecyclesDispatcher.onRouteUpdate({
previousLocation: this.previousLocation,
location: nextProps.location,
location: nextLocation,
});
// Route has loaded, we can reset previousLocation.
this.previousLocation = null;
Expand All @@ -59,7 +61,7 @@ class PendingNavigation extends React.Component {
},
this.stopProgressBar,
);
const {hash} = nextProps.location;
const {hash} = nextLocation;
if (!hash) {
window.scrollTo(0, 0);
} else {
Expand Down Expand Up @@ -94,7 +96,7 @@ class PendingNavigation extends React.Component {
this.clearProgressBarTimeout();
this.progressBarTimeout = setTimeout(() => {
clientLifecyclesDispatcher.onRouteUpdateDelayed({
location: this.props.location,
location: normalizeLocation(this.props.location),
});
nprogress.start();
}, delay);
Expand All @@ -107,7 +109,9 @@ class PendingNavigation extends React.Component {

render() {
const {children, location} = this.props;
return <Route location={location} render={() => children} />;
return (
<Route location={normalizeLocation(location)} render={() => children} />
);
}
}

Expand Down
70 changes: 70 additions & 0 deletions packages/docusaurus/src/client/__tests__/normalizeLocation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* 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.
*/

import normalizeLocation from '../normalizeLocation';

describe('normalizeLocation', () => {
test('rewrite locations with index.html', () => {
expect(
normalizeLocation({
pathname: '/docs/introduction/index.html',
search: '?search=foo',
hash: '#features',
}),
).toEqual({
pathname: '/docs/introduction',
search: '?search=foo',
hash: '#features',
});

expect(
normalizeLocation({
pathname: '/index.html',
search: '',
hash: '#features',
}),
).toEqual({
pathname: '/',
search: '',
hash: '#features',
});
});

test('untouched pathnames', () => {
expect(
normalizeLocation({
pathname: '/docs/introduction',
search: '',
hash: '#features',
}),
).toEqual({
pathname: '/docs/introduction',
search: '',
hash: '#features',
});

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

expect(
normalizeLocation({
pathname: '/',
}),
).toEqual({
pathname: '/',
});
});
});
34 changes: 34 additions & 0 deletions packages/docusaurus/src/client/normalizeLocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 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.
*/

// Memoize previously normalized pathnames.
const pathnames = {};

function normalizeLocation(location) {
if (pathnames[location.pathname]) {
return {
...location,
pathname: pathnames[location.pathname],
};
}

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

if (pathname === '') {
pathname = '/';
}

pathnames[location.pathname] = pathname;

return {
...location,
pathname,
};
}

export default normalizeLocation;