"wrangler dev" gives me a 301 redirect to my site's URL...?

I have several workers running at various routes on my site, but it seems I am no longer able to run wrangler dev. When I try to load a Worker via http://127.0.0.1:8787, it just gives me a 301 redirect to my actual website’s homepage.

This even affects a brand-new “Hello worker!” fresh out of a wrangler generate.

The Worker works fine if I actually do publish it, but this makes development and debugging difficult. I am deeply confused.

The worker looks like this:

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

})
/**
 * Respond with hello worker text
 * @param {Request} request
 */
async function handleRequest(request) {
  return new Response('Hello worker!', {
    headers: { 'content-type': 'text/plain' },
  })
}

The wrangler.toml file looks like this, with my account_id and zone_id removed:

name = "my-worker"
type = "javascript"

account_id = "my-account-id-here"
workers_dev = true
zone_id = "my-zone-id-here"
route = "https://www.my-site.com/workers/my-worker/"
compatibility_date = "2022-01-28"

…and the 301 looks like this:

curl -I http://127.0.0.1:8787
HTTP/1.1 301 Moved Permanently
date: Fri, 28 Jan 2022 07:20:20 GMT
connection: keep-alive
cache-control: max-age=3600
expires: Fri, 28 Jan 2022 08:20:20 GMT
location: https://www.my-site.com/
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
report-to: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=XqBvJVvTlzwBg8R7%2FRoRdpw0i0AqgNrfEYMf7na%2FjLTPILe23DcK1%2FjDuV8sqKIVw%2Bb%2FxZ3UHXekAKtyHHDYThi6pwxcg0vl3%2FGyNoc5ek%2BMluShUoOQawqdHIzOlw%3D%3D"}],"group":"cf-nel","max_age":604800}
nel: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
server: cloudflare
cf-ray: 6d4875294bb72d32-ORD
alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400
content-length: 0

What’s going on?

Your route doesn’t match your worker because the worker sees requests on http://my-worker-my-comany.workers.dev.

Try commenting out route / routes when running wrangler dev

1 Like

Thanks so much, that was the problem.

I guess this is a strong argument for using environment variables and only setting a route in production.

For anyone else who may encounter this, if you remove the route and it still doesn’t work, just make sure that your browser has not cached the 301 redirect.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.