Skip to content

Commit 3e09755

Browse files
fix: normalizePathSlashes should normalize multiple leading slashes (#2186)
1 parent fc66fbe commit 3e09755

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

src/routes.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export function createHref(
8686

8787
const compiledRoute = `${compile(preparedRoute)(params)}${search}`;
8888

89-
if (options.withBasename) {
89+
if (options.withBasename && basename) {
9090
// For SPA links react-router adds basename itself
9191
// It is needed for external links - <a> or uikit <Link>
9292
return normalizePathSlashes(`${basename}/${compiledRoute}`);

src/utils/__test__/index.test.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ describe('normalizePathSlashes', () => {
1616
test('should handle paths with multiple trailing slashes', () => {
1717
expect(normalizePathSlashes('path////')).toBe('path/');
1818
});
19+
test('should handle paths with multiple leading slashes', () => {
20+
expect(normalizePathSlashes('////path')).toBe('/path');
21+
});
1922
test('should handle full paths with normal slashes', () => {
2023
expect(normalizePathSlashes('http://example.com/path/to/resource')).toBe(
2124
'http://example.com/path/to/resource',
@@ -26,9 +29,14 @@ describe('normalizePathSlashes', () => {
2629
'http://example.com/path/to/resource',
2730
);
2831
});
32+
test('should not replace double slashes near protocols (after a colon)', () => {
33+
expect(normalizePathSlashes('http://host.ydb.com')).toBe('http://host.ydb.com');
34+
expect(normalizePathSlashes('https://host.ydb.com')).toBe('https://host.ydb.com');
35+
expect(normalizePathSlashes('grpc://host.ydb.com')).toBe('grpc://host.ydb.com');
36+
});
2937
test('should replace slashes more than two slashes after a colon', () => {
30-
expect(normalizePathSlashes('http://///example.com/path/to/resource')).toBe(
31-
'http://example.com/path/to/resource',
32-
);
38+
expect(normalizePathSlashes('http:////host.ydb.com')).toBe('http://host.ydb.com');
39+
expect(normalizePathSlashes('https://///host.ydb.com')).toBe('https://host.ydb.com');
40+
expect(normalizePathSlashes('grpc://///host.ydb.com')).toBe('grpc://host.ydb.com');
3341
});
3442
});

src/utils/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ export async function wait<T = unknown>(time: number, value?: T): Promise<T | un
1414

1515
export function normalizePathSlashes(path: string) {
1616
// Prevent multiple slashes when concatenating path parts
17-
return path.replaceAll(/([^:])(\/\/+)/g, '$1/');
17+
// (?<!:) - negative lookbehind - ignore parts that start with :
18+
return path.replaceAll(/(?<!:)\/\/+/g, '/');
1819
}

0 commit comments

Comments
 (0)