chore: normalize formatting for workers

This commit is contained in:
Cory Dransfeldt 2024-10-19 19:53:31 -07:00
parent 2f6cfbe7ae
commit 2cd835d31b
No known key found for this signature in database
14 changed files with 879 additions and 604 deletions

View file

@ -1,39 +1,40 @@
const scriptName = '/js/script.js'
const endpoint = '/api/event'
const scriptName = "/js/script.js";
const endpoint = "/api/event";
addEventListener("fetch", (event) => {
event.passThroughOnException()
event.respondWith(handleRequest(event))
})
event.passThroughOnException();
event.respondWith(handleRequest(event));
});
async function handleRequest(event) {
const url = new URL(event.request.url)
const pathname = url.pathname
const url = new URL(event.request.url);
const pathname = url.pathname;
if (pathname === scriptName) {
return getScript(event)
return getScript(event);
} else if (pathname === endpoint) {
return postData(event)
return postData(event);
}
return new Response(null, { status: 404 })
return new Response(null, { status: 404 });
}
async function getScript(event) {
const cache = caches.default
let response = await cache.match(event.request)
const cache = caches.default;
let response = await cache.match(event.request);
if (!response) {
const scriptUrl =
'https://plausible.io/js/plausible.outbound-links.tagged-events.js'
response = await fetch(scriptUrl)
if (response.ok) event.waitUntil(cache.put(event.request, response.clone()))
"https://plausible.io/js/plausible.outbound-links.tagged-events.js";
response = await fetch(scriptUrl);
if (response.ok)
event.waitUntil(cache.put(event.request, response.clone()));
}
return response
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)
}
const request = new Request(event.request);
request.headers.delete("cookie");
return await fetch("https://plausible.io/api/event", request);
}