presets(vercel): support vercel cron#4308
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
📝 WalkthroughWalkthroughThis PR adds native Vercel Cron Jobs support to Nitro. Users can define scheduled tasks in ChangesVercel Cron Tasks Integration
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
commit: |
|
@coderabbitai review |
✅ Action performedReview finished.
|
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
test/presets/vercel.test.ts (1)
21-33: 🏗️ Heavy liftAdd runtime coverage for the new cron handler contract.
These snapshot updates prove that the cron artifacts are emitted, but they never exercise
src/presets/vercel/runtime/cron-handler.ts. Regressions inCRON_SECRETvalidation, missingx-vercel-cron-schedulehandling, orrunCronTasksinvocation would still ship unnoticed.Also applies to: 469-560
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/presets/vercel.test.ts` around lines 21 - 33, Add a runtime test that actually imports and invokes the cron request handler to exercise CRON_SECRET validation, x-vercel-cron-schedule handling, and runCronTasks invocation: in the existing test suite (the "should add route rules to config" area) add new tests that (1) set process.env.CRON_SECRET and call the exported cron handler function with/without the x-vercel-cron-schedule header to assert it accepts valid secrets and rejects missing/invalid ones, (2) mock/spy the runCronTasks function to verify it is called with the expected schedule from the header (and not called on auth failure), and (3) assert the handler returns the correct HTTP responses; reference the cron handler export name and runCronTasks symbol when locating code to call/mock.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/presets/vercel/types.ts`:
- Around line 131-141: The cronHandlerRoute option is consumed verbatim (used in
preset.ts and utils.ts) which allows invalid values like "api/cron"; normalize
or validate it at the API boundary: in the code that reads the option (the
config parsing/merge path that references cronHandlerRoute in preset.ts and
utils.ts) call withLeadingSlash(cronHandlerRoute) to ensure it starts with "/"
or throw a clear config error if the value is not an absolute path (include the
user value in the error message and mention expected format like
"/_vercel/cron"); ensure downstream uses use the normalized value so generated
Vercel cron paths and Nitro handler routes are always valid.
In `@src/presets/vercel/utils.ts`:
- Around line 235-242: Generated cron entries (cronEntries) are blindly merged
into config.crons causing duplicate {path, schedule} entries; update the merge
to deduplicate by comparing path and schedule pairs from
nitro.options.scheduledTasks (cronPath and schedule) before assigning to
config.crons. Locate the block building cronEntries and change the logic to
filter out any entry whose {path, schedule} already exists in config.crons (or
vice versa), e.g., build a Set keying on `${path}|${schedule}` or use
Array.prototype.find to skip duplicates, then assign the combined unique list
back to config.crons.
---
Nitpick comments:
In `@test/presets/vercel.test.ts`:
- Around line 21-33: Add a runtime test that actually imports and invokes the
cron request handler to exercise CRON_SECRET validation, x-vercel-cron-schedule
handling, and runCronTasks invocation: in the existing test suite (the "should
add route rules to config" area) add new tests that (1) set
process.env.CRON_SECRET and call the exported cron handler function with/without
the x-vercel-cron-schedule header to assert it accepts valid secrets and rejects
missing/invalid ones, (2) mock/spy the runCronTasks function to verify it is
called with the expected schedule from the header (and not called on auth
failure), and (3) assert the handler returns the correct HTTP responses;
reference the cron handler export name and runCronTasks symbol when locating
code to call/mock.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 8f5f8fc4-6c37-46c5-b006-94ccb6eb4bcd
📒 Files selected for processing (7)
docs/1.guide/10.tasks.mddocs/2.deploy/20.providers/vercel.mdsrc/presets/vercel/preset.tssrc/presets/vercel/runtime/cron-handler.tssrc/presets/vercel/types.tssrc/presets/vercel/utils.tstest/presets/vercel.test.ts
| /** | ||
| * The route path for the Vercel cron handler endpoint. | ||
| * | ||
| * When `experimental.tasks` and `scheduledTasks` are configured, | ||
| * Nitro registers a cron handler at this path that Vercel invokes | ||
| * on each scheduled cron trigger. | ||
| * | ||
| * @default "/_vercel/cron" | ||
| * @see https://vercel.com/docs/cron-jobs | ||
| */ | ||
| cronHandlerRoute?: string; |
There was a problem hiding this comment.
Normalize or validate cronHandlerRoute at the API boundary.
This new public option is consumed verbatim in both preset.ts and utils.ts. A value like "api/cron" will generate an invalid Vercel cron path and a mismatched Nitro handler route instead of failing fast. Please normalize with withLeadingSlash() or throw an explicit config error when the value is not an absolute path.
As per coding guidelines: "Prefer explicit errors over silent failures in error handling" and "Include actionable context in error messages".
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/presets/vercel/types.ts` around lines 131 - 141, The cronHandlerRoute
option is consumed verbatim (used in preset.ts and utils.ts) which allows
invalid values like "api/cron"; normalize or validate it at the API boundary: in
the code that reads the option (the config parsing/merge path that references
cronHandlerRoute in preset.ts and utils.ts) call
withLeadingSlash(cronHandlerRoute) to ensure it starts with "/" or throw a clear
config error if the value is not an absolute path (include the user value in the
error message and mention expected format like "/_vercel/cron"); ensure
downstream uses use the normalized value so generated Vercel cron paths and
Nitro handler routes are always valid.
| export default eventHandler(async (event) => { | ||
| // Validate CRON_SECRET if set - https://vercel.com/docs/cron-jobs/manage-cron-jobs#securing-cron-jobs | ||
| const cronSecret = process.env.CRON_SECRET; | ||
| if (cronSecret) { |
There was a problem hiding this comment.
We shuld make it required as part of backport (there had been security reports)
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/presets/vercel/runtime/cron-handler.ts (1)
6-6: ⚡ Quick winRemove the inline explanatory comment at Line 6.
This comment narrates what the next lines do; please drop it to match source style for
src/**/*.{ts,js}.As per coding guidelines, "Do not add comments explaining what the line does unless prompted."
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/presets/vercel/runtime/cron-handler.ts` at line 6, Remove the inline explanatory comment that reads "Require and validate CRON_SECRET - https://vercel.com/docs/cron-jobs/manage-cron-jobs#securing-cron-jobs" from the top of the cron-handler module so the code only contains the actual CRON_SECRET validation logic (look for references to CRON_SECRET in cron-handler/cron handling code). Ensure no other formatting or logic changes are made.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/presets/vercel/runtime/cron-handler.ts`:
- Line 6: Remove the inline explanatory comment that reads "Require and validate
CRON_SECRET -
https://vercel.com/docs/cron-jobs/manage-cron-jobs#securing-cron-jobs" from the
top of the cron-handler module so the code only contains the actual CRON_SECRET
validation logic (look for references to CRON_SECRET in cron-handler/cron
handling code). Ensure no other formatting or logic changes are made.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 0de51920-e1a7-4432-b84c-9c423a5fd281
📒 Files selected for processing (2)
src/presets/vercel/runtime/cron-handler.tssrc/presets/vercel/utils.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- src/presets/vercel/utils.ts
|
|
||
| :read-more{title="Securing cron jobs" to="https://vercel.com/docs/cron-jobs/manage-cron-jobs#securing-cron-jobs"} | ||
|
|
||
| To prevent unauthorized access to the cron handler, set a `CRON_SECRET` environment variable in your Vercel project settings. When `CRON_SECRET` is set, Nitro validates the `Authorization` header on every cron invocation. |
There was a problem hiding this comment.
Docs are outdated, it is an essential step now
🔗 Linked issue
Backport of #4030
❓ Type of change
📚 Description
Support Vercel Cron within Nitro Tasks. Automatically creates Vercel Cron configuration based on Nitro scheduled tasks, so fully zero-config when deploying to Vercel.
📝 Checklist