Skip to content

Commit

Permalink
fix(core): resolve plausible host
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Feb 27, 2024
1 parent 5347368 commit 2f9c79d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/core/src/providers/plausible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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`;
}
19 changes: 19 additions & 0 deletions packages/core/test/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 2f9c79d

Please sign in to comment.