Skip to content

Commit

Permalink
fix: toolbar wildcard cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Oct 28, 2024
1 parent 70917b9 commit ae33a7a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
11 changes: 8 additions & 3 deletions frontend/src/toolbar/stats/currentPageLogic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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'
)
})
})
})
14 changes: 10 additions & 4 deletions frontend/src/toolbar/stats/currentPageLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit ae33a7a

Please sign in to comment.