mirror of
https://github.com/ThatGuySam/doesitarm.git
synced 2026-05-18 06:44:46 -07:00
Keep the raw worker URL pointed at the canonical site
The default worker is published to workers.dev, but fetching that hostname as the origin returns Cloudflare's placeholder page. Redirect the raw workers.dev host to doesitarm.com while keeping the existing pass-through behavior for routed custom hosts. Constraint: workers.dev has no real site origin behind the default worker Rejected: Proxy workers.dev through doesitarm.com | redirect is simpler and avoids presenting duplicate public hosts Confidence: high Scope-risk: narrow Directive: Keep this redirect host-specific so custom-domain worker routes can continue pass-through behavior Tested: npm --prefix doesitarm-default run build Not-tested: Post-deploy live redirect, pending GitHub Actions deployment
This commit is contained in:
parent
d1f49267c0
commit
81b2179d0f
1 changed files with 24 additions and 1 deletions
|
|
@ -16,10 +16,33 @@ addEventListener('fetch', event => {
|
||||||
event.respondWith(handleRequest(event.request))
|
event.respondWith(handleRequest(event.request))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const canonicalHost = 'doesitarm.com'
|
||||||
|
const workersDevHost = 'doesitarm-default.samcarlton.workers.dev'
|
||||||
|
const workerHeaderName = 'x-workers-hello'
|
||||||
|
const workerHeaderValue = 'Hello from Cloudflare Workers'
|
||||||
|
|
||||||
|
function redirectToCanonicalHost(url) {
|
||||||
|
url.protocol = 'https:'
|
||||||
|
url.hostname = canonicalHost
|
||||||
|
|
||||||
|
return new Response(null, {
|
||||||
|
headers: {
|
||||||
|
Location: url.toString(),
|
||||||
|
[workerHeaderName]: workerHeaderValue
|
||||||
|
},
|
||||||
|
status: 302
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Alter Headers - https://developers.cloudflare.com/workers/examples/alter-headers
|
// Alter Headers - https://developers.cloudflare.com/workers/examples/alter-headers
|
||||||
async function handleRequest(request) {
|
async function handleRequest(request) {
|
||||||
// const visitor = ua( process.env.GA_TRACKING_ID )
|
// const visitor = ua( process.env.GA_TRACKING_ID )
|
||||||
|
const url = new URL(request.url)
|
||||||
|
|
||||||
|
if (url.hostname === workersDevHost) {
|
||||||
|
return redirectToCanonicalHost(url)
|
||||||
|
}
|
||||||
|
|
||||||
const response = await fetch(request)
|
const response = await fetch(request)
|
||||||
|
|
||||||
|
|
@ -28,7 +51,7 @@ async function handleRequest(request) {
|
||||||
const newResponse = new Response(response.body, response)
|
const newResponse = new Response(response.body, response)
|
||||||
|
|
||||||
// Add a custom header with a value
|
// Add a custom header with a value
|
||||||
newResponse.headers.append("x-workers-hello", "Hello from Cloudflare Workers")
|
newResponse.headers.append(workerHeaderName, workerHeaderValue)
|
||||||
|
|
||||||
// // Delete headers
|
// // Delete headers
|
||||||
// newResponse.headers.delete("x-header-to-delete")
|
// newResponse.headers.delete("x-header-to-delete")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue