# This is a combination of 3 commits.
# This is the 1st commit message: fix: redirects + update root cdn url # This is the commit message #2: chore: workflow # This is the commit message #3: chore: naming
This commit is contained in:
parent
ed88631875
commit
88a4ec4acd
45 changed files with 1122 additions and 133 deletions
36
workers/analytics/index.js
Normal file
36
workers/analytics/index.js
Normal file
|
@ -0,0 +1,36 @@
|
|||
const ScriptName = '/js/script.js';
|
||||
const Endpoint = '/api/event';
|
||||
|
||||
const ScriptWithoutExtension = ScriptName.replace('.js', '')
|
||||
|
||||
addEventListener('fetch', event => {
|
||||
event.passThroughOnException();
|
||||
event.respondWith(handleRequest(event));
|
||||
})
|
||||
|
||||
async function handleRequest(event) {
|
||||
const pathname = new URL(event.request.url).pathname
|
||||
const [baseUri, ...extensions] = pathname.split('.')
|
||||
|
||||
if (baseUri.endsWith(ScriptWithoutExtension)) {
|
||||
return getScript(event, extensions)
|
||||
} else if (pathname.endsWith(Endpoint)) {
|
||||
return postData(event)
|
||||
}
|
||||
return new Response(null, { status: 404 })
|
||||
}
|
||||
|
||||
async function getScript(event, extensions) {
|
||||
let response = await caches.default.match(event.request);
|
||||
if (!response) {
|
||||
response = await fetch("https://plausible.io/js/plausible." + extensions.join("."));
|
||||
event.waitUntil(caches.default.put(event.request, response.clone()));
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
async function postData(event) {
|
||||
const request = new Request(event.request);
|
||||
request.headers.delete('cookie');
|
||||
return await fetch("https://plausible.io/api/event", request);
|
||||
}
|
Reference in a new issue