Fix tracker script name route matching
This commit is contained in:
1 parent
0a838649b7
commit
25081d690e
3 files changed
+61
-4
No files matched your search
@@ -0,0 +1,17 @@
|
||||
import { matchesConfiguredPath } from '../match-configured-path';
|
||||
|
||||
test('matches the exact configured path', () => {
|
||||
expect(matchesConfiguredPath('/d.js', 'd.js')).toBe(true);
|
||||
});
|
||||
|
||||
test('does not match unrelated asset paths that only share the suffix', () => {
|
||||
expect(matchesConfiguredPath('/_next/static/chunks/app/dashboard.js', 'd.js')).toBe(false);
|
||||
});
|
||||
|
||||
test('matches paths under the configured base path', () => {
|
||||
expect(matchesConfiguredPath('/umami/d.js', 'd.js', '/umami')).toBe(true);
|
||||
});
|
||||
|
||||
test('normalizes leading slashes in configured paths', () => {
|
||||
expect(matchesConfiguredPath('/script.js', '/script.js')).toBe(true);
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
function normalizePathname(pathname?: string) {
|
||||
if (!pathname) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return `/${pathname.replace(/^\/+/, '')}`;
|
||||
}
|
||||
|
||||
function normalizeBasePath(basePath?: string) {
|
||||
if (!basePath) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return `/${basePath.replace(/^\/+|\/+$/g, '')}`;
|
||||
}
|
||||
|
||||
export function matchesConfiguredPath(
|
||||
pathname: string,
|
||||
configuredPath?: string,
|
||||
basePath?: string,
|
||||
) {
|
||||
const normalizedPathname = normalizePathname(pathname);
|
||||
const normalizedConfiguredPath = normalizePathname(configuredPath);
|
||||
|
||||
if (!normalizedConfiguredPath) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (normalizedPathname === normalizedConfiguredPath) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const normalizedBasePath = normalizeBasePath(basePath);
|
||||
|
||||
return normalizedBasePath
|
||||
? normalizedPathname === `${normalizedBasePath}${normalizedConfiguredPath}`
|
||||
: false;
|
||||
}
|
||||
Reference in new issue
Block a user