# 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
5
workers/analytics/README.md
Normal file
5
workers/analytics/README.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
# analytics worker
|
||||
|
||||
```bash
|
||||
wrangler deploy --env production
|
||||
```
|
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);
|
||||
}
|
13
workers/analytics/wrangler.toml
Normal file
13
workers/analytics/wrangler.toml
Normal file
|
@ -0,0 +1,13 @@
|
|||
name = "analytics-worker"
|
||||
main = "./index.js"
|
||||
compatibility_date = "2023-01-01"
|
||||
|
||||
account_id = ""
|
||||
workers_dev = true
|
||||
|
||||
[env.production]
|
||||
name = "analytics-worker-production"
|
||||
routes = [
|
||||
{ pattern = "coryd.dev/js/*", zone_id = "" },
|
||||
{ pattern = "coryd.dev/api/event", zone_id = "" }
|
||||
]
|
Reference in a new issue