From ae33a7a2f67c1732a20d3b7a28be219f9e94b3aa Mon Sep 17 00:00:00 2001 From: Paul D'Ambra Date: Mon, 28 Oct 2024 14:01:14 +0000 Subject: [PATCH] fix: toolbar wildcard cleaning --- .../src/toolbar/stats/currentPageLogic.test.ts | 11 ++++++++--- frontend/src/toolbar/stats/currentPageLogic.ts | 14 ++++++++++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/frontend/src/toolbar/stats/currentPageLogic.test.ts b/frontend/src/toolbar/stats/currentPageLogic.test.ts index 3c65d6a224f65..c943d482ebba1 100644 --- a/frontend/src/toolbar/stats/currentPageLogic.test.ts +++ b/frontend/src/toolbar/stats/currentPageLogic.test.ts @@ -8,7 +8,7 @@ describe('current page logic', () => { it('can ignore posthog init hash param when other hash params present', () => { // not technically a valid URL but :shrug: expect(withoutPostHogInit(`https://wat.io?something=a#${posthogInitHashParam}#myfragment`)).toBe( - 'https://wat.io/?something=a#myfragment' + 'https://wat.io?something=a#myfragment' ) }) it('can handle multiple curly braces in the init', () => { @@ -17,15 +17,20 @@ describe('current page logic', () => { withoutPostHogInit( `https://wat.io?something=a#__posthog={something}and something}#myfragment={something}` ) - ).toBe('https://wat.io/?something=a#myfragment={something}') + ).toBe('https://wat.io?something=a#myfragment={something}') }) it('can ignore posthog init hash param when no other hash params present', () => { expect(withoutPostHogInit(`https://wat.io?something=a#${posthogInitHashParam}`)).toBe( - 'https://wat.io/?something=a' + 'https://wat.io?something=a' ) }) it('gives nonsense back if it receives it', () => { expect(withoutPostHogInit('i am not a url')).toBe('i am not a url') }) + it('supports wildcards too', () => { + expect(withoutPostHogInit('https://*.wat.io/category/*/product/1/?something=a#myfragment')).toBe( + 'https://*.wat.io/category/*/product/1/?something=a#myfragment' + ) + }) }) }) diff --git a/frontend/src/toolbar/stats/currentPageLogic.ts b/frontend/src/toolbar/stats/currentPageLogic.ts index 894f0f71c7a16..2aba55673232d 100644 --- a/frontend/src/toolbar/stats/currentPageLogic.ts +++ b/frontend/src/toolbar/stats/currentPageLogic.ts @@ -28,10 +28,16 @@ const replaceWithWildcard = (part: string): string => { */ export function withoutPostHogInit(href: string): string { try { - const url = new URL(href) - url.hash = url.hash.replace(/__posthog=\{[^}]*}[^#]*/, '').replace(/^##/, '#') - url.hash = url.hash === '#' ? '' : url.hash - return url.toString() + // we can't use `new URL(href)` because it behaves differently between browsers + // and e.g. converts `https://*.example.com/` to `https://%2A.example.com/` + const firstHash = href.indexOf('#') + if (firstHash === -1) { + return href + } + return href + .replace(/__posthog=\{[^}]*}[^#]*/, '') + .replace('##', '#') + .replace(/#$/, '') } catch { return href }