Custom Worker
The worker generated by the Cloudflare adapter only exports a fetch handler (opens in a new tab).
Sometimes your application needs to expose another type of handler (i.e. a scheduled handler (opens in a new tab)) or export a Durable Object (opens in a new tab). This can be achieved by creating a custom worker.
The custom worker re-uses the generated fetch handler.
Create your custom worker Worker
The following custom worker re-uses the generated fetch handler and adds a scheduled handler:
// custom-worker.ts
// @ts-ignore `.open-next/worker.ts` is generated at build time
import { default as handler } from "./.open-next/worker.js";
export default {
fetch: handler.fetch,
async scheduled(event) {
// ...
},
} satisfies ExportedHandler<CloudflareEnv>;
// The re-export is only required if your app uses the DO Queue and DO Tag Cache
// See https://opennext.js.org/cloudflare/caching for details
// @ts-ignore `.open-next/worker.ts` is generated at build time
export { DOQueueHandler, DOShardedTagCache } from "./.open-next/worker.js";
See an example in the adapter repository (opens in a new tab).
Update the entry point in your wrangler configuration
// wrangler.jsonc
{
- "main": "./.open-next/worker.js"
+ "main": "./path/to/custom-worker.ts",
}