Skip to content

Commit 81b2179

Browse files
committed
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
1 parent d1f4926 commit 81b2179

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

doesitarm-default/src/index.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,33 @@ addEventListener('fetch', event => {
1616
event.respondWith(handleRequest(event.request))
1717
})
1818

19+
const canonicalHost = 'doesitarm.com'
20+
const workersDevHost = 'doesitarm-default.samcarlton.workers.dev'
21+
const workerHeaderName = 'x-workers-hello'
22+
const workerHeaderValue = 'Hello from Cloudflare Workers'
23+
24+
function redirectToCanonicalHost(url) {
25+
url.protocol = 'https:'
26+
url.hostname = canonicalHost
27+
28+
return new Response(null, {
29+
headers: {
30+
Location: url.toString(),
31+
[workerHeaderName]: workerHeaderValue
32+
},
33+
status: 302
34+
})
35+
}
36+
1937

2038
// Alter Headers - https://developers.cloudflare.com/workers/examples/alter-headers
2139
async function handleRequest(request) {
2240
// const visitor = ua( process.env.GA_TRACKING_ID )
41+
const url = new URL(request.url)
42+
43+
if (url.hostname === workersDevHost) {
44+
return redirectToCanonicalHost(url)
45+
}
2346

2447
const response = await fetch(request)
2548

@@ -28,7 +51,7 @@ async function handleRequest(request) {
2851
const newResponse = new Response(response.body, response)
2952

3053
// Add a custom header with a value
31-
newResponse.headers.append("x-workers-hello", "Hello from Cloudflare Workers")
54+
newResponse.headers.append(workerHeaderName, workerHeaderValue)
3255

3356
// // Delete headers
3457
// newResponse.headers.delete("x-header-to-delete")

0 commit comments

Comments
 (0)