Testing a worker linked to routes


We have a CF worker set up in front of our domain using a route trigger. This means that any requests to our domain go through the worker.

However I’m not sure how I can test the worker locally (using wrangler), as when I run the worker locally using wrangler dev, when I navigate to I just see this:

It seems as though the local version of the worker is not forwarding the request to the origin?

How can I run the worker locally so that it behaves in the same way as it does on production?

Many thanks

Try wrangler dev --remote instead?

Hey Erisa,
I did try that but the behaviour is the same. I think it has something to do with the fact that requests to this worker in prod come in via route bindings which is not the case during development.
I’ve found that I can “emulate” the production behaviour by doing something like this:

addEventListener('fetch', (event) => {
  let request = event.request

  // Mimic behaviour of worker in prod
  if (ENVIRONMENT === 'development') {
    const url = new URL(event.request.url)
    url.hostname = 'myproddomain.com'
    request = new Request(url, request)

  event.respondWith(handleAndLogToHoneycomb(event, request))

However, it doesn’t feel right as I can’t truly test what happens in prod without deploying to prod (and potentially breaking all requests to our domain in the process :cold_sweat:)