Skip to content

Instantly share code, notes, and snippets.

@EverlastingBugstopper
Last active November 8, 2023 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EverlastingBugstopper/6e964c8919afdb97397c74fcb1097ccf to your computer and use it in GitHub Desktop.
Save EverlastingBugstopper/6e964c8919afdb97397c74fcb1097ccf to your computer and use it in GitHub Desktop.
testing instructions for wrangler dev beta

Testing wrangler dev

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.

DISCLAIMER

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!

Usage

$ 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

Workers

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.

Workers Sites

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.

Advanced

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.

Very Advanced

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!

@Knskan3
Copy link

Knskan3 commented Nov 8, 2023

Hey mate, I just wanted to thank you for this small article. It helped me a lot :)

@EverlastingBugstopper
Copy link
Author

I'm surprised it's still helpful after all these years - especially since Wrangler 2 is a completely new codebase! Hope things work out for you and thank you for the kind note :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment