Skip to content

Indy-Center/adapter-cloudflare

Repository files navigation

@indy-center/adapter-cloudflare

Fork of @sveltejs/adapter-cloudflare v7.2.8, Workers-only. Fixes the worker output path so users can bring their own src/worker.ts entrypoint for scheduled handlers, RPC WorkerEntrypoint exports, and any other Cloudflare Worker handlers alongside the SvelteKit fetch handler.

License: MIT

What's different

Pages support is removed. This adapter is Workers-only. If you need Cloudflare Pages, use the upstream @sveltejs/adapter-cloudflare.

Worker output path is fixed. Upstream uses wrangler.jsonc's main field as both the build output destination and the deploy entry point, which means any custom main value gets clobbered on vite build. This fork always writes to .svelte-kit/cloudflare/_worker.js regardless of main, so you can safely point main at your own wrapper. This is the fix proposed in sveltejs/kit#14029 and will land properly in Kit 3.

Usage

npm install -D @indy-center/adapter-cloudflare
// svelte.config.js
import adapter from "@indy-center/adapter-cloudflare";

export default {
  kit: { adapter: adapter() },
};

Set main in wrangler.jsonc to your own worker wrapper:

{
  "main": "src/worker.ts",
  "assets": {
    "binding": "ASSETS",
    "directory": ".svelte-kit/cloudflare",
  },
}
// src/worker.ts
import sv from "../.svelte-kit/cloudflare/_worker.js";
import { WorkerEntrypoint } from "cloudflare:workers";

export default {
  fetch: sv.fetch,

  async scheduled(event, env, ctx) {
    // cron logic here
  },
} satisfies ExportedHandler<Env>;

// optional — exposes RPC methods to other Workers via service bindings
export class Website extends WorkerEntrypoint<Env> {
  async myMethod() { ... }
}

Typing env

ambient.d.ts declares a global Env (and App.Platform.env: Env) that picks up bindings from Cloudflare.Env. Augment Cloudflare.Env in your project to type your specific bindings:

// src/app.d.ts
declare global {
  namespace Cloudflare {
    interface Env {
      DB: D1Database;
      MY_KV: KVNamespace;
    }
  }
  // ...your App.Locals etc.
}

export {};

This is necessary because the file generated by wrangler types (worker-configuration.d.ts) does not reliably merge into the global Cloudflare.Env interface in SvelteKit projects. Declaring bindings in app.d.ts is the workaround.

Using $env/dynamic/* from worker code

Your src/worker.ts (and anything it imports) is bundled by wrangler's esbuild, not vite — so SvelteKit's $env/dynamic/private and $env/dynamic/public virtual modules don't resolve. The adapter ships a runtime shim with the same { env } shape; alias $env/dynamic/* to it in wrangler.jsonc and call setEnv from your worker entrypoint before invoking any code that reads from it:

// wrangler.jsonc
{
  "alias": {
    "$env/dynamic/private": "@indy-center/adapter-cloudflare/env-shim",
    "$env/dynamic/public": "@indy-center/adapter-cloudflare/env-shim",
  },
}
// src/worker.ts
import { setEnv } from "@indy-center/adapter-cloudflare/env-shim";
import sv from "../.svelte-kit/cloudflare/_worker.js";

export default {
  fetch: sv.fetch,
  async scheduled(_event, env, _ctx) {
    setEnv(env as unknown as Record<string, string | undefined>);
    // ...now `$env/dynamic/*` reads inside imported modules work
  },
} satisfies ExportedHandler<Env>;

Vite ignores wrangler's alias block, so SvelteKit routes keep using the real $env plugin untouched.

Testing scheduled handlers locally

Miniflare doesn't auto-fire cron triggers. Start wrangler with the test flag and hit the endpoint manually:

npx wrangler dev --test-scheduled
curl "http://localhost:8787/__scheduled?cron=*+*+*+*+*"

Project layout

  • src/worker.js — source for the bundled SvelteKit worker template.
  • files/worker.js — pre-built output of src/worker.js; copied into the SvelteKit build by the adapter.
  • index.js — adapter implementation.
  • utils.js — wrangler config validation.
  • index.d.ts — TypeScript types for adapter options.
  • ambient.d.ts — global App.Platform, Env, and Cloudflare.Env type augmentation.
  • env-shim.js / env-shim.d.ts — runtime shim for $env/dynamic/* when bundled by wrangler.

Local development

npm install
npm run build        # rebuild files/worker.js from src/worker.js
npm run format       # prettier
npm run format:check

Deployment

npm publish

prepublishOnly runs build and format:check automatically. Requires publish access to the @indy-center npm org.

Upstream tracking

Based on @sveltejs/adapter-cloudflare v7.2.8. To pick up upstream changes, diff packages/adapter-cloudflare in the sveltejs/kit repo against the version noted above and apply relevant patches. This fork exists as a stopgap until Kit 3 ships proper support for custom worker entrypoints.

Disclaimer

We are not affiliated with the FAA or any aviation governing body. This software is for flight simulation use on the VATSIM network.

About

Fork of @sveltejs/adapter-cloudflare, Workers-only, with env-shim and custom worker entrypoint support

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors