Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function createHref(

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

if (options.withBasename) {
if (options.withBasename && basename) {
// For SPA links react-router adds basename itself
// It is needed for external links - <a> or uikit <Link>
return normalizePathSlashes(`${basename}/${compiledRoute}`);
Expand Down
14 changes: 11 additions & 3 deletions src/utils/__test__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ describe('normalizePathSlashes', () => {
test('should handle paths with multiple trailing slashes', () => {
expect(normalizePathSlashes('path////')).toBe('path/');
});
test('should handle paths with multiple leading slashes', () => {
expect(normalizePathSlashes('////path')).toBe('/path');
});
test('should handle full paths with normal slashes', () => {
expect(normalizePathSlashes('http://example.com/path/to/resource')).toBe(
'http://example.com/path/to/resource',
Expand All @@ -26,9 +29,14 @@ describe('normalizePathSlashes', () => {
'http://example.com/path/to/resource',
);
});
test('should not replace double slashes near protocols (after a colon)', () => {
expect(normalizePathSlashes('http://host.ydb.com')).toBe('http://host.ydb.com');
expect(normalizePathSlashes('https://host.ydb.com')).toBe('https://host.ydb.com');
expect(normalizePathSlashes('grpc://host.ydb.com')).toBe('grpc://host.ydb.com');
});
test('should replace slashes more than two slashes after a colon', () => {
expect(normalizePathSlashes('http://///example.com/path/to/resource')).toBe(
'http://example.com/path/to/resource',
);
expect(normalizePathSlashes('http:////host.ydb.com')).toBe('http://host.ydb.com');
expect(normalizePathSlashes('https://///host.ydb.com')).toBe('https://host.ydb.com');
expect(normalizePathSlashes('grpc://///host.ydb.com')).toBe('grpc://host.ydb.com');
});
});
3 changes: 2 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export async function wait<T = unknown>(time: number, value?: T): Promise<T | un

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