jayMakes11
jayMakes11
Explore posts from servers
CDCloudflare Developers
Created by jayMakes11 on 6/27/2023 in #pages-help
How to change the domain used for deploy previews?
Is this even possible? Example: My site's name is my-awesome-site but the auto-created cloudflare dev domain for it is my-awesome-site-5kw.pages.dev. I'd like to change it to be my-awesome-site.pages.dev. Is this possible? I initially created my Pages project directly via Wrangler but deleted and recreated it via Git. When creating it via Wrangler I was able to customize this. Now I'm not. Is this a limitation of using the Git integration?
2 replies
CDCloudflare Developers
Created by jayMakes11 on 6/21/2023 in #pages-help
Can't access environment variables in Wrangler dev
I've added environment variables in my CF dashboard. When running wrangler dev I expect to be able to access these in a function (e.g. export async function onRequestGet(context) {console.log(context.env)}). However, the variable I've set in my CF dashboard is always undefined. There's some confusion around whether I need to define these locally (e.g. in a .env file). I've tried this, but this also does not work. I'd prefer to NOT have to define them locally (similiar to the Netlify dev UX, where ones defined in the panel are available locally). Any insight?
6 replies
CDCloudflare Developers
Created by jayMakes11 on 4/3/2023 in #pages-help
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?
2 replies