Thanks for trying this out! We're really excited about this feature and hope you are too :)
If you haven't used Wrangler to build/publish your worker projects yet, we recommend checking out our Workers Quickstart before going any further.
This feature is in alpha and it almost definitely has bugs and the way you use wrangler dev
in the future may be slightly different than you use it today. Proceed with caution!
$ wrangler dev --help
👂 Start a local server for developing your worker
USAGE:
wrangler dev [OPTIONS]
FLAGS:
--help Prints help information
OPTIONS:
-e, --env <env> environment to build
-h, --host <host> domain to test behind your worker. defaults to example.com
-i, --ip <ip> ip to listsen on. defaults to localhost
-p, --port <port> port to listen on. defaults to 8787
wrangler dev
works very similarly to wrangler preview
except that instead of opening your browser to preview your worker, it will start a server on localhost that will execute your worker on incoming HTTP requests. From there you can use cURL, Postman, your browser, or any other HTTP client to test the behavior of your worker before publishing it.
You should run wrangler dev
from your worker directory, and if your worker makes any requests to a backend, you should specify the host with --host example.com
.
From here you should be able to send HTTP requests to localhost:8787
along with any headers and paths, and your worker should execute as expected. Additionally, you should see console.log messages and exceptions appearing in your terminal. If either of these things don't happen, or you think the output is incorrect, please file an issue!
You should be able to use cURL
, Postman, and any other HTTP client you'd like with this.
wrangler dev
should work with Workers Sites as well! If your site has some sort of configuration for the base url, you'll want to change them to point to localhost:8787
instead of the usual. Then you can just run wrangler dev
with the same configuration options outlined above.
If you'd like for wrangler dev
to listen on something other than localhost
, you can pass something like --ip 127.0.0.1
. This may be useful if you want to test with Docker.
You can also pass --port 8000
if you are already using port 8787
for something else.
If you would like to try something that hasn't been tested yet, you could try to put a worker in front of something you're running on localhost.
Let's say you have a react app that you're developing locally and it's running on localhost:4000
and you want to also develop a worker at the same time (maybe play around with the new HTMLRewriter API a bit). Unfortunately, you cannot call $ wrangler dev --host localhost:4000
because Cloudflare's preview service needs the back end to be a public URL.
What you can do instead is spin up a TryCloudflare endpoint and pass that url to the --host
argument of wrangler dev
and then develop them in tandem.
This is a completely unpaved path that has not been tested at all so your mileage may vary. If you get it to work, we'd love to hear about it!
Hey mate, I just wanted to thank you for this small article. It helped me a lot :)