Skip to content

Commit 1db3048

Browse files
icd2k3jschrader-nr
andauthored
Fix: Handle double slashes in URL (#106)
* fix: handle double slashes in url * chore: bump version Co-authored-by: Justin Schrader <[email protected]>
1 parent 8e6f61d commit 1db3048

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-router-breadcrumbs-hoc",
3-
"version": "3.2.9",
3+
"version": "3.2.10",
44
"description": "small, flexible, higher order component for rendering breadcrumbs with react-router 4.x",
55
"repository": "icd2k3/react-router-breadcrumbs-hoc",
66
"main": "dist/cjs/index.js",

src/index.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,4 +377,11 @@ describe('react-router-breadcrumbs-hoc', () => {
377377
expect(forwardedProps).toEqual('prop forwarding works');
378378
});
379379
});
380+
381+
describe('Edge cases', () => {
382+
it('Should handle 2 slashes in a URL (site.com/sandwiches//tuna)', () => {
383+
const { breadcrumbs } = render({ pathname: '/sandwiches//tuna' });
384+
expect(breadcrumbs).toBe('Home / Sandwiches / Tuna');
385+
});
386+
});
380387
});

src/index.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,17 +199,20 @@ export const getBreadcrumbs = (
199199

200200
pathname
201201
.split('?')[0]
202-
// Remove trailing slash "/" from pathname.
203-
.replace(/\/$/, '')
204202
// Split pathname into sections.
205203
.split('/')
206204
// Reduce over the sections and call `getBreadcrumbMatch()` for each section.
207-
.reduce((previousSection: string, currentSection: string) => {
205+
.reduce((previousSection: string, currentSection: string, index: number) => {
208206
// Combine the last route section with the currentSection.
209207
// For example, `pathname = /1/2/3` results in match checks for
210208
// `/1`, `/1/2`, `/1/2/3`.
211209
const pathSection = !currentSection ? '/' : `${previousSection}/${currentSection}`;
212210

211+
// Ignore trailing slash or double slashes in the URL
212+
if (pathSection === '/' && index !== 0) {
213+
return '';
214+
}
215+
213216
const breadcrumb = getBreadcrumbMatch({
214217
currentSection,
215218
location,

0 commit comments

Comments
 (0)