From 5aca1d739cdf5e86b052105bf781aea2503d306e Mon Sep 17 00:00:00 2001 From: Artem Kovalov Date: Mon, 2 Nov 2020 19:15:43 +0100 Subject: [PATCH] fix(v2): navbar dropdown crash when item.to is undefined (#3662) * Fix for undefined path with dropdown menu in Navbar * Filter out 'href' links and check for undefined in utils * Minot syntax fixes * allow isSamePath to accept undefined values Co-authored-by: slorber --- .../src/__tests__/utils.test.js | 10 ++++++++++ packages/docusaurus-theme-classic/src/utils/index.ts | 9 +++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/docusaurus-theme-classic/src/__tests__/utils.test.js b/packages/docusaurus-theme-classic/src/__tests__/utils.test.js index 3c78736cafd7..bdae0c5259b4 100644 --- a/packages/docusaurus-theme-classic/src/__tests__/utils.test.js +++ b/packages/docusaurus-theme-classic/src/__tests__/utils.test.js @@ -19,4 +19,14 @@ describe('isSamePath', () => { test('should be false for compared path with double trailing slash', () => { expect(isSamePath('/docs', '/docs//')).toBeFalsy(); }); + + test('should be true for twice undefined/null', () => { + expect(isSamePath(undefined, undefined)).toBeTruthy(); + expect(isSamePath(undefined, undefined)).toBeTruthy(); + }); + + test('should be false when one undefined', () => { + expect(isSamePath('/docs', undefined)).toBeFalsy(); + expect(isSamePath(undefined, '/docs')).toBeFalsy(); + }); }); diff --git a/packages/docusaurus-theme-classic/src/utils/index.ts b/packages/docusaurus-theme-classic/src/utils/index.ts index 6575f1fa08c6..d2b769cab124 100644 --- a/packages/docusaurus-theme-classic/src/utils/index.ts +++ b/packages/docusaurus-theme-classic/src/utils/index.ts @@ -6,7 +6,12 @@ */ // Compare the 2 paths, ignoring trailing / -export const isSamePath = (path1, path2) => { - const normalize = (str) => (str.endsWith('/') ? str : `${str}/`); +export const isSamePath = ( + path1: string | undefined, + path2: string | undefined, +) => { + const normalize = (pathname: string | undefined) => { + return !pathname || pathname?.endsWith('/') ? pathname : `${pathname}/`; + }; return normalize(path1) === normalize(path2); };