This is the repository for the worker functions that back the Threads for Slack app. It is built and deployed with Cloudflare Workers.
This project uses npm to manage dependencies and provide scripts for deploying the worker to Cloudflare.
Install the dependencies with:
npm install
To deploy the worker, you need a Cloudflare workers account - sign up here. The signup process will guide you through choosing a workers.dev subdomain.
Once you have signed up, you need to authenticate your local machine with your account. Do this by running
npm run wrangler login
This will save an API key to your ~/.wranger
directory, and will be used automatically when deploying the worker to Cloudflare.
The project requires a wrangler.toml
file to build and deploy the worker to Cloudflare.
To get started:
- copy the
wrangler.toml.example
file towrangler.toml
You'll need to fill in the account_id
value. This can be found on the Cloudflare workers dashboard:
- Log in to your Cloudflare workers account
- Select your worker subdomain
- Copy the Account ID from the right hand pane, and paste it into the
toml
file
During development, you can deploy the worker to Cloudflare's network and access it through localhost. This is achieved by running the command:
npm run dev
To publish the worker to production, you have 2 options.
Cloudflare maintains the workers.dev
domain, and you can push your worker to a subdomain hosted on this network. To do this, edit the wrangler.toml
file:
- under
[env.prod]
, setworkers_dev = true
Then run the following command in the terminal
npm run publish
You can now access the worker at https://app.{your-worker-subdomain}.workers.dev
.
If you prefer to deploy to a custom domain, follow these instructions.
- If you haven't already done so, register a new domain at your favourite domain registrar. Make sure to use a registrar which supports setting custom DNS nameservers, options here include Namecheap and iwantmyname, amongst others.
- Log in to the main Cloudflare dashboard.
- In the dashboard, create a new site with your new domain name.
- Back in your domain registrar, change your nameservers to point to Cloudflare.
At this point, you will need to wait for the nameserver changes to propagate, which can take a few hours. You'll get an email from Cloudflare once it has detected that it is in control of the DNS for this domain - at this point, you can continue.
Here, we'll set up some DNS records to point your custom domain to the Cloudflare worker.
Since Cloudflare is in control of both the DNS and the worker, the DNS records are essentially 'dummy' records, simply pointing your domain to Cloudflare's own internal network.
- Go to the DNS panel in your Cloudflare dashboard.
- Add the following records:
- Type: A, Name: @, Content: 192.0.2.1, TTL: Auto
- Type: AAAA, Name: @, Content: 100::, TTL: Auto
Note: if you want to deploy the worker to a subdomain, change the
@
value to your subdomain - for example, to deploy tohttps://worker.example.com
, enter the valueworker
in the Name field.
We now need to update the wrangler.toml
file to deploy the worker to your new Cloudflare-managed domain. To do this:
- Go to the Cloudflare dashboard and select your site.
- Make a note of the Zone ID under the API section.
- In the
wrangler.toml
file, edit the following values under the[env.prod]
section:
- Change the
zone_id
value to the ones copied from the Cloudflare dashboard - Enter the following route:
https://{your-custom-domain}/*
You can now deploy to Cloudflare's network with
npm run publish
Check you can access your site by opening a web browser at https://{your-domain}/
.
If you can't access the worker on your custom domain, your DNS cache may need to be flushed. On a mac, you can do this with the following terminal command:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder