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

feat(cloudflare): cloudflare:fetch and cloudflare:durable:fetch hooks #3047

Closed
wants to merge 1 commit into from

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Jan 29, 2025

This PR enables two new hooks:

  • cloudflare:fetch(event)
  • cloudflare:durable:fetch(event)

These hooks (while adding to complexity and runtime overhead) allow custom extensions to normal nitro lifecycle.

The main use case of this is for durable objects to allow redirect some requests from main worker to the single instance durable worker.

export default defineNitroPlugin((nitroApp: NitroApp) => {
  nitroApp.hooks.hook("cloudflare:fetch", async (event) => {
    // Force redirects all fetch requests to the durable object
    return event.durableFech();
  });

  nitroApp.hooks.hook("cloudflare:durable:fetch", async (event) => {
    console.log("Handling response in durable object:", event.url);
  });
});

With the above plugin, event in event handlers has event.context.cloudflare.context with the value of DurableObjectState type


(this is mainly a draft idea; An alternative is to simply expose a method of direct fetch to durable)

@pi0
Copy link
Member Author

pi0 commented Jan 30, 2025

Closing in favor of #3048, too much complexity and makes more edge cases if used wrongly, especially since we completely loose control over lifecycle

@pi0 pi0 closed this Jan 30, 2025
@pi0 pi0 deleted the feat/cf-durable-fetch branch January 30, 2025 00:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant