# edgeWorker

A Cloudflare Worker that handles all requests before they forwarding to the Vercel Origin. There's an HTML cache for all page requests that mean subsequent requests don't need to go to the origin.

## Development

Cloudflare `wrangler` has a dev mode which bundles the edgeWorker locally, pushes it up to Cloudflare, hosting it under a special `songwhip-web-development.songwhip.workers.dev` domain. It then runs a local server on port `8787` which you can use to dev/debug. Requests to `localhost:8787` will hit the remote development Worker but all `console.log()` output will be visible in your terminal.

### Setup

1. Install dependencies: `yarn`
2. Copy `.env.shadow` to `.env` and fill in the values

### Running

1. `yarn dev`
2. Open browser at `localhost:8787` to hit the Worker
3. Observe console output, or optionally hit `[d]` to open a devtools window.

### Limitations

- Automatic rebuild/reload on file change doesn't seem to be working, so when you make a change to a file you'll need to restart the development server manually.
- Devtools doesn't seem to offer much more than prettier/formatted console output. Maybe I'm missing something 🤷‍♂️

## Using a local songwhip-web instance

You can even run edgeWorker completely locally.

1. Start `songwhip-web` locally (`yarn dev` from project root)
2. Change `SONGWHIP_WEB_ORIGIN_ENDPOINT` to point at `http://localhost:3000` (or whatever port you're using).
3. Run `yarn dev` from inside `edgeWorker/`

## How to emergency deploy to production/staging locally

Github Actions deploys a new edgeWorker instance (and fresh/empty KV store) with each production/staging deploy. There are cases if deploying an emergency hot-fix that you may need to deploy from your local machine.

1. Set the `CLOUDFLARE_TOKEN` in `.env`
2. Set all correct env-vars in `.env`
3. Run `yarn deploy:production`

## Why am I seeing 404s for static assets?

You're likely hitting old cached html from a previous deployment. Create a new empty KV development instance.
