diff --git a/package.json b/package.json index 3b7b68b..da3f559 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-router-breadcrumbs-hoc", - "version": "3.2.9", + "version": "3.2.10", "description": "small, flexible, higher order component for rendering breadcrumbs with react-router 4.x", "repository": "icd2k3/react-router-breadcrumbs-hoc", "main": "dist/cjs/index.js", diff --git a/src/index.test.js b/src/index.test.js index e642775..013ab09 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -377,4 +377,11 @@ describe('react-router-breadcrumbs-hoc', () => { expect(forwardedProps).toEqual('prop forwarding works'); }); }); + + describe('Edge cases', () => { + it('Should handle 2 slashes in a URL (site.com/sandwiches//tuna)', () => { + const { breadcrumbs } = render({ pathname: '/sandwiches//tuna' }); + expect(breadcrumbs).toBe('Home / Sandwiches / Tuna'); + }); + }); }); diff --git a/src/index.tsx b/src/index.tsx index b0f916f..73538bf 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -199,17 +199,20 @@ export const getBreadcrumbs = ( pathname .split('?')[0] - // Remove trailing slash "/" from pathname. - .replace(/\/$/, '') // Split pathname into sections. .split('/') // Reduce over the sections and call `getBreadcrumbMatch()` for each section. - .reduce((previousSection: string, currentSection: string) => { + .reduce((previousSection: string, currentSection: string, index: number) => { // Combine the last route section with the currentSection. // For example, `pathname = /1/2/3` results in match checks for // `/1`, `/1/2`, `/1/2/3`. const pathSection = !currentSection ? '/' : `${previousSection}/${currentSection}`; + // Ignore trailing slash or double slashes in the URL + if (pathSection === '/' && index !== 0) { + return ''; + } + const breadcrumb = getBreadcrumbMatch({ currentSection, location,