Simple proxy works in preview, not when published

I’ve eliminated all of my custom code, and just copied from the simple proxy example app.

export async function handleRequest(event: FetchEvent): Promise<Response> {
  console.log(event)
  let request = event.request
  let oldUrl = new URL(request.url)
  oldUrl.hostname = HOST
  request = new Request(oldUrl, request)

  let response = await fetch(request)
  response = new Response(response.body, response)
  return response
}

This should be a simple proxy that just fetches a resource from another URL and serves it. And indeed it works when running preview
wrangler preview --watch works great.
Clicking into the Cloudflare console after deploying and manually doing a get request to a URL works.

However, when making that GET request from behind the actual subdomain I’ve deployed to, every URL besides the root (index.html) throws a 404. What gives? Seems like the example code at least should run just fine.

Check your request logs and see what is being requested…

Here’s code that works for me and I believe does what you want:

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

/**
 * Respond to the request
 * @param {Request} request
 */
async function handleRequest(request) {
  let newURL = new URL(request.url)
  newURL.hostname = 'www.example.com'
  request = new Request(newURL, request)

  return await fetch(request)
}
1 Like

Request logs? I haven’t found anything yet regarding request logs with Cloudflare workers. I did see a recommendation to write logs to a HTTP endpoint such as Sentry.

I copied the code above and got the same result: 404’s for all of the resources served by the endpoint except for index.html. I chose a random site of a favorite db, fauna.com, and all css and js resources served by the root html are returning 404 result when deployed; the same code when using wrangler preview works great.

So, turns out Cloudflare was sending 404 for every resource that wasn’t at the root, before even getting to the worker.

Debugging with my infra buddy, looks like the routing to the worker didn’t have mysubdomain.mydomain.com/* – it was missing the /*

Hey Robert,

Thanks for that, It doesn’t seem to be working for me when running in a part of a site and not the entire subdomain.

e.g it works on blog2.example.com but the route won’t work on example.com/blog. I thought it should do but want to confirm I’m not overlooking something?

Many thanks
Josh