Workers with route configured are not being triggered

I need to do some host rewriting along header updates and was trying to use workers for that. I've configured it to *domain.com/* and enabled 100% of the logs. But so far I've only seem logs when my requests are made directly to the worker, not on domain.com. Any pointers to why they might not being triggered? Here's my code so far:
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
const url = new URL(request.url)

console.log("Hostname:", url.hostname);

url.hostname = 'other_domain.com'

const modifiedRequest = new Request(url, request)
modifiedRequest.headers.set('Host', url.hostname)

return fetch(modifiedRequest)
}

export default {
async fetch(request, env, ctx) {
return handleRequest(request);
},
};
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
const url = new URL(request.url)

console.log("Hostname:", url.hostname);

url.hostname = 'other_domain.com'

const modifiedRequest = new Request(url, request)
modifiedRequest.headers.set('Host', url.hostname)

return fetch(modifiedRequest)
}

export default {
async fetch(request, env, ctx) {
return handleRequest(request);
},
};
6 Replies
Jürgen Leschner
@Bruno Miranda I don't know if this is the root cause of your issue, but from your code above it looks like you have implemented both the old service worker api and the new ESM api for your worker. I would start with removing the eventlistener at the top. https://developers.cloudflare.com/workers/reference/migrate-to-module-workers/
Cloudflare Docs
Migrate from Service Workers to ES Modules | Cloudflare Workers docs
Write your Worker code in ES modules syntax for an optimized experience.
Jürgen Leschner
b.t.w. you might consider using a redirect rule or Origin rules instead of a worker which will be invoked on every request.
Cloudflare Docs
Single Redirects | Cloudflare Rules docs
Single Redirects allow you to create static or dynamic URL redirects. A simple interface with wildcard support makes it easy to define source and target URL patterns without needing complex functions or regular expressions, efficiently handling thousands of URLs with a single rule. Dynamic URL redirects also support advanced features such as str...
Cloudflare Docs
Origin Rules | Cloudflare Rules docs
Origin Rules allow you to customize where the incoming traffic will go and with which parameters. Currently you can perform the following overrides:
Bruno Miranda
Bruno MirandaOP2mo ago
@Jürgen Leschner my first attempt was using redirect and origin rules, however they are failing for *.domain.com requests. Worker came into place as a second possibility. I will try removing that from the worker and testing, thanks for the reply.
a worker which will be invoked on every request
Every request for the route that I setup or every request in my zone?
Jürgen Leschner
the route that targets the worker (I thought you were using a wildcard so all request would be affected)
Bruno Miranda
Bruno MirandaOP2mo ago
Ah gotcha, yeah, I'm using wildcard, but we got other domains on that zone too, I was woried that it would affect those domains as well
Jürgen Leschner
Yeah, based on what you wrote with *domain.com/* I thought you needed all requests for all paths and subdomains to be proxied to the new origin. I would recommend getting redirects to work because that will take the request load off the old origin (but it assumes that the clients can do redirects)
Want results from more Discord servers?
Add your server