Skip to content

Commit fb50b0a

Browse files
authored
fix(plausible): honor scriptInput.src in bundle.resolve for self-hosted (#775)
1 parent 8b486fd commit fb50b0a

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

packages/script/src/registry.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ export async function registry(resolve?: (path: string) => Promise<string>): Pro
301301
envDefaults: { domain: '' },
302302
bundle: {
303303
resolve: (options?: PlausibleAnalyticsInput) => {
304+
// Self-hosted Plausible: when a custom `scriptInput.src` is provided,
305+
// bundle from that origin instead of the default plausible.io CDN.
306+
const userSrc = (options as any)?.scriptInput?.src
307+
if (typeof userSrc === 'string' && userSrc.trim().length > 0)
308+
return userSrc.trim()
304309
if (options?.scriptId)
305310
return `https://plausible.io/js/pa-${options.scriptId}.js`
306311
const extensions = Array.isArray(options?.extension) ? options.extension.join('.') : [options?.extension]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { getBundleResolve, registry } from '../../packages/script/src/registry'
3+
4+
async function getPlausibleResolve() {
5+
const all = await registry()
6+
const script = all.find(s => s.registryKey === 'plausibleAnalytics')!
7+
const resolve = getBundleResolve(script)
8+
if (!resolve)
9+
throw new Error('plausibleAnalytics bundle.resolve missing')
10+
return resolve
11+
}
12+
13+
describe('plausibleAnalytics bundle.resolve', () => {
14+
it('returns default plausible.io URL with scriptId', async () => {
15+
const resolve = await getPlausibleResolve()
16+
expect(resolve({ scriptId: 'abc123' } as any)).toBe('https://plausible.io/js/pa-abc123.js')
17+
})
18+
19+
it('returns default plausible.io URL with legacy extension', async () => {
20+
const resolve = await getPlausibleResolve()
21+
expect(resolve({ extension: 'hash' } as any)).toBe('https://plausible.io/js/script.hash.js')
22+
})
23+
24+
it('returns default basic plausible.io URL with no options', async () => {
25+
const resolve = await getPlausibleResolve()
26+
expect(resolve(undefined)).toBe('https://plausible.io/js/script.js')
27+
})
28+
29+
it('honors user-supplied scriptInput.src for self-hosted Plausible', async () => {
30+
// Regression test for https://github.com/nuxt/scripts/issues/768
31+
const resolve = await getPlausibleResolve()
32+
const selfHosted = 'https://my-self-hosted-plausible.io/js/script.js'
33+
expect(
34+
resolve({ scriptId: 'abc123', scriptInput: { src: selfHosted } } as any),
35+
).toBe(selfHosted)
36+
expect(
37+
resolve({ scriptInput: { src: selfHosted } } as any),
38+
).toBe(selfHosted)
39+
})
40+
})

0 commit comments

Comments
 (0)