From 2f9c79dabc841399efb674cfb244c54e3ddd71ca Mon Sep 17 00:00:00 2001 From: XLor Date: Tue, 27 Feb 2024 20:39:22 +0800 Subject: [PATCH] fix(core): resolve plausible host --- packages/core/src/providers/plausible.ts | 10 +++++++++- packages/core/test/generate.test.ts | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/core/src/providers/plausible.ts b/packages/core/src/providers/plausible.ts index 956a44c..b2303d5 100644 --- a/packages/core/src/providers/plausible.ts +++ b/packages/core/src/providers/plausible.ts @@ -12,7 +12,7 @@ export interface PlausibleOptions { } export function Plausible(options: PlausibleOptions): ScriptTag { - const src = options.src ?? `https://plausible.io/js/script.js`; + const src = resolveSrc(options.src ?? `https://plausible.io/js/script.js`); const domain = options.domain; return { @@ -23,3 +23,11 @@ export function Plausible(options: PlausibleOptions): ScriptTag { } }; } + +function resolveSrc(src: string) { + const RE = /https:\/\/(.*)\/js\/script\.js/; + if (RE.test(src)) { + return src; + } + return `https://${src}/js/script.js`; +} diff --git a/packages/core/test/generate.test.ts b/packages/core/test/generate.test.ts index b8f1d17..448faa2 100644 --- a/packages/core/test/generate.test.ts +++ b/packages/core/test/generate.test.ts @@ -98,6 +98,25 @@ describe('Generate script tags', () => { }, ] `); + + expect( + generate({ + plausible: { + src: `plausible.io`, + domain: `garden.onekuma.cn` + } + }) + ).toMatchInlineSnapshot(` + [ + { + "dataset": { + "domain": "garden.onekuma.cn", + }, + "defer": true, + "src": "https://plausible.io/js/script.js", + }, + ] + `); }); it('should generate cloudflare', () => {