Before getting started, you need to install the Cloudflare
wrangler CLI globally.
Next, either use the
edge CLI command or setup manually:
lib/main.dart file to view the minimal HTTP request fetch handler.
Each incoming request to your edge function will be passed to the
fetch handler, which is expected to return a
Response instance. The
fetch handler provides a
Request instance which contains useful information about the incoming request, such as the method, URL, headers, body and more.
Request instance, you are also provided an
Environment instance, enables you to access service bindings (e.g. Durable Objects). Additionally the
ExecutionContext argument enables you to perform actions such as defining functions which can run after a response has been sent to the client.
To run your application locally, use the
wrangler dev --local command from the root of your project:
wrangler dev --local
You can remove the
--localflag to run your code directly on the Cloudflare Worker environment. This is useful for testing the production environment locally, however it will take longer to start up as your code is uploaded.
This command will start a development server where you can access your application, by default at
http://127.0.0.1:8787. Any changes you make to your Dart code will trigger a hot-reload.
To deploy your application to Cloudflare Workers, first remove the
--dev flag from the
- command = "dart run edge build cloudflare --dev"
+ command = "dart run edge build cloudflare"
--dev flag will ensure that your application is built in production mode, which will result in a smaller bundle size and optimized code.
Please upvote this feature request to help remove this manual step!
Next, run the
wrangler publish command from the root of your project: