-
-
Notifications
You must be signed in to change notification settings - Fork 652
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
context c.set/get
not working in cloudflare pages functions
#3320
Comments
Hi @zoubingwu Thank you for creating the issue. This looks like a bug, but it's a known issue that can be resolved because a If you want to do more complex things for Cloudflare Pages, you can try the Cloudflare Pages starter template: npm create hono@latest -- --template cloudflare-pages |
@yusukebe cf context prodvides a data object to attach some data and will persist during the request, their docs didn't mention that clearly tho but it works see https://developers.cloudflare.com/pages/functions/api-reference/#eventcontext and https://community.cloudflare.com/t/what-is-context-data-in-pages-functions/476559 can we use that instead? I found that this object is not accessible in hono's context. I updated the code in the reproduce repo, you can check it out. // _middleware.ts
export const onRequest = [
// handleMiddleware(async (c, next) => {
// console.log("middleware setting user");
// c.set('user', 'joe');
// console.log(c);
// c.env.eventContext.data.user = 'joe2' // not working
// return await next();
// })
async (context) => {
context.data.user = "joe"; // working
return await context.next();
},
];
// [[route]].ts
const routes = app.get("/hello", (c) => {
return c.json({
//@ts-ignore
message: `Hello, ${c.env.eventContext.data.user}!`,
});
}); |
Hi @zoubingwu Exactly. Cloudflare Pages provides Hi @BarryThePenguin If you have any thoughts, please share them. |
I'd expect this to work.. handleMiddleware(async (c, next) => {
console.log("middleware setting user");
c.set('user', 'joe');
console.log(c);
- c.env.eventContext.data.user = 'joe2' // not working
+ c.executionCtx.data.user = 'joe2' // should work
return await next();
}) The Cloudflare hono/src/adapter/cloudflare-pages/handler.ts Lines 50 to 53 in f9349ec
Line 407 in f9349ec
|
I can see a solution where using |
according to #1219, if we use |
This is closed by #3332! |
What version of Hono are you using?
4.5.8
What runtime/platform is your app running on?
cloudflare pages
What steps can reproduce the bug?
made a minimal repro here: https://github.com/zoubingwu/hono-ctx-set-cf-pages
then open
http://localhost:8888/
, you will see alert withhello undefined
What is the expected behavior?
should be
hello joe
since it was set in middlewareWhat do you see instead?
hello undefined
Additional information
The text was updated successfully, but these errors were encountered: