Skip to content

feat: support custom worker output path for easier integration of cloudflare worker handlers #14029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
62 changes: 62 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 @@ -25,6 +25,7 @@ export default {
adapter: adapter({
// See below for an explanation of these options
config: undefined,
workerScriptPath: undefined,
platformProxy: {
configPath: undefined,
environment: undefined,
Expand All @@ -46,6 +47,12 @@ export default {

Path to your [Wrangler configuration file](https://developers.cloudflare.com/workers/wrangler/configuration/). If you would like to use a Wrangler configuration filename other than `wrangler.jsonc`, `wrangler.json`, or `wrangler.toml` you can specify it using this option.

### workerScriptPath

Specifies the output path of the generated Worker script file (e.g. `.svelte-kit/cloudflare/_worker.js`). By default, the adapter relies on the `main` field in your Wrangler configuration file to determine the Worker entrypoint. Setting this option allows you to control where the adapter emits the compiled Worker script.

This is useful when you want to define your own Worker entrypoint (e.g. `src/index.ts`) that imports and wraps the SvelteKit handler, making it easier to add custom Cloudflare Worker handlers such as fetch, scheduled, or queue.

### platformProxy

Preferences for the emulated `platform.env` local bindings. See the [getPlatformProxy](https://developers.cloudflare.com/workers/wrangler/api/#parameters-1) Wrangler API documentation for a full list of options.
Expand Down Expand Up @@ -227,3 +234,58 @@ assets.binding = "ASSETS"+++
}+++
}
```

## Cloudflare Worker Handlers

The `workerScriptPath` option allows you to control where the adapter outputs the compiled Worker script.

By default, the Cloudflare adapter writes the compiled Worker to `.svelte-kit/cloudflare/_worker.js`, and this file is referenced as the `main` entry in your `wrangler.toml` or `wrangler.jsonc`.

If you want to define your own custom Worker entrypoint (e.g. `src/index.ts`) to add additional handlers like `fetch`, `scheduled`, or `queue`, you can specify the generated script path using this option.

### svelte.config.js

```ts
/// file: svelte.config.js
import adapter from '@sveltejs/adapter-cloudflare';

export default {
kit: {
--- adapter: adapter()---
+++ adapter: adapter({
workerScriptPath: '.svelte-kit/cloudflare/_worker.js'
})+++
}
};
```

### wrangler.toml

```toml
/// file: wrangler.toml
---main = ".svelte-kit/cloudflare/_worker.js"---
+++main = "src/index.ts"+++
```

### wrangler.jsonc

```jsonc
/// file: wrangler.jsonc
{
--- "main": ".svelte-kit/cloudflare/_worker.js",---
+++ "main": "src/index.ts",+++
}
```

### src/index.ts

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

export default {
async fetch(request, env, ctx) {
return sveltekit.fetch(request, env, ctx);
},
} satisfies ExportedHandler<Env>;
```
7 changes: 7 additions & 0 deletions packages/adapter-cloudflare/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ export interface AdapterOptions {
* during development and preview.
*/
platformProxy?: GetPlatformProxyOptions;

/**
* Worker script `_worker.js` output directory.
* If not specified, the adapter will use the `main` field in your
* wrangler file, or default to `_worker.js` in the output directory.
*/
workerScriptPath?: string;
}

export interface RoutesJSONSpec {
Expand Down
4 changes: 3 additions & 1 deletion packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export default function (options = {}) {
worker_dest = `${dest}/_worker.js`;
}
} else {
if (wrangler_config.main) {
if (options.workerScriptPath) {
worker_dest = options.workerScriptPath;
} else if (wrangler_config.main) {
worker_dest = wrangler_config.main;
}
if (wrangler_config.assets?.directory) {
Expand Down
Loading