Skip to content
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

Allow adding additional functions to _worker.js #10496

Open
smitssjors opened this issue Aug 6, 2023 · 10 comments · May be fixed by #13207
Open

Allow adding additional functions to _worker.js #10496

smitssjors opened this issue Aug 6, 2023 · 10 comments · May be fixed by #13207

Comments

@smitssjors
Copy link

smitssjors commented Aug 6, 2023

Describe the problem

Cloudflare Queues is currently in open beta. It would be nice to be able to consume messages from the same worker that handles HTTP requests, as opposed to having to creating a separate worker. For a worker to consume from a queue a new queue function needs to be defined on the worker object.

// worker.js
export default {
  async fetch(request, env, ctx) {
    // Handle request...
  },
  async queue(batch, env) {
    // Process batch...
  },
};

Currently there is no easy way to add this queue function to the woker object in the generated _worker.js.

Other Cloudflare services such as Email Workers follow a similar approach.

Describe the proposed solution

I'd suggest a special src/cloudflare-worker.js file which would export functions such as queue which then during compilation get added to the _worker.js file.

// src/cloudflare-worker.js

export const queue = (batch, env) =>  // Process batch...

Alternatives considered

No response

Importance

would make my life easier

Additional Information

No response

@smitssjors smitssjors changed the title Allow for adding additional functions to _worker.js feat-req(adapter-cloudflare) Allow for adding additional functions to _worker.js Aug 6, 2023
@benmccann benmccann changed the title feat-req(adapter-cloudflare) Allow for adding additional functions to _worker.js Allow adding additional functions to _worker.js Aug 8, 2023
@a-hariti
Copy link

How would the local DX look like in this context? @smitssjors

I think CF pages works now as long as you don't use anything specific to CF,
miniflare still doesn't cover all the cases as well :(

@smitssjors
Copy link
Author

@a-hariti It shouldn't have to be any different than it is now.

@a-hariti
Copy link

@smitssjors That's a terrible DX as it only works on the build output

you have to wait for a fresh build on each minute change

@smitssjors
Copy link
Author

@a-hariti Yes I agree, but that is out of the scope of this issue. I think that fixing that would require substantial work on how adapters integrate with kit and vite. Currently they just adapt the output of building.

@ghostdevv
Copy link
Member

It could be an interesting idea to come up with something similar to how adapter node works. We could have a src/worker.ts file that looks something like:

import { handle } from 'some-virtual-module'

export default {
	fetch: handle
}

@ghostdevv
Copy link
Member

Though I only think this would work when deploying to workers right?

@smitssjors
Copy link
Author

@ghostdevv To me this looks like a nicer solution. The node adapter docs are not clear where the custom server will live e.g., does kit copy over all files in src or does a user need to copy them over manually?

Though I only think this would work when deploying to workers right?

I think so.

@lsfratel
Copy link

+1

@thomasfosterau
Copy link

thomasfosterau commented Dec 20, 2024

Sorry for resurrecting this issue, but I've got a use case which would greatly benefit from using queues as well as a scheduled worker.

Are there any further requirements for this beyond simply adding an option to the Cloudflare adapter which imports functions from a defined file and adds them to the generated worker?

If not, this seems fairly easy to implement, I might be able to put together a PR this weekend.

@eltigerchino
Copy link
Member

Other handlers don't seem to be supported for the Cloudflare Pages function. See #13207 (comment) and #13207 (comment)

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

Successfully merging a pull request may close this issue.

7 participants