Skip to content

Commit 6ae03dd

Browse files
icd2k3jschrader-nr
andauthored
fix: handle extra slashes in urls (#5)
* fix: handle extra slashes in urls * chore: bump version Co-authored-by: Justin Schrader <[email protected]>
1 parent 9b3652e commit 6ae03dd

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": "use-react-router-breadcrumbs",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "A hook for displaying and setting breadcrumbs for react router",
55
"main": "dist/cjs/index.js",
66
"module": "dist/es/index.js",

src/index.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,11 @@ describe('use-react-router-breadcrumbs', () => {
396396
expect(forwardedProps).toEqual('prop forwarding works');
397397
});
398398
});
399+
400+
describe('Edge cases', () => {
401+
it('Should handle 2 slashes in a URL (site.com/sandwiches//tuna)', () => {
402+
const { breadcrumbs } = render({ pathname: '/sandwiches//tuna' });
403+
expect(breadcrumbs).toBe('Home / Sandwiches / Tuna');
404+
});
405+
});
399406
});

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)