Skip to content

Commit 22888e3

Browse files
committed
fix: src and endpoint paths do not work when relative (#186)
1 parent 3879f57 commit 22888e3

4 files changed

Lines changed: 36 additions & 7 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ dist
44
.vscode
55
.vercel
66
.claude
7+
.env*.local

packages/web/src/utils.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,13 @@ describe('utils', () => {
271271
).toBe(scriptSrc);
272272
});
273273

274+
it('adds leading slash to config value', () => {
275+
const scriptSrc = `${Math.random()}.js`;
276+
expect(
277+
loadProps({}, JSON.stringify({ analytics: { scriptSrc } })).src,
278+
).toBe(`/${scriptSrc}`);
279+
});
280+
274281
it('uses props over config string', () => {
275282
const scriptSrc = `https://example.com/${Math.random()}.js`;
276283
expect(
@@ -280,6 +287,16 @@ describe('utils', () => {
280287
).src,
281288
).toBe(scriptSrc);
282289
});
290+
291+
it('adds leading slash to props value', () => {
292+
const scriptSrc = `${Math.random()}.js`;
293+
expect(
294+
loadProps(
295+
{ scriptSrc },
296+
JSON.stringify({ analytics: { scriptSrc: 'notused' } }),
297+
).src,
298+
).toBe(`/${scriptSrc}`);
299+
});
283300
});
284301

285302
describe('dataset', () => {

packages/web/src/utils.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ function escapeRegExp(string: string): string {
126126

127127
function getScriptSrc(props: AnalyticsProps & { basePath?: string }): string {
128128
if (props.scriptSrc) {
129-
return props.scriptSrc;
129+
return makeAbsolute(props.scriptSrc);
130130
}
131131
if (isDevelopment()) {
132132
return 'https://va.vercel-scripts.com/v1/script.debug.js';
133133
}
134134
if (props.basePath) {
135-
return `${props.basePath}/insights/script.js`;
135+
return makeAbsolute(`${props.basePath}/insights/script.js`);
136136
}
137137
return '/_vercel/insights/script.js';
138138
}
@@ -166,13 +166,13 @@ export function loadProps(
166166
dataset.disableAutoTrack = '1';
167167
}
168168
if (props.viewEndpoint) {
169-
dataset.viewEndpoint = props.viewEndpoint;
169+
dataset.viewEndpoint = makeAbsolute(props.viewEndpoint);
170170
}
171171
if (props.eventEndpoint) {
172-
dataset.eventEndpoint = props.eventEndpoint;
172+
dataset.eventEndpoint = makeAbsolute(props.eventEndpoint);
173173
}
174174
if (props.sessionEndpoint) {
175-
dataset.sessionEndpoint = props.sessionEndpoint;
175+
dataset.sessionEndpoint = makeAbsolute(props.sessionEndpoint);
176176
}
177177
if (isDevelopment() && props.debug === false) {
178178
dataset.debug = 'false';
@@ -184,7 +184,7 @@ export function loadProps(
184184
if (props.endpoint) {
185185
dataset.endpoint = props.endpoint;
186186
} else if (props.basePath) {
187-
dataset.endpoint = `${props.basePath}/insights`;
187+
dataset.endpoint = makeAbsolute(`${props.basePath}/insights`);
188188
}
189189

190190
return {
@@ -193,3 +193,11 @@ export function loadProps(
193193
dataset,
194194
};
195195
}
196+
197+
function makeAbsolute(url: string): string {
198+
return url.startsWith('http://') ||
199+
url.startsWith('https://') ||
200+
url.startsWith('/')
201+
? url
202+
: `/${url}`;
203+
}

packages/web/src/vue/create-component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ export function createComponent(
1414
const route = useRoute();
1515
inject(
1616
{
17-
...props,
17+
// trim out undefined values to avoid overriding config values
18+
...Object.fromEntries(
19+
Object.entries(props).filter(([_, v]) => v !== undefined),
20+
),
1821
basePath: getBasePath(),
1922
// keep auto-tracking unless we have route support (Nuxt or vue-router).
2023
disableAutoTrack: Boolean(route),

0 commit comments

Comments
 (0)