Skip to content

Conversation

@BattlefieldDuck
Copy link

@BattlefieldDuck BattlefieldDuck commented Jul 22, 2025

closes #13692
closes #10117
closes #1712

This PR allow users to specify the output path for the generated worker script. This option takes precedence over the main field in the wrangler config, enabling more flexible integration with custom Cloudflare Worker handlers.

The Problem

Currently, @sveltejs/adapter-cloudflare outputs the compiled worker to .svelte-kit/cloudflare/_worker.js, and this is hardcoded as the entrypoint via the main field in wrangler.jsonc. This setup makes it difficult to extend the Worker with custom handlers (e.g., fetch, queue, scheduled, etc.) since the generated file isn't easily imported or wrapped.

After this PR

With this change, you can override the default worker script path and use your own file (e.g., src/worker.ts) as the main entry in the wrangler config, allowing for easier extension and composition.

Example setup

Update 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",
	}
}

Create src/worker.ts:

import sv from '../.svelte-kit/cloudflare/_worker.js'

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

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

You can now extend this file with additional handlers, just like a standard Cloudflare Worker script. For example, you can add queue, scheduled, or custom logic before or after calling sv.fetch().

✅ Result

  • You can define your own worker entrypoint (e.g., src/worker.ts).
  • This enables easy composition with Cloudflare Worker Handlers, such as fetch, queue, scheduled, etc.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

Introduces a new 'workerScriptPath' option to AdapterOptions, allowing users to specify the output directory for the worker script. The implementation prioritizes this option over the 'main' field in the wrangler config, providing more flexibility in worker script placement.
@changeset-bot
Copy link

changeset-bot bot commented Jul 22, 2025

🦋 Changeset detected

Latest commit: 343627b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/adapter-cloudflare Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@svelte-docs-bot
Copy link

@teemingc
Copy link
Member

teemingc commented Jul 22, 2025

Thanks for the PR! Just a heads up that we're exploring a similar route in #14008 which should allow users to specify their own worker and import a request handler from SvelteKit. This follows the same integration pattern which Remix uses with Cloudflare's Vite plugin

@BattlefieldDuck
Copy link
Author

Thanks for the PR! Just a heads up that we're exploring a similar route in #14008 which should allow users to specify their own worker and import a request handler from SvelteKit. This follows the same integration pattern which Remix uses with Cloudflare's Vite plugin

Appreciate you sharing that, and #14008 looks like a solid approach! Thanks for all the work you're putting into it, looking forward to seeing it land.

Eliminates the workerScriptPath option from AdapterOptions and related logic in the Cloudflare adapter. The worker script path is now determined solely by the wrangler config or defaults, simplifying configuration.
Removed documentation for the deprecated `workerScriptPath` option and added guidance on defining a custom Worker entrypoint for Cloudflare Workers. The new section explains how to wrap the SvelteKit handler and configure Wrangler to use a custom entry file, streamlining the process for adding additional Worker handlers.
The fetch function in the Cloudflare Worker example now explicitly returns a Promise<Response>, improving type safety and clarity in TypeScript usage.
@BattlefieldDuck
Copy link
Author

BattlefieldDuck commented Nov 1, 2025

I created an example sveltekit project with Durable Object using this pull request package with minimal code changes.

npm install -D https://pkg.pr.new/sveltejs/kit/@sveltejs/adapter-cloudflare@14029

Check this commit: BattlefieldDuck/sveltekit-durable-object-example@02b56e8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow custom worker script when deploying to Cloudflare Cloudflare Worker Adapter - Durable Objects

2 participants