Skip to content

Commit

Permalink
optimize: use map instead of object
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed Apr 22, 2022
1 parent 12eeedb commit 2b82e53
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/docusaurus/src/client/normalizeLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,28 @@ import routes from '@generated/routes';
import type {Location} from 'history';

// Memoize previously normalized pathnames.
const pathnames: {[rawPathname: string]: string} = {};
const pathnames = new Map<string, string>();

export default function normalizeLocation<T extends Location>(location: T): T {
if (pathnames[location.pathname]) {
if (pathnames.has(location.pathname)) {
return {
...location,
pathname: pathnames[location.pathname],
pathname: pathnames.get(location.pathname),
};
}

// 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);
if (matchedRoutes.some(({route}) => route.exact === true)) {
pathnames[location.pathname] = location.pathname;
pathnames.set(location.pathname, location.pathname);
return location;
}

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

pathnames[location.pathname] = pathname;
pathnames.set(location.pathname, pathname);

return {
...location,
Expand Down

0 comments on commit 2b82e53

Please sign in to comment.