Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ dist
.vscode
.vercel
.claude
.env*.local
17 changes: 17 additions & 0 deletions packages/web/src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,13 @@ describe('utils', () => {
).toBe(scriptSrc);
});

it('adds leading slash to config value', () => {
const scriptSrc = `${Math.random()}.js`;
expect(
loadProps({}, JSON.stringify({ analytics: { scriptSrc } })).src,
).toBe(`/${scriptSrc}`);
});

it('uses props over config string', () => {
const scriptSrc = `https://example.com/${Math.random()}.js`;
expect(
Expand All @@ -280,6 +287,16 @@ describe('utils', () => {
).src,
).toBe(scriptSrc);
});

it('adds leading slash to props value', () => {
const scriptSrc = `${Math.random()}.js`;
expect(
loadProps(
{ scriptSrc },
JSON.stringify({ analytics: { scriptSrc: 'notused' } }),
).src,
).toBe(`/${scriptSrc}`);
});
});

describe('dataset', () => {
Expand Down
20 changes: 14 additions & 6 deletions packages/web/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ function escapeRegExp(string: string): string {

function getScriptSrc(props: AnalyticsProps & { basePath?: string }): string {
if (props.scriptSrc) {
return props.scriptSrc;
return makeAbsolute(props.scriptSrc);
}
if (isDevelopment()) {
return 'https://va.vercel-scripts.com/v1/script.debug.js';
}
if (props.basePath) {
return `${props.basePath}/insights/script.js`;
return makeAbsolute(`${props.basePath}/insights/script.js`);
}
return '/_vercel/insights/script.js';
}
Expand Down Expand Up @@ -166,13 +166,13 @@ export function loadProps(
dataset.disableAutoTrack = '1';
}
if (props.viewEndpoint) {
dataset.viewEndpoint = props.viewEndpoint;
dataset.viewEndpoint = makeAbsolute(props.viewEndpoint);
}
if (props.eventEndpoint) {
dataset.eventEndpoint = props.eventEndpoint;
dataset.eventEndpoint = makeAbsolute(props.eventEndpoint);
}
if (props.sessionEndpoint) {
dataset.sessionEndpoint = props.sessionEndpoint;
dataset.sessionEndpoint = makeAbsolute(props.sessionEndpoint);
}
if (isDevelopment() && props.debug === false) {
dataset.debug = 'false';
Expand All @@ -184,7 +184,7 @@ export function loadProps(
if (props.endpoint) {
dataset.endpoint = props.endpoint;
} else if (props.basePath) {
dataset.endpoint = `${props.basePath}/insights`;
dataset.endpoint = makeAbsolute(`${props.basePath}/insights`);
}

return {
Expand All @@ -193,3 +193,11 @@ export function loadProps(
dataset,
};
}

function makeAbsolute(url: string): string {
return url.startsWith('http://') ||
url.startsWith('https://') ||
url.startsWith('/')
? url
: `/${url}`;
}
5 changes: 4 additions & 1 deletion packages/web/src/vue/create-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export function createComponent(
const route = useRoute();
inject(
{
...props,
// trim out undefined values to avoid overriding config values
...Object.fromEntries(
Object.entries(props).filter(([_, v]) => v !== undefined),
),
basePath: getBasePath(),
// keep auto-tracking unless we have route support (Nuxt or vue-router).
disableAutoTrack: Boolean(route),
Expand Down