[Question] How to Debug Workers on a Subdomain

Hello everyone,

I am working on a Cloudflare Worker project, and the worker will be deployed on a subdomain (i.e. the route key in wrangler.toml is set to foo.mydomain.com). However, I am not able to debug it using wrangler dev.

When I visit localhost:8787 wrangler redirects me to my root domain, without triggering the worker. I have tried to visit foo.localhost:8787 after modifying /etc/hosts, but it still doesn’t work.

May I know if there is any way to debug the worker? Or currently I can only debug a worker that is on a root domain/path?

FYI, here is an example of my wrangler.toml

name = "my-worker"
type = "webpack"
account_id = "<account_id>"
workers_dev = false
route = "https://foo.mydomain.com/*"
zone_id = "<zone_id>"

[env.dev]
name = "my-worker-dev"
route = "https://foo-dev.mydomain.com/*"
webpack_config = "webpack.dev.config.js"

Thank you.

Hello,

Thank you for your reply. Unfortunately, the docs doesn’t seem to help.

I’ve run the wrangler dev command in my Terminal and I visit localhost:8787 in my browser as stated on the docs.

Instead of getting the response from the worker, I get redirected to my actual website (e.g. mydomain.com).

If I change the route key in wrangler.toml to a path (e.g. https://mydomain.com/worker/*), it works completely fine, no redirect, and I get the response from my worker script.

May I know what I have missed?

You can debug any enviroment using the flag -e followed by the name you assigned to it:

This should work for you: wrangler dev -e env.dev

and if you need the preview mode use wrangler dev -e env.dev instead.

@escribeme Thank you for your suggestion. However, the command wrangler dev -e env.dev failed with error: Error: ⚠️ Could not find environment with name "env.dev".

While wrangler dev -e dev (without env.) runs, I am still getting the unexpected redirect.

What message do you get when you enter wrangler preview --watch -e dev?

@escribeme Thank you for your help, wrangler preview --watch -e dev did work! I’ve missed this command before. I get the response from my worker using this command.

However, I am still not able to make wrangler dev -e dev work, maybe there is a bug on wrangler…