fix(send): keep the root path when REMOVE_TRAILING_SLASH is enabled

The trailing-slash regex also matched the leading slash of the root URL, so with REMOVE_TRAILING_SLASH set every home page view was stored with an empty url_path: the home page vanished from the paths/entry/exit reports and could not be selected as a funnel step. Skip the match at the start of the path so "/" and "/#hash" are preserved while "/blog/" and "/blog/#hash" are still trimmed.

Closes #4152
This commit is contained in:
NoiceHax authored and Francis Cao committed 2026-08-17 11:21:07 -07:00
1 parent d6a3b8c6d3
commit 319a09cdb5
2 files changed
+35 -1

No files matched your search

+33
View File
@@ -482,6 +482,39 @@ describe('event url and referrer parsing', () => {
expect(saveEventMock.mock.calls[0][0]).toMatchObject({ urlPath: '/blog' });
});
test('REMOVE_TRAILING_SLASH keeps the root path as "/"', async () => {
process.env.REMOVE_TRAILING_SLASH = '1';
await callPOST({
type: 'event',
payload: { website: WEBSITE_ID, hostname: 'example.com', url: '/' },
});
expect(saveEventMock.mock.calls[0][0]).toMatchObject({ urlPath: '/' });
});
test('REMOVE_TRAILING_SLASH keeps the root path when the url has a hash', async () => {
process.env.REMOVE_TRAILING_SLASH = '1';
await callPOST({
type: 'event',
payload: { website: WEBSITE_ID, hostname: 'example.com', url: '/#hero' },
});
expect(saveEventMock.mock.calls[0][0]).toMatchObject({ urlPath: '/#hero' });
});
test('REMOVE_TRAILING_SLASH strips a trailing slash before the hash', async () => {
process.env.REMOVE_TRAILING_SLASH = '1';
await callPOST({
type: 'event',
payload: { website: WEBSITE_ID, hostname: 'example.com', url: '/blog/#hero' },
});
expect(saveEventMock.mock.calls[0][0]).toMatchObject({ urlPath: '/blog#hero' });
});
test('maps a "/undefined" pathname to an empty url path', async () => {
await callPOST({
type: 'event',
+2 -1
View File
@@ -214,7 +214,8 @@ export async function POST(request: Request) {
const twclid = currentUrl.searchParams.get('twclid');
if (process.env.REMOVE_TRAILING_SLASH) {
urlPath = urlPath.replace(/\/(?=(#.*)?$)/, '');
// Never strip the root slash, otherwise the home page is saved with an empty path
urlPath = urlPath.replace(/(?!^)\/(?=(#.*)?$)/, '');
}
if (referrer) {