Page's function to proxy an analytics script (from Plausible.io), getting console MIME related error

Here's the console error I'm getting:
Refused to execute script from '[URL_REDACTED]' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
Refused to execute script from '[URL_REDACTED]' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
Here's the script:
const ScriptName = '/js/script.js'
const Endpoint = '/api/event'

const ScriptWithoutExtension = ScriptName.replace('.js', '')

export async function onRequest({ request, waitUntil }) {
const pathname = new URL(request.url).pathname
const [baseUri, ...extensions] = pathname.split('.')

if (baseUri.endsWith(ScriptWithoutExtension)) {
return getScript(request, waitUntil, extensions)
} else if (pathname.endsWith(Endpoint)) {
return postData(request)
}
return new Response(null, { status: 404 })
}

async function getScript(request, waitUntil, extensions) {
let response = await caches.default.match(request)
if (!response) {
response = await fetch('https://plausible.io/js/plausible.' + extensions.join('.'))
waitUntil(caches.default.put(request, response.clone()))
}
return response
}

async function postData(req) {
const request = new Request(req)
request.headers.delete('cookie')
return await fetch('https://plausible.io/api/event', request)
}
const ScriptName = '/js/script.js'
const Endpoint = '/api/event'

const ScriptWithoutExtension = ScriptName.replace('.js', '')

export async function onRequest({ request, waitUntil }) {
const pathname = new URL(request.url).pathname
const [baseUri, ...extensions] = pathname.split('.')

if (baseUri.endsWith(ScriptWithoutExtension)) {
return getScript(request, waitUntil, extensions)
} else if (pathname.endsWith(Endpoint)) {
return postData(request)
}
return new Response(null, { status: 404 })
}

async function getScript(request, waitUntil, extensions) {
let response = await caches.default.match(request)
if (!response) {
response = await fetch('https://plausible.io/js/plausible.' + extensions.join('.'))
waitUntil(caches.default.put(request, response.clone()))
}
return response
}

async function postData(req) {
const request = new Request(req)
request.headers.delete('cookie')
return await fetch('https://plausible.io/api/event', request)
}
That's a modified version of a Workers script they give in their docs (here: https://plausible.io/docs/proxy/guides/cloudflare). I got it from here in the forum. Any ideas?
1 Reply
jayMakes11
jayMakes11OP2y ago
Thank you! Will test and report back.
Want results from more Discord servers?
Add your server