Support custom Plausible script path
dependabot-auto-merge / dependabot (pull_request_target) Skipped
Fix PHP code style issues / php-code-styling (push) Successful in 6s
PHPStan / phpstan (push) Successful in 2m32s
run-tests / P8.3 - L11.* - prefer-lowest - ubuntu-latest (push) Failing after 34s
run-tests / P8.4 - L11.* - prefer-lowest - ubuntu-latest (push) Canceled after 0s
run-tests / P8.4 - L11.* - prefer-stable - ubuntu-latest (push) Canceled after 0s
run-tests / P8.3 - L12.* - prefer-lowest - ubuntu-latest (push) Canceled after 0s
run-tests / P8.3 - L12.* - prefer-stable - ubuntu-latest (push) Canceled after 0s
run-tests / P8.4 - L12.* - prefer-lowest - ubuntu-latest (push) Canceled after 0s
run-tests / P8.4 - L12.* - prefer-stable - ubuntu-latest (push) Canceled after 0s
run-tests / P8.3 - L11.* - prefer-stable - ubuntu-latest (push) Canceled after 2s

Add a PLAUSIBLE_SCRIPT config option so site-specific scripts
(js/pa-XXXXX.js) and legacy script extensions can be proxied. The
script is still served locally from /js/script.js and the cache key
is derived from the script URL so changing the script takes effect
immediately.

Closes #14
This commit is contained in:
mathiaspalmqvist committed 2026-11-12 08:27:43 +01:00
1 parent a60e4cbdcf
commit 513ceb61a0
4 files changed
+94 -27

No files matched your search

+48 -18
View File
@@ -1,35 +1,65 @@
<?php
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
it('returns plausible script from cache', function () {
Cache::put('plausible_script_js', 'console.log("Cached script");', now()->addHours(6));
$response = $this->get('/js/script.js');
$response->assertStatus(200)
->assertHeader('Content-Type', 'application/javascript')
->assertSee('Cached script');
});
it('fetches plausible script if not cached', function () {
it('fetches plausible script and caches it', function () {
Http::fake([
config('plausible-proxy.domain').'/js/script.js' => Http::response('console.log("Plausible script");', 200),
'https://plausible.io/js/script.js' => Http::response('console.log("Plausible script");', 200),
]);
$response = $this->get('/js/script.js');
$response->assertStatus(200)
$this->get('/js/script.js')
->assertStatus(200)
->assertHeader('Content-Type', 'application/javascript')
->assertSee('Plausible script');
expect(Cache::get('plausible_script_js'))->toBe('console.log("Plausible script");');
$this->get('/js/script.js')
->assertStatus(200)
->assertSee('Plausible script');
Http::assertSentCount(1);
});
it('fetches a custom script path', function () {
config()->set('plausible-proxy.script', 'js/pa-12345ABCDE.js');
Http::fake([
'https://plausible.io/js/pa-12345ABCDE.js' => Http::response('console.log("Custom script");', 200),
]);
$this->get('/js/script.js')
->assertStatus(200)
->assertSee('Custom script');
Http::assertSent(fn ($request) => $request->url() === 'https://plausible.io/js/pa-12345ABCDE.js');
});
it('handles trailing and leading slashes in domain and script path', function () {
config()->set('plausible-proxy.domain', 'https://plausible.example.com/');
config()->set('plausible-proxy.script', '/js/script.outbound-links.js');
Http::fake([
'https://plausible.example.com/js/script.outbound-links.js' => Http::response('console.log("Outbound");', 200),
]);
$this->get('/js/script.js')->assertStatus(200)->assertSee('Outbound');
});
it('does not serve a cached script after the script path changes', function () {
Http::fake([
'https://plausible.io/js/script.js' => Http::response('console.log("Old script");', 200),
'https://plausible.io/js/pa-12345ABCDE.js' => Http::response('console.log("New script");', 200),
]);
$this->get('/js/script.js')->assertSee('Old script');
config()->set('plausible-proxy.script', 'js/pa-12345ABCDE.js');
$this->get('/js/script.js')->assertSee('New script');
});
it('handles failed script fetch', function () {
Http::fake([
config('plausible-proxy.domain').'/js/script.js' => Http::response('', 500),
'https://plausible.io/js/script.js' => Http::response('', 500),
]);
$response = $this->get('/js/script.js');