-
-
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 in adapter-cloudflare-workers #13207
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: e71366e 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/. |
documentation/docs/25-build-and-deploy/70-adapter-cloudflare-workers.md
Outdated
Show resolved
Hide resolved
documentation/docs/25-build-and-deploy/70-adapter-cloudflare-workers.md
Outdated
Show resolved
Hide resolved
documentation/docs/25-build-and-deploy/70-adapter-cloudflare-workers.md
Outdated
Show resolved
Hide resolved
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 |
@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 |
It’s a little ugly but the changes now allow the |
I'm glad this is coming.
which I think should be solved by: cloudflare/workers-sdk#7292 For the time being, I failed to get the "dual wrangler dev" setup to work. Even though I had it running it still didn't connect. But this might be my fault. But I never got around:
but that could be a workaround And am I correct that handlers.js/ts needs to be explicitly specified? I think that's missing in the docs (of this PR) |
Okey I updated the example repo. Using this PR:https://github.com/philippviereck/local-sveltekit-do
Both throw:
NOT Using this PR (workaround):https://github.com/philippviereck/local-sveltekit-do/tree/workaround Issues:
EDIT: In both repos I only tested Durable Objects. I did not once try the other worker handlers! This is all only regarding DOs |
@philippviereck when I get a chance I'll take a look at your repo and see what I can do (currently without reliable internet, which is a side-effect of moving house in Australia 🙃). Do any of the Wrangler commands work, such as |
Yes I think that PR needs to land to solve the Durable Objects issue. If that fixes the error you're encountering then everything's all good 😄
Yes, it needs to be explicitly specified in order to be used. Not sure if having a default is a good idea. What do you think needs to be improved in the docs for this PR? |
This is some great work! I've run into this issue recently while trying to play around with DOs but being unable to find a way to deploy them unless I made a separate project, and even then nothing worked locally :( At this point are we waiting on cloudflare? |
Can you share a minimal reproduction? |
If I’m not mistaken, @half2me is referring to the same issue as @philippviereck, which is a Cloudflare issue: cloudflare/workers-sdk#7292. Not much we can do about it other than wait for Cloudflare to fix it, but given it really only affects local previews where Durable Objects are used, it might be tolerable? |
'vite build' does error too, if I remember correctly. I will look at this today again |
@philippviereck I had a look at your repo - the issue is definitely that @eltigerchino any objection to me removing the Durable Objects parts of this PR so the rest can be merged? |
Apologies for the delayed response. Documentation ImprovementsThe current documentation doesn't clearly state that handlers need to be explicitly specified. At minimum, we should add a note that the default value is Proposed Enhancement: Standardized Naming ConventionRather than just documenting the default behavior, I believe we should adopt a standardized naming convention consistent with other platform-specific integrations (like Vercel, Netlify, and Node - see PRs #13430 and #13477). With this approach, a
...at least that's what I believe would create a clean API. Short-term SolutionTo avoid further delays, I suggest: Instead of implementing a consistent naming convention immediately, we could change the default value from Naming ConsiderationsIdeally, @threepointone I'm tagging you as you've been influential in the Durable Objects space. This integration would be valuable for SvelteKit × DO users - I myself became a DO user specifically through using SvelteKit, as Cloudflare was the only adapter with a free commercial tier. I later upgraded to paid and started using the whole CF dev platform, but the integration discrepancies between SvelteKit and Cloudflare have always been a major pain point. There have been many improvements to the local dev and overall Cloudflare × SvelteKit experience, but this enhancement would finally make their harmony complete - so maybe you can bring this up internally 😇 |
Hey, I’m from the Cloudflare team - sorry for the delay on the durable object issue, we are actively working on it now, but I can’t really estimate a timeline and I don’t want to hold up this PR 😅 If you can leave the DO part for a followup PR maybe, I can comment there once I've got a PR up with a wrangler pre-release to test with? Or let me know how you else you’d like to go about it :) Having a handlers.ts (or whatever it ends up being called) file that the user writes would work well though for what I think our solution will be - we’ll probably let you pass this into getPlatformProxy and that should give you access to Durable objects (and workflows or whatever) in local dev. |
adapter-cloudflare-workers
adapter-cloudflare-workers
@philippviereck I’ve make some small changes to the documentation -- is there anything further specifically you think needs to be mentioned or that is unclear?
I’m a bit skeptical that this is a good way to go about things, at least as far as this PR is concerned. What this PR does is to add the
I personally think the best default here is just to not include any handlers unless they’re explicitly specified. There isn’t really a pre-existing default handler file being used by anyone, unlike
I don’t think the additional Cloudflare handlers are really analogous to SvelteKit’s hooks, so it’s probably best to avoid conflating the two. I’d be interested to see if anyone thinks Happy to be corrected on this, but I also believe Cloudflare is (slowly) merging Pages and Workers into one product. I wouldn’t be surprised if the outcome of that eventually happening for SvelteKit is that there is eventually just a single |
Thanks for the update @emily-shen!
I’ve updated this PR to remove anything to do with Durable Objects for now -- for me the
Just to clarify, at the moment this PR works by merging the user’s handlers file into the Cloudflare Worker already created by this SvelteKit adapter (which just exports a fetch handler). If I’m not mistaken, SvelteKit won’t have to change how it uses |
documentation/docs/25-build-and-deploy/70-adapter-cloudflare-workers.md
Outdated
Show resolved
Hide resolved
documentation/docs/25-build-and-deploy/70-adapter-cloudflare-workers.md
Outdated
Show resolved
Hide resolved
I agree with you on all points. You make a good point about naming the option "worker," but since DOs aren't yet supported, "handlers" is more accurate. |
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