Skip to content

Commit 7b05c8e

Browse files
feat(vite-plugin): Add support for email and cron triggers (#10416)
Add support for testing [Cron Triggers](https://developers.cloudflare.com/workers/configuration/cron-triggers/#test-cron-triggers-locally) and [Email Workers](https://developers.cloudflare.com/email-routing/email-workers/local-development/) in `vite dev` and `vite preview`. * feat(vite-plugin): Add support for email triggers * feedback fixes * feat(vite-plugin): add cron triggers playground * fix tests * feedback suggestions Co-authored-by: James Opstad <[email protected]> * Update packages/vite-plugin-cloudflare/playground/cron-triggers/wrangler.jsonc Co-authored-by: James Opstad <[email protected]> * feedback fixes * feedback fixes <3 Dario * fix lock file * fix tests * fix indentation --------- Co-authored-by: James Opstad <[email protected]>
1 parent e81c2cf commit 7b05c8e

File tree

22 files changed

+335
-2
lines changed

22 files changed

+335
-2
lines changed

.changeset/wide-donuts-sin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/vite-plugin": minor
3+
---
4+
5+
Add support for testing [Cron Triggers](https://developers.cloudflare.com/workers/configuration/cron-triggers/#test-cron-triggers-locally) and [Email Workers](https://developers.cloudflare.com/email-routing/email-workers/local-development/) in `vite dev` and `vite preview`.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { expect, test } from "vitest";
2+
import { getTextResponse, serverLogs } from "../../__test-utils__";
3+
4+
test("Supports testing Cron Triggers at '/cdn-cgi/handler/scheduled' route", async () => {
5+
const cronResponse = await getTextResponse("/cdn-cgi/handler/scheduled");
6+
expect(cronResponse).toBe("ok");
7+
expect(serverLogs.info.join()).toContain("Cron processed");
8+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "@playground/cron-trigger-worker",
3+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"build": "vite build",
7+
"check:type": "tsc --build",
8+
"dev": "vite dev",
9+
"preview": "vite preview"
10+
},
11+
"devDependencies": {
12+
"@cloudflare/vite-plugin": "workspace:*",
13+
"@cloudflare/workers-tsconfig": "workspace:*",
14+
"@cloudflare/workers-types": "catalog:default",
15+
"typescript": "catalog:default",
16+
"vite": "catalog:vite-plugin",
17+
"wrangler": "workspace:*"
18+
}
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { WorkerEntrypoint } from "cloudflare:workers";
2+
3+
export default class extends WorkerEntrypoint {
4+
override async fetch(): Promise<Response> {
5+
return new Response("Hello cron trigger Worker playground!");
6+
}
7+
8+
override async scheduled() {
9+
console.log("Cron processed");
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"files": [],
3+
"references": [
4+
{ "path": "./tsconfig.node.json" },
5+
{ "path": "./tsconfig.worker.json" }
6+
]
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": ["@cloudflare/workers-tsconfig/base.json"],
3+
"include": ["vite.config.ts", "__tests__"]
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": ["@cloudflare/workers-tsconfig/worker.json"],
3+
"include": ["src"]
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"$schema": "http://turbo.build/schema.json",
3+
"extends": ["//"],
4+
"tasks": {
5+
"build": {
6+
"outputs": ["dist/**"]
7+
}
8+
}
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { cloudflare } from "@cloudflare/vite-plugin";
2+
import { defineConfig } from "vite";
3+
4+
export default defineConfig({
5+
plugins: [cloudflare({ inspectorPort: false, persistState: false })],
6+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "cron-trigger-worker",
3+
"main": "src/index.ts",
4+
"compatibility_date": "2025-02-14",
5+
"triggers": {
6+
"crons": ["* * * * *"], // every minute
7+
},
8+
}

0 commit comments

Comments
 (0)