This project is a Cloudflare Worker designed to act as a cron trigger. Its primary purpose is to periodically invoke a Cloudflare Pages Function, specifically one that handles the regeneration of assets for another project.
- Scheduled execution via Cloudflare Cron Triggers.
- Invokes a specified Cloudflare Pages Function URL.
- Configurable via
wrangler.tomland environment variables. - Includes an optional mechanism for securing the trigger endpoint using a shared secret.
PCC-Worker/
├── src/
│ └── index.ts # Main worker script
├── .gitignore # Specifies intentionally untracked files
├── LICENSE # Project license (GPL-3.0)
├── package.json # Project dependencies and scripts
├── pnpm-lock.yaml # PNPM lockfile
├── pnpm-workspace.yaml # PNPM workspace configuration (if part of a monorepo)
├── README.md # This file
├── tsconfig.json # TypeScript configuration
└── wrangler.toml # Wrangler configuration for Cloudflare Worker
- Node.js (preferably a recent LTS version)
- PNPM package manager (https://pnpm.io/)
- A Cloudflare account.
wranglerCLI installed and configured (usually handled as a dev dependency via PNPM).
-
Clone the repository (if you haven't already):
# If applicable, otherwise you already have the project # git clone <repository-url> # cd PCC-Worker
-
Install Dependencies:
pnpm install
-
Configure
wrangler.toml: Open thewrangler.tomlfile and customize the following:-
name: You can change the worker name if desired (e.g.,"my-cron-worker"). -
compatibility_date: Update to a recent date if necessary. -
[vars]section:PAGES_FUNCTION_URL: Crucial. Set this to the full URL of your Cloudflare Pages Function that needs to be triggered. Example:PAGES_FUNCTION_URL = "https://your-pages-project.pages.dev/regenerate-assets"CRON_SECRET(Optional, but Recommended for Security):- If you want to secure the endpoint, uncomment this line and set a strong, unique secret string.
Example:
CRON_SECRET = "your-very-strong-and-secret-key" - You will also need to configure your target Pages Function to expect and verify this secret (e.g., via an
X-Custom-Cron-Secretheader).
- If you want to secure the endpoint, uncomment this line and set a strong, unique secret string.
Example:
-
[triggers]section:crons: Define the cron schedule for when the worker should run. The default is["0 3 * * *"](every day at 3 AM UTC). Refer to the Cloudflare Cron Triggers documentation for syntax.
-
-
(Recommended) Secure your Pages Function: If you've set
CRON_SECRETinwrangler.toml:- In this worker (
src/index.ts): Uncomment theheaderssection in thefetchcall to send the secret:Change to:// headers: { // Uncomment if you use a secret // 'X-Custom-Cron-Secret': env.CRON_SECRET, // },
headers: { 'X-Custom-Cron-Secret': env.CRON_SECRET, },
- In your Cloudflare Pages project (the one hosting the target function):
- Add an environment variable (e.g.,
CRON_SHARED_SECRET) with the same value as theCRON_SECRETin this worker. - Modify your Pages Function (e.g.,
/functions/regenerate-assets.ts) to check for theX-Custom-Cron-Secretheader and validate its value againstcontext.env.CRON_SHARED_SECRET. If it doesn't match or is missing, return an unauthorized response.
- Add an environment variable (e.g.,
- In this worker (
-
Type Checking (Optional but Recommended):
pnpm typecheck
-
Local Development (Simulating Cron): While
wrangler devcan run the worker, cron triggers are not directly simulated locally in the same way they run on Cloudflare. You can test the worker'sfetchlogic by manually triggering it or adapting the script for local testing if needed.# Starts a local server for the worker, but cron won't run automatically pnpm dev -
Deployment: Deploy the worker to your Cloudflare account:
pnpm deploy
Wrangler will use the settings in
wrangler.tomlto deploy and configure the worker, including setting up the cron trigger.
Once deployed, you can monitor the worker's execution and logs through the Cloudflare dashboard:
- Log in to your Cloudflare account.
- Navigate to Workers & Pages.
- Select your worker (e.g.,
pcc-worker-cron-trigger). - Go to the Logs tab to see invocation logs,
console.logoutputs, and any errors. - You can also view trigger history and metrics.
If you wish to contribute, please follow standard Git practices (fork, branch, commit with conventional messages, pull request).
This project is licensed under the GNU General Public License v3.0.