diff --git a/.changeset/small-hounds-greet.md b/.changeset/small-hounds-greet.md new file mode 100644 index 000000000000..6e8c6d955e97 --- /dev/null +++ b/.changeset/small-hounds-greet.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-cloudflare': minor +--- + +feat: support custom worker output path for easier integration of cloudflare worker handlers diff --git a/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md b/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md index 0e438e5b0f12..573e5629eb7a 100644 --- a/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md +++ b/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md @@ -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, @@ -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. @@ -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; +``` diff --git a/packages/adapter-cloudflare/index.d.ts b/packages/adapter-cloudflare/index.d.ts index 0c02fb786cee..a40975b5e757 100644 --- a/packages/adapter-cloudflare/index.d.ts +++ b/packages/adapter-cloudflare/index.d.ts @@ -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 { diff --git a/packages/adapter-cloudflare/index.js b/packages/adapter-cloudflare/index.js index 58e95d11736c..82dbe4f62b17 100644 --- a/packages/adapter-cloudflare/index.js +++ b/packages/adapter-cloudflare/index.js @@ -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) {