Skip to content
5 changes: 5 additions & 0 deletions .changeset/small-hounds-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare': minor
---

feat: support custom worker output path for easier integration of cloudflare worker handlers
33 changes: 33 additions & 0 deletions documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,39 @@ When building for Cloudflare Workers, this adapter expects to find a [Wrangler c
}
```

### Worker entrypoint

Handlers are methods on Workers that can receive and process external inputs, and can be invoked from outside your Worker.

You can define your own custom Worker entrypoint (e.g. `src/worker.ts`) to add additional [Cloudflare Workers Handlers](https://developers.cloudflare.com/workers/runtime-apis/handlers/).

```ts
/// file: src/worker.ts
import sv from '../.svelte-kit/cloudflare/_worker.js';

export default {
async fetch(request, env, ctx): Promise<Response> {
return sv.fetch(request, env, ctx);
},

// other handlers ...
} satisfies ExportedHandler<Env>;
```

```jsonc
/// file: wrangler.jsonc
{
"name": "<any-name-you-want>",
--- "main": ".svelte-kit/cloudflare/_worker.js",---
+++ "main": "src/worker.ts",+++
"compatibility_date": "2025-01-01",
"assets": {
"binding": "ASSETS",
"directory": ".svelte-kit/cloudflare",
}
}
```

### Deployment

You can use the Wrangler CLI to deploy your application by running `npx wrangler deploy` or use the [Cloudflare Git integration](https://developers.cloudflare.com/workers/ci-cd/builds/) to enable automatic builds and deployments on push.
Expand Down
3 changes: 0 additions & 3 deletions packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ export default function (options = {}) {
worker_dest = `${dest}/_worker.js`;
}
} else {
if (wrangler_config.main) {
worker_dest = wrangler_config.main;
}
if (wrangler_config.assets?.directory) {
// wrangler doesn't resolve `assets.directory` to an absolute path unlike
// `main` and `pages_build_output_dir` so we need to do it ourselves here
Expand Down
Loading