Cloudflare Workers is a new service that has been developed by Cloudflare to allow for serverless computing across all of their data centers. It is similar to AWS Lambda and GCP Functions, but it has been much faster and cheaper in my experience. After setting up your first worker, a good next step is to deploy it to your own custom domain. In this tutorial, I will show you how to properly configure the DNS records and Cloudflare routes in order to accomplish this. And don’t worry, all steps can be done straight from the Cloudflare Dashboard – no need to use the CLI.
Time needed: 15 minutes.
Setup Cloudflare Workers on a Custom Domain
- Save and Deploy Your Worker
If you haven’t done so already, save and deploy your Cloudflare Worker. Note the workers.dev subdomain – you will need this in the next steps. Mine, for example, is the following: https://worker.aesweb.workers.dev
- Configure the DNS Records on Cloudflare
Go to your site’s Cloudflare dashboard and click on the DNS tab. From here, you will need to add a CNAME record that points to your worker’s worker.dev domain. Make sure that the proxy status is “Proxied”. In the example below, I have set up the worker to be accessible from the domain worker.andressevilla.com.
- Configure a Worker Route
Click on the Workers tab on your site’s Cloudflare dashboard. Click the “Add Route” button. Type your desired custom domain in the “Route” box and select your desired worker in the “Worker” box. Make sure you add an asterisk to the end of the domain if you want the route to work for all subfolders/pages.
And that’s all there is to it! Your worker should now be accessible from your desired domain. If you run into any issues, try the Cloudflare Community Forum.