wrangler-legacy icon indicating copy to clipboard operation
wrangler-legacy copied to clipboard

`wrangler dev` runs published worker, not local

Open jamesarosen opened this issue 3 years ago • 9 comments

🐛 Bug report

Describe the bug

I have a worker that I've already published (not via wrangler publish). When I run wrangler dev, I get a local HTTP server that seems to run the published version of the worker, regardless of what my local code looks like.

Reproduce the bug

Create a webpack-style worker

# wrangler.toml
account_id = "2d9f…"
entry-point = "./index.js"
name = "my-worker"
routes = [
  "www.example.com/my-worker/*",
]
type = "webpack"
webpack_config = "./webpack.config.js"
zone_id = "97d7…"
// index.js

// The published version is much more complex, but I've reduced it to debug locally:
addEventListener('fetch', event => {
  console.log(`fetch ${event.request.method} ${event.request.url}`)
  event.respondWith(new Response('OK', { status: 200 }))
})

Run wrangler dev:

$ wrangler dev
👂  Listening on http://127.0.0.1:8787
🌀  Detected changes...
💁  Ignoring stale first change

Make a local request:

$ curl -svo /dev/null http://127.0.0.1:8787
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8787 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:8787
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 307 Temporary Redirect
< date: Fri, 26 Feb 2021 00:14:55 GMT
< content-type: text/plain;charset=UTF-8
< content-length: 24
…

The wrangler dev log shows

[2021-02-25 16:14:55] GET example.com/ HTTP/1.1 307 Temporary Redirect

If I make a minor change, like commenting out the console.log, the wrangler dev log shows

🌀  Detected changes...

But the request always goes to my zone. It never invokes the local worker.

I've also tried running wrangler build both before wrangler dev and while it's running. Neither has any effect.

Expected behavior

When I make a request to http://127.0.0.1:8787, it invokes the worker, prints to the log, and receives a 200 OK.

Environment and versions

Mac OS 11.2

$ wrangler -V
👷 ✨  wrangler 1.13.0

$ node -v
v12.16.3

jamesarosen avatar Feb 26 '21 00:02 jamesarosen

The problem was that "www.example.com/my-worker/*" didn't match the URL since www.example.com doesn't match 127.0.0.1:8787. The only thing that works is removing routes and adding workers_dev = true.

Is there no way to keep routes in my wrangler.toml and yet also support local development?

This doesn't work:

routes = [
  # production:
  "www.example.com/my-worker/*",

  # things I've tried for dev:
  "my-worker.example.workers.dev/my-worker/*",
  "localhost:9000/my-worker/*",
  "127.0.0.1:9000/my-worker/*",
  "/my-worker/*",
]

[dev]
ip = "127.0.0.1"
local_protocol="http"
port=9000

jamesarosen avatar Feb 26 '21 00:02 jamesarosen

Possibly a duplicate of #1546

jamesarosen avatar Feb 26 '21 01:02 jamesarosen

I had the same experience and came up with a work-around. I have my worker published to a subdomain and dev doesn't work correctly, hitting the root domain. Removing the route in the default config and adding a production env fixed it.

Here's my working wrangler.toml:

name = "worker-dev"
type = "javascript"
account_id = "***"
workers_dev = true
zone_id = "***"

[env.staging]
name = "worker-staging"
route = "worker-staging.example.com/*"

[env.production]
name = "worker"
route = "worker.example.com/*"

wrangler publish no longer publishes to the correct domain, so just do wrangler publish -e production instead.

maedox avatar Mar 02 '21 11:03 maedox

That doesn't quite work for me, but it inspired this, which does!

name = "worker-dev"
type = "javascript"
account_id = "***"
workers_dev = true
zone_id = "***"

[env.development]
name = "worker-development"
route = "*"

[env.staging]
name = "worker-staging"
route = "worker-staging.example.com/*"

[env.production]
name = "worker"
route = "worker.example.com/*"

I can then wrangler dev --env=development. I just need to make sure never to do wrangler publish --env=dev.

When I run the worker that way and inspect fetchEvent.request.url, the origin https://myzone.com/ even though my curl is to http://127.0.0.1:8787/.

jamesarosen avatar Mar 03 '21 00:03 jamesarosen

Awesome. Glad it worked out.

I added all my wrangler scripts in package.json to avoid mistakes. Forgetting to add the env is something I would definitely do.

maedox avatar Mar 03 '21 11:03 maedox

This issue has been automatically marked as stale because it has not had recent activity in the last 180 days. It will be closed if no further activity occurs in the next week. Please feel free to comment if you'd like it to remain open, and thank you for your contributions.

stale[bot] avatar Sep 19 '21 20:09 stale[bot]

Bump. There's a good workaround, but this is definitely still a bug.

jamesarosen avatar Sep 20 '21 23:09 jamesarosen

+1 to solve this issue. It is very annoying

danbars avatar Dec 24 '21 21:12 danbars

+1 to solve this

lamhoangtung avatar Apr 12 '22 07:04 lamhoangtung

Thanks for creating this issue! Wrangler v1 is now deprecated and support is only being provided for critical updates or security concerns. As such, we are closing this PR.

New versions of Wrangler are maintained in the workers-sdk repo. If you are running into a similar issue with wrangler v2, please report it or create a new PR in the workers-sdk repo. For more info about wrangler v1 deprecation, please check out our blog post.

JacobMGEvans avatar Feb 27 '23 16:02 JacobMGEvans