Cron handlers resolve void/env to its browser build in Cloudflare deploys

Đang mở
#717 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
48/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
typescript, vite
Lĩnh vực
build-system

Hướng nghiên cứu

Read the deploy wrapper code in dist/wrapper-*.mjs, especially the block that writes .void/handlers/handlers.ts and runs Vite build(), then inspect .void/handlers/out/__void_handlers.js after deployment. Confirm which export of void/env the handler bundle resolves and compare it with the Worker build. Done when deployed cron and queue handlers can read server environment keys, while the existing development and Vitest behavior still works.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

What happens

In a deployed Cloudflare Worker, a cron job that reads a server value through void/env throws:

env: Key 'FOO' is not exposed to the client. Void only exposes VITE_* schema keys to browser code. Either rename the key in env.ts with that prefix, or move this access to a server module.

The same code works under void dev (POST /__void/scheduled) and in Vitest with @cloudflare/vitest-pool-workers, so tests do not catch it. Request handlers in the same Worker read the same key without trouble.

Versions: void 1.3.0 with @tanstack/react-start 1.169.4, D1, direct Cloudflare deploy (void deploy --platform cloudflare). The handler bundling code below is unchanged in 1.6.1.

Reproduce

  1. env.ts: export default defineEnv({ FOO: z.string() }), and set FOO as a Worker secret.

  2. crons/foo.ts:

    import { defineScheduled } from "void";
    import { env } from "void/env";
    
    export const cron = "0 * * * *";
    
    export default defineScheduled(async () => {
      console.log(env.FOO);
    });
    
  3. Deploy, then let the schedule fire, or call POST /__void/scheduled with a CRON_SECRET. The run fails with the error above; the HTTP route returns {"error":"AggregateError: Scheduled jobs failed"}.

Likely cause

The deploy wrapper bundles cron and queue handlers separately (.void/handlers/handlers.ts → .void/handlers/out/__void_handlers.js) with Vite's build() in library mode (dist/wrapper-*.mjs, the block that writes handlers.ts). That build sets no ssr option or resolve conditions, so Vite resolves packages with its default client conditions. void/env's browser export wins, and the bundle inlines dist/runtime/env-public-client.mjs instead of the workerd/worker build, env-public.mjs. The generated bundle contains the client module's "is not exposed to the client" message.

Queue consumers go through the same handler bundle, so they are probably affected too.

Possible fix

Build the handlers bundle with Worker resolve conditions (for example resolve.conditions: ["workerd", "worker", "import", "module", "default"], or as an SSR build targeting webworker). Or alias void/env to the server build, as the build already aliases void to void/handler.

Workaround

In code that cron jobs run, read values from the Worker's bindings with import { env } from "cloudflare:workers", validated with the same schema.

Ngôn ngữ chính
TypeScript
Star
627
Fork
66
Merge trung bình
14 giờ 17 phút
Pull request đã merge (30 ngày)
200

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của voidzero-dev/void

Tất cả issue của voidzero-dev/void

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.