Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/1.docs/50.tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ export default defineTask({
### Platform support

- `dev`, `node-server`, `bun` and `deno-server` presets are supported with [croner](https://croner.56k.guru/) engine.
- `cloudflare_module` preset has native integration with [Cron Triggers](https://developers.cloudflare.com/workers/configuration/cron-triggers/). Make sure to configure wrangler to use the same patterns you define in `scheduledTasks` to be matched.
- `vercel` preset has native integration with [Vercel Cron Jobs](https://vercel.com/docs/cron-jobs). Nitro automatically generates the cron job configuration at build time β€” no manual `vercel.json` setup required.
- `cloudflare_module` preset has native integration with [Cron Triggers](https://developers.cloudflare.com/workers/configuration/cron-triggers/). Nitro automatically generates the cron triggers in the wrangler config at build time - no manual wrangler setup required.
- `vercel` preset has native integration with [Vercel Cron Jobs](https://vercel.com/docs/cron-jobs). Nitro automatically generates the cron job configuration at build time - no manual `vercel.json` setup required.
- More presets (with native primitives support) are planned to be supported!


Expand Down
26 changes: 25 additions & 1 deletion docs/2.deploy/20.providers/cloudflare.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,30 @@ export default defineConfig({
})
```

### Scheduled Tasks (Cron Triggers)

When using [Nitro tasks](/docs/tasks) with `scheduledTasks`, Nitro automatically generates [Cron Triggers](https://developers.cloudflare.com/workers/configuration/cron-triggers/) in the wrangler config at build time.

```ts [nitro.config.ts]
import { defineNitroConfig } from "nitro/config";

export default defineNitroConfig({
preset: "cloudflare_module",
experimental: {
tasks: true,
},
scheduledTasks: {
"* * * * *": ["cms:update"],
"0 15 1 * *": ["db:cleanup"],
},
cloudflare: {
deployConfig: true,
},
})
```

No manual Wrangler configuration is needed - Nitro handles it for you.

## Cloudflare Pages

**Preset:** `cloudflare_pages`
Expand Down Expand Up @@ -321,7 +345,7 @@ From this moment, when running

you will be able to access the `MY_VARIABLE` and `MY_KV` from the request event just as illustrated above.

#### Wrangler environments
#### Wrangler environments

If you have multiple Wrangler environments, you can specify which Wrangler environment to use during Cloudflare dev emulation:

Expand Down
16 changes: 16 additions & 0 deletions src/presets/cloudflare/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,22 @@ export async function writeWranglerConfig(nitro: Nitro, cfTarget: "pages" | "mod
}
}

// Nitro Tasks cron triggers
if (
nitro.options.experimental.tasks &&
Object.keys(nitro.options.scheduledTasks || {}).length > 0 &&
cfTarget !== "pages"
) {
const schedules = Object.keys(nitro.options.scheduledTasks!);
wranglerConfig.triggers = defu(wranglerConfig.triggers, { crons: [] });
const existingCrons = new Set(wranglerConfig.triggers!.crons);
for (const schedule of schedules) {
if (!existingCrons.has(schedule)) {
wranglerConfig.triggers!.crons!.push(schedule);
}
}
}

// Write wrangler.json
await writeFile(wranglerConfigPath, JSON.stringify(wranglerConfig, null, 2), true);

Expand Down
9 changes: 9 additions & 0 deletions test/presets/cloudflare-module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,13 @@ describe("nitro:preset:cloudflare-module", async () => {
const entry = await fsp.readFile(resolve(ctx.outDir, "server", "index.mjs"), "utf8");
expect(entry).toMatch(/export \{.*myScheduled.*\}/);
});

it("should auto-generate cron triggers in wrangler.json", async () => {
const wranglerConfig = await fsp
.readFile(resolve(ctx.outDir, "server", "wrangler.json"), "utf8")
.then((r) => JSON.parse(r));
expect(wranglerConfig.triggers).toEqual({
crons: ["* * * * *"],
});
});
});
Loading