Skip to content

feat: allow additional handlers to be exported by adapter-cloudflare #13739

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 5 commits into
base: main
Choose a base branch
from

Conversation

thomasfosterau
Copy link

@thomasfosterau thomasfosterau commented Apr 24, 2025

closes #10117
closes #1712

Cloudflare Workers allows for handlers to be exported from a worker as methods on an object exported as a default export. Currently, adapter-cloudflare-workers exports a fetch handler method, but there is no way to add other handler methods, such as a scheduled or queue handler. This functionality was requested just over a year ago in #10496.

This PR adds a configuration option for @sveltejs/adapter-cloudflare to provide a file whose exports get added to the default option exported by the worker the adapter generates. The fetch handler does not get overridden.


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.

Copy link

changeset-bot bot commented Apr 24, 2025

🦋 Changeset detected

Latest commit: c9cd1de

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

@thomasfosterau
Copy link
Author

This is no longer a draft as all test, lints and checks should now be passing. This PR replaces #13207 as the two Cloudflare adapters have now been merged into one.

The approach here is the same as in #13207: the Cloudflare adapter optionally takes a path to a file which exports anything that can be exported from a Cloudflare Worker, except for the fetch property of the default export.

I have intentionally not followed what the Node adapter does (generate two files: one which exports the SvelteKit handler, and one which runs that handler as a server, allowing users to optionally import the handler and run it in their own server) as I don’t think there is a strong reason to do it for this adapter, given a Cloudflare Worker is just an export handler and not a server itself. Correct me if I’m wrong, but most of the use cases of a custom fetch handler can be realised inside SvelteKit itself, specifically inside the handle server hook or reroute. Any advanced use cases can import the fetch handler from the generated worker if necessary.

@thomasfosterau
Copy link
Author

For reference, the following handlers will work as properties on the default export, alongside fetch:

The following functionality is available as classes which can be exported as named exports from the file:

@thomasfosterau thomasfosterau marked this pull request as ready for review May 10, 2025 03:34
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.

Cloudflare Worker Adapter - Durable Objects
2 participants