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
5 changes: 5 additions & 0 deletions packages/script/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ export async function registry(resolve?: (path: string) => Promise<string>): Pro
envDefaults: { domain: '' },
bundle: {
resolve: (options?: PlausibleAnalyticsInput) => {
// Self-hosted Plausible: when a custom `scriptInput.src` is provided,
// bundle from that origin instead of the default plausible.io CDN.
const userSrc = (options as any)?.scriptInput?.src
if (typeof userSrc === 'string' && userSrc.trim().length > 0)
return userSrc.trim()
if (options?.scriptId)
return `https://plausible.io/js/pa-${options.scriptId}.js`
const extensions = Array.isArray(options?.extension) ? options.extension.join('.') : [options?.extension]
Expand Down
40 changes: 40 additions & 0 deletions test/unit/plausible-bundle-resolve.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { describe, expect, it } from 'vitest'
import { getBundleResolve, registry } from '../../packages/script/src/registry'

async function getPlausibleResolve() {
const all = await registry()
const script = all.find(s => s.registryKey === 'plausibleAnalytics')!
const resolve = getBundleResolve(script)
if (!resolve)
throw new Error('plausibleAnalytics bundle.resolve missing')
return resolve
}

describe('plausibleAnalytics bundle.resolve', () => {
it('returns default plausible.io URL with scriptId', async () => {
const resolve = await getPlausibleResolve()
expect(resolve({ scriptId: 'abc123' } as any)).toBe('https://plausible.io/js/pa-abc123.js')
})

it('returns default plausible.io URL with legacy extension', async () => {
const resolve = await getPlausibleResolve()
expect(resolve({ extension: 'hash' } as any)).toBe('https://plausible.io/js/script.hash.js')
})

it('returns default basic plausible.io URL with no options', async () => {
const resolve = await getPlausibleResolve()
expect(resolve(undefined)).toBe('https://plausible.io/js/script.js')
})

it('honors user-supplied scriptInput.src for self-hosted Plausible', async () => {
// Regression test for https://github.com/nuxt/scripts/issues/768
const resolve = await getPlausibleResolve()
const selfHosted = 'https://my-self-hosted-plausible.io/js/script.js'
expect(
resolve({ scriptId: 'abc123', scriptInput: { src: selfHosted } } as any),
).toBe(selfHosted)
expect(
resolve({ scriptInput: { src: selfHosted } } as any),
).toBe(selfHosted)
})
})
Loading