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: make context available to middleware #934

Closed

Conversation

rmarscher
Copy link
Contributor

Re: #914 I noticed I had to use ctx.context.__hono_context to access the hono context from middleware. While that is ok, I thought it might be useful to try to make the ctx.context available to middleware from the unstable_getCustomContext API.

Copy link

vercel bot commented Oct 3, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
waku ✅ Ready (Inspect) Visit Preview Oct 3, 2024 6:43pm

Copy link

codesandbox-ci bot commented Oct 3, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

throw new Error('Cannot rerender');
},
};
await runWithRenderStoreInternal(renderStore, () => run(0));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't looks nice. This function shouldn't be used outside RSC capabilities.
Consider unstable_getHonoContext is for server components.
Middleware can use __hono_context directly.
A little concern of mine is there can be two types of middleware, one that depends on hono and the other that doesn't.

@dai-shi
Copy link
Owner

dai-shi commented Oct 3, 2024

not planned for now.

@dai-shi dai-shi closed this Oct 3, 2024
@dai-shi
Copy link
Owner

dai-shi commented Oct 4, 2024

(but this brings me back to possibly prefer hono/context-storage... 😅 )

or change "render storage" to "request storage" 🤔 . Not anytime soon.

@rmarscher
Copy link
Contributor Author

Yes, no problem. As I work on my apps, I will look for ways to replace any specific use of Hono with the Waku context. I actually am seeing less and less of a need for it now that I understand Waku better. We could just make the Cloudflare bindings accessible on the Waku context instead of Hono.

(but this brings me back to possibly prefer hono/context-storage... 😅 )
or change "render storage" to "request storage" 🤔 . Not anytime soon.

Haha... Not anytime soon sounds good. I'm ready to move onto other things. 😃 But... moving c.env/c.executionCtx to ctx.context.cloudflareEnv / ctx.context.executionCtx for the Cloudflare Pages build might be worthwhile. Then I could avoid mentioning Hono so much in the Cloudflare guide.

@dai-shi
Copy link
Owner

dai-shi commented Oct 4, 2024

The reason I mark getCustomContext and getHonoContext unstable is because I'm not very confident yet. Let's learn more with getting more feedback.

Naming is hard. Hono's middleware and Waku's middleware, and Hono's context and Waku's context.

@rmarscher
Copy link
Contributor Author

One thing to note here, if we call the same function (like a data fetcher) that needs something from context, then it is necessary to pass through the context when calling it from waku middleware but use getCustomContext when calling it from a server component. I use EffectTS for data fetching on one of my apps and have a "Layer" for my context. When in middleware, I supply the context to the layer and otherwise it fetches it from getCustomContext. I think EffectTS Layers just create an ALS context (https://effect.website/docs/guides/context-management/services). So I'm basically just doing the same thing from this PR in userland now.

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.

2 participants