---
name: deploy-and-purge
description: >
  Apply this skill whenever the user asks to deploy the site, push to production,
  or explicitly "deploy and clear the cache." It handles the full Git push,
  Cloudflare Pages deployment tracking, and forcefully purging the Cloudflare Zone
  cache via API to ensure CSS and static assets are immediately invalidated.
metadata:
  author: ACRA Insight
  version: 1.0.0
  platform: cloudflare
---

# Deploy & Cache Purge — Production Standard

Every time the site is deployed, we must ensure the Cloudflare edge cache is forcefully purged so that users never see stale CSS or cached React server components. This skill encodes the exact deployment and cache invalidation sequence.

---

## 1. Commit and Push

First, ensure the local working directory is clean and pushed to the `main` branch. This triggers the Cloudflare Pages auto-build (if connected) or prepares the codebase for manual deployment.

```bash
git add .
git commit -m "Deploy: [Brief summary of changes]"
git push origin main
```

---

## 2. Trigger Deployment

Depending on the user's setup, run the specific Cloudflare Pages deployment script. For OpenNext/Next.js projects, this is usually:

```bash
npm run build:cf
npm run deploy:cf
```

*(If the user relies on Git-triggered auto-deployments, you can skip to Step 3 and wait ~60-90 seconds for the build to finish.)*

---

## 3. Force-Purge the Cloudflare Zone Cache

Cloudflare Pages automatically bursts the cache for the Pages alias (`*.pages.dev`), but custom domains often hold onto stale CSS and assets. You must purge the zone via the Cloudflare API.

**Prerequisites in `.env.local`:**
- `CLOUDFLARE_API_TOKEN`
- `CLOUDFLARE_ZONE_ID`

### If `CLOUDFLARE_ZONE_ID` is missing:
Fetch it using the API token and append it to `.env.local`:

```bash
curl -s -X GET "https://api.cloudflare.com/client/v4/zones" \
     -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
     -H "Content-Type: application/json"
```
*(Extract the `id` from the response for the target domain and save it as `CLOUDFLARE_ZONE_ID`)*

### The Purge Command:

Run this command to purge everything across the zone:

```bash
source .env.local && curl -X POST "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/purge_cache" \
     -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
     -H "Content-Type: application/json" \
     --data '{"purge_everything":true}'
```

**Rules:**
- Always confirm with the user before purging the entire cache on a high-traffic production site, unless they explicitly asked to "clear the cache."
- If the token returns an IP-restriction error (403), inform the user that their Cloudflare API Token restricts IP addresses and they must run the `curl` command themselves from an authorized IP.

---

## 4. Verification

After the purge returns `{"success":true}`, inform the user:
> "Deployment pushed and Cloudflare edge cache fully purged. A hard refresh will show all changes immediately."
