-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
feat: allow additional handlers and durable objects #13207
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: 615909a The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
Does exporting handlers other than cc: @dario-piotrowicz @jamesopstad @petebacondarwin
I think "handlers" works well since that's what they're called in the official docs https://developers.cloudflare.com/workers/runtime-apis/handlers/ We should add some documentation on adding these handlers to our Cloudflare adapter docs https://github.com/sveltejs/kit/blob/main/documentation/docs/25-build-and-deploy/70-adapter-cloudflare-workers.md
The |
I’ll admit I hadn’t actually checked because from the Cloudflare documentation I assumed I agree the the Cloudflare documentation doesn’t explicitly say it’s not supported, at least because it does say you can copy-paste any worker into a I can update the PR to remove the functionality from the
I will can write some and include it in this PR, might have to be post-Christmas though.
If this is desirable I can update the PR to do it as well. Given the handlers are just properties on the default export object they were fairly easy to do, the Durable Object classes would just need to be exported using |
Thanks! Take your time.
I think they might also have to be named exports but I haven't tested any of this myself. |
preview: https://svelte-dev-git-preview-kit-13207-svelte.vercel.app/ this is an automated message |
I’ve removed the handlers code from For future reference, the ‘compatibility matrix’ for Workers versus Pages is here: https://developers.cloudflare.com/workers/static-assets/compatibility-matrix/. |
|
||
Path to a file with additional [handlers](https://developers.cloudflare.com/workers/runtime-apis/handlers/) and (optionally) [Durable Objects](https://developers.cloudflare.com/durable-objects/) to be exported from the file the adapter generates. This allows you to, for example, include handlers for scheduled or queue triggers alongside the fetch handler your SvelteKit app. | ||
|
||
> [!NOTE] The adapter expects the `handlers` file to have a default export. If you only want to export a Durable Object, add `export default {};` to the file. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell, the bundler should throw an error at build time if there isn’t a default export in the handlers
file. I can’t see a nice way to handle there not being a default export, even an empty object is a sensible default/fallback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we split the durable objects into a separate file/option to avoid this pitfall?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don’t think so, although it will be a minor annoyance to anyone only using Durable Objects. It will be a documented requirement and there will be an error if the default export is missing.
Removing the requirement of a default export in the future wouldn’t be a breaking change if there was an easy way at build time to check whether there’s a default export and modify the generated worker accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may have to add a check ourselves since the bundling step will be removed and delegated to Cloudflare's wrangler (meaning the error would only surface when deploying).
Is it possible to do something like:
if (handler) {
const module = await import(handler);
if (!('default' in module)) {
throw new Error('The adapter expects the `handlers` file to have a default export. If you only want to export a Durable Object, add `export default {};` to the file.');
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would assume that would throw an error at build time if the handlers file is written in, e.g., TypeScript or if the handlers file references a module not available at build time (e.g., something available in Cloudflare Workers but not Node). Only trivial workers aren't going to have this problem.
I think this is just an unavoidable problem if bundling is delegated to Wrangler. A possible solution would be to parse the handlers file and look for a default export in the AST, but that seems overly complicated.
The pages adapter can also be used when deploying workers with static assets. The official It appears that you must use the pages adapter with static assets because the workers adapter requires the |
@kansson interesting — is that documented anywhere in the SvelteKit docs? I can't see that it is anywhere, and Cloudflare doesn't even seem to explicitly say that's what they're doing in their docs. I'd be interested to know if Cloudflare's approach is accidentally including I can add the exported handlers functionality back to |
We added compatibility with Workers Static Assets to the Cloudflare adapter some weeks ago. It ignores the Pages related files such as _headers when uploading the assets. The Workers Static Assets PR for the Cloudflare Workers adapter is in draft #13072 |
This comment was marked as off-topic.
This comment was marked as off-topic.
LGTM but I'll wait for another maintainer to review this to check if this is the API we want |
It looks like as it stands, this PR won't work with Cloudflare's RPC feature for Workers as it requires the fetch handler to be a method on a class, not an exported object. See: https://developers.cloudflare.com/workers/runtime-apis/rpc/ I'll rewrite this PR to handle this sometime this week. |
It seems like it would still work if we use Class Instances instead of Functions https://developers.cloudflare.com/workers/runtime-apis/rpc/#class-instances . We could consider just documenting this if it's too much trouble to add compatibility for Functions by identifying the default exported methods and conditionally extending the WorkerEntrypoint class. |
@eltigerchino I definitely think it's doable, but the behaviour will need to be documented, yes. FWIW, all the adapter needs to do is either do what this PR already does, or re-export the class extending |
closes #10117
closes #1712
Cloudflare Workers
(and Pages, while it’s still a separate product)allows for handlers to be exported from a worker as methods on an object exported as a default export. Currently,adapter-cloudflare-workers
exports afetch
handler method, but there is no way to add other handler methods, such as ascheduled
orqueue
handler. This functionality was requested just over a year ago in #10496.This PR adds a configuration option for
@sveltejs/adapter-cloudflare-workers
to provide a file whose exports get added to the default option exported by the module the adapter generates. Thefetch
handler the two adapter uses does not get overridden.export
?handlers
?Currentlypnpm check
is failing because theworktop
dependency has type definitions for the exported handler type that are very out of date.Edit: Cloudflare Pages does not, in fact, support additional handlers.
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.Edits