Skip to content

fix(core): keep plugin cron alive in astro dev under Cloudflare adapter - #1636

Merged
ascorbic merged 1 commit into
emdash-cms:mainfrom
MA2153:fix/plugin-cron-undefined-dev-1635
Jun 27, 2026
Merged

ascorbic merged 1 commit into
emdash-cms:mainfrom
MA2153:fix/plugin-cron-undefined-dev-1635

Conversation

@MA2153

@MA2153 MA2153 commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes plugin cron being silently inert during astro dev under the @astrojs/cloudflare adapter.

The scheduler virtual module (virtual:emdash/scheduler) decides whether to inject a NodeCronScheduler based on a command. It was reading Vite's config.command, which the Cloudflare adapter reports as "build" even during astro dev — the adapter produces the worker bundle via a nested Vite build pass. As a result generateSchedulerModule returned createScheduler = null, the middleware skipped setContextFactory({ cronReschedule }), and ctx.cron was left undefined. Plugins could not call ctx.cron.schedule(...), _emdash_cron_tasks stayed empty, and the plugin cron hook never fired.

The fix threads Astro's authoritative command into the virtual-modules plugin and bases the scheduler decision on it (dev → in-process timer, everything else → null under Cloudflare). astro dev now keeps the NodeCronScheduler regardless of how the Cloudflare adapter pre-bundles the worker, so plugin cron, scheduled publishing, and cleanup run again in local dev.

Closes #1635

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (if applicable) — n/a, no admin UI strings in this change
  • I have added a changeset (if this PR changes a published package)
  • New features link to an approved Discussion — n/a, bug fix

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude Opus 4.8 (Claude Code)

Screenshots / test output

Added createVirtualModulesPlugin scheduler wiring tests reproducing the bug (Cloudflare adapter + Astro command dev while Vite reports build). Written test-first: the reproducing test failed (scheduler resolved to null) before the fix, passes after.

 Test Files  1 passed (1)
      Tests  15 passed (15)

🤖 Generated with Claude Code

The scheduler virtual module decided whether to inject a NodeCronScheduler
from Vite's config.command, which the Cloudflare adapter reports as "build"
even during `astro dev` (it bundles the worker via a nested Vite build pass).
That suppressed the in-process timer, leaving ctx.cron undefined so plugins
could not schedule cron tasks and the cron hook never fired.

Base the decision on Astro's authoritative command instead, so local dev
keeps the in-process timer for plugin cron, scheduled publishing, and cleanup.

Closes emdash-cms#1635

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@changeset-bot

changeset-bot Bot commented Jun 26, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 23f440f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 16 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/sandbox-workerd Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/do-demo-site Patch
@emdash-cms/do-solo-demo-site Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
create-emdash Patch
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions Bot added review/needs-review No maintainer or bot review yet area/core size/M labels Jun 26, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jun 26, 2026

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@1636

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@1636

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@1636

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@1636

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@1636

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@1636

emdash

npm i https://pkg.pr.new/emdash@1636

create-emdash

npm i https://pkg.pr.new/create-emdash@1636

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@1636

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@1636

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@1636

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@1636

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@1636

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@1636

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@1636

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@1636

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@1636

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@1636

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@1636

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@1636

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@1636

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@1636

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@1636

commit: 23f440f

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the right fix for the reported bug: the Cloudflare adapter runs a nested Vite build pass during astro dev, so config.command is "build" even though the user is in dev. Threading Astro’s authoritative command into the virtual-modules plugin and mapping only "dev" → "serve" keeps the local Node timer alive for cron, scheduled publishing, and cleanup in that scenario without changing behavior for production Cloudflare builds or non-Cloudflare adapters.

I checked:

  • The scheduler generation logic and how createScheduler is consumed by emdash-runtime.ts to set cronReschedule.
  • The integration wiring in packages/core/src/astro/integration/index.ts; command from astro:config:setup is captured and passed through createViteConfig.
  • All call sites of createVirtualModulesPlugin in the repo (only the one internal call plus the new unit tests).
  • The new test reproduces the exact failure mode (Vite configResolved reports "build" while Astro command is "dev") and asserts the scheduler is non-null.

The change is focused, the existing generateSchedulerModule contract is preserved, test coverage is appropriate, and the changeset is present and well-written. I found no issues.

@github-actions github-actions Bot added review/approved Approved; no new commits since and removed review/needs-review No maintainer or bot review yet labels Jun 27, 2026
@ascorbic

Copy link
Copy Markdown
Member

Thanks!

@ascorbic
ascorbic merged commit d8487f9 into emdash-cms:main Jun 27, 2026
44 of 45 checks passed
@emdashbot emdashbot Bot mentioned this pull request Jun 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core review/approved Approved; no new commits since size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Plugin ctx.cron is undefined under @astrojs/cloudflare in astro dev — plugin cron never schedules or fires

2 participants