Problem connecting to actor from front end using cloudflare worker with hono

I'm not sure if how I'm setting it up wrong, but the code for cloudflare driver seems to skip setting the registry route if you pass a hono instance as an option. https://github.com/rivet-gg/rivetkit/blob/main/packages/drivers/cloudflare-workers/src/handler.ts
// Mount registry router at /registry
if (!hono) {
app.route("/registry", serverOutput.hono);
}
// Mount registry router at /registry
if (!hono) {
app.route("/registry", serverOutput.hono);
}
I'm very confused how to add my own api routes while also having registry mounted in cloudflare worker
GitHub
rivetkit/packages/drivers/cloudflare-workers/src/handler.ts at main...
🧰 The open-source alternative to Durable Objects. Easily self-hostable and works with your infrastructure. - rivet-gg/rivetkit
7 Replies
jog1t
jog1t•4mo ago
Hey, this topic has been already covered here - https://discord.com/channels/822914074136018994/1403394492215726161/1403418432036143276 let me know if the linked example works for you! In the meantime, I will put this as a official example!
wing
wingOP•4mo ago
Is it just updating the package version? @jog1t I managed to get it working by doing this:
export default {
...handler,
fetch: (request: any, env: any, ctx: any) => {
const url = new URL(request.url);

if (url.pathname.startsWith('/registry') && handler && handler.fetch) {
return handler.fetch(request, env, ctx);
}

return app.fetch(request, env, ctx);
},
};
export default {
...handler,
fetch: (request: any, env: any, ctx: any) => {
const url = new URL(request.url);

if (url.pathname.startsWith('/registry') && handler && handler.fetch) {
return handler.fetch(request, env, ctx);
}

return app.fetch(request, env, ctx);
},
};
I'm wondering for what reason createHandler doesn't mount registry routes when passing a hono instance?
jog1t
jog1t•4mo ago
You would need to show me the code before the change, and I will tell you why! 🙂
wing
wingOP•4mo ago
I can show my code, but I don’t think it’s related to why the createHandler doesn’t mount the registry routes. In the createHandler function, the if check skips mounting the registry endpoints if you pass a hono instance as an argument to createHandler
// Before
const app = new Hono();

const { handler, ActorHandler } = createHandler(app);

export { ActorHandler };

export default handler
// Before
const app = new Hono();

const { handler, ActorHandler } = createHandler(app);

export { ActorHandler };

export default handler
// After
const app = new Hono();

const { handler, ActorHandler } = createHandler();

export { ActorHandler };

export default {
...handler,
fetch: (request: any, env: any, ctx: any) => {
const url = new URL(request.url);

if (url.pathname.startsWith('/registry') && handler && handler.fetch) {
return handler.fetch(request, env, ctx);
}

return app.fetch(request, env, ctx);
},
};
// After
const app = new Hono();

const { handler, ActorHandler } = createHandler();

export { ActorHandler };

export default {
...handler,
fetch: (request: any, env: any, ctx: any) => {
const url = new URL(request.url);

if (url.pathname.startsWith('/registry') && handler && handler.fetch) {
return handler.fetch(request, env, ctx);
}

return app.fetch(request, env, ctx);
},
};
jog1t
jog1t•4mo ago
You’re right this: https://github.com/rivet-gg/rivetkit/blob/0433897ee37478366f1426061674db94a873f5e1/packages/drivers/cloudflare-workers/src/handler.ts#L78. this is not needed Will fix this is upcoming release! Thank you so much for spotting this!
wing
wingOP•4mo ago
No worries! I was wondering if there was a reason the if check existed and I was doing something wrong, or if it wasn't necessary Thanks for looking into it!
jog1t
jog1t•4mo ago
actually, you were doing it right from the beginning, this is probably some leftover!

Did you find this page helpful?