Skip to content

The open alternative to Salesforce, designed for AI.

fix(shared): keep trailing slashes inside the query or hash in normalizeUrlOrigin (#28421)

Charles Bochet78e014cb5f0a
diff --git a/packages/twenty-shared/src/utils/url/__tests__/normalizeUrlOrigin.test.ts b/packages/twenty-shared/src/utils/url/__tests__/normalizeUrlOrigin.test.ts
index a6b83faf93d..c9c639fa7f1 100644
--- a/packages/twenty-shared/src/utils/url/__tests__/normalizeUrlOrigin.test.ts
+++ b/packages/twenty-shared/src/utils/url/__tests__/normalizeUrlOrigin.test.ts
@@ -28,6 +28,16 @@ describe('normalizeUrlOrigin', () => {
input: 'https://www.example.com/',
expected: 'https://www.example.com',
},
+ {
+ title: 'should keep a trailing slash that belongs to a query value',
+ input: 'https://example.com/login?redirect=/',
+ expected: 'https://example.com/login?redirect=/',
+ },
+ {
+ title: 'should keep a trailing slash that belongs to a hash fragment',
+ input: 'https://app.example.com/#/projects/',
+ expected: 'https://app.example.com/#/projects/',
+ },
{
title: 'should handle query parameters',
input: 'htTps://wwW.exAmple.coM/TEST?Param=Value',
diff --git a/packages/twenty-shared/src/utils/url/normalizeUrlOrigin.ts b/packages/twenty-shared/src/utils/url/normalizeUrlOrigin.ts
index 4e50170f515..8aebde11eb8 100644
--- a/packages/twenty-shared/src/utils/url/normalizeUrlOrigin.ts
+++ b/packages/twenty-shared/src/utils/url/normalizeUrlOrigin.ts
@@ -1,3 +1,5 @@
+import { isNonEmptyString } from '@sniptt/guards';
+
import { getUrlSafely } from '@/utils/getUrlSafely';
import { isDefined } from '@/utils/validation';
@@ -9,5 +11,13 @@ export const normalizeUrlOrigin = (rawUrl: string) => {
return rawUrl;
}
- return (url.origin + url.pathname + url.search + url.hash).replace(/\/$/, '');
+ const href = url.origin + url.pathname + url.search + url.hash;
+ const hasQueryOrHash =
+ isNonEmptyString(url.search) || isNonEmptyString(url.hash);
+
+ if (hasQueryOrHash) {
+ return href;
+ }
+
+ return href.replace(/\/$/, '');
};