-
Notifications
You must be signed in to change notification settings - Fork 10
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: support getLoadContext
#6
Conversation
Hey @jfsiii ! I've tried to implement regarding the issue. Can you review it? |
@yusukebe Adding those items to the context is a step in the right direction, but it still doesn't allow users to add their own values to the context As I quote in #5 , according to the Remix docs:
The official handler from Remix also accepts the export const onRequest = createPagesFunctionHandler({
build,
getLoadContext,
}); Are you opposed to allowing this? It's a critical feature for me. I have this patched/working locally and am happy to send a PR if that helps. |
@jfsiii Thank you for the comment.
No! It will be a good feature, and I'd like you to send a PR. So, first, I'll merge this PR, and after that, you will create a PR, okay? |
I'm working on supporting |
getLoadContext
I've added the code to support For dev server, you can add For Cloudflare Pages, add it here: https://github.com/yusukebe/hono-remix-adapter/pull/6/files#diff-26d9f92c12cdb12bbf15be7f110c94f8bd7557c13b4929cb240829e6d4d13f68R7 What do you think about this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TL;DR It works! Ship it 🚀
I left lots of comments/questions, but nothing that I think should hold up the PR. They'd all be easy enough to follow up on later. Especially during this 0.x period
@@ -20,6 +21,7 @@ export default defineConfig({ | |||
}), | |||
serverAdapter({ | |||
adapter, | |||
getLoadContext, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't leave a comment on line 14, but it should be
remixCloudflareDevProxy({
getLoadContext,
}),
to match the example vite.config.ts from https://remix.run/docs/en/main/guides/vite#augmenting-load-context
I'm not sure if/how that impacts serverAdapter
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I'm not 💯 in my testing, but when I added
remixCloudflareDevProxy({
getLoadContext,
}),
and removed getLoadContext
from serverAdapter
, the example app still passed my bindings and augmented context through
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
About remixCloudflareDevProxy
, perhaps it may not need it because this hono-remix-adapter
does the same thing. I'll test it; if so, I will create the PR.
// Relaxing the type definition | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
cloudflare: any |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like we could do more than any
here.
It might be tricky/impractical to use the "real" Env
and PlatformProxy
types, but I think this works
// Relaxing the type definition | |
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |
cloudflare: any | |
cloudflare: { | |
env: Record<string, unknown> | |
cf: IncomingRequestCfProperties | |
ctx: ExecutionContext | |
caches: typeof caches | |
} |
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export const defaultGetLoadContext = ({ context }: any) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Names are hard, but if you gave that type a name like:
type SomeContext = {
cloudflare: {
env: Record<string, unknown>
cf: IncomingRequestCfProperties
ctx: ExecutionContext
caches: typeof caches
}
};
We could use it like
export type GetLoadContext = (args: {
request: Request
context: SomeContext
}) => AppLoadContext
export const defaultGetLoadContext = ({ context }: { context: SomeContext}) => {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you override the definition of AppLoadContext
in your app: https://github.com/yusukebe/hono-remix-adapter/pull/6/files#diff-fe3b5413a3ab56944fc97a3eb9b71e83c41e4d0b41a2303932ae92bd8c82cc8fR11-R13
The type mismatch will occur. To remove the typo error, I've added the any
. This may be fixed in the future, but it remains as any
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yusukebe Oh, right. Apologies for not testing that locally. I think 0.2.0 shipped with this type, because I'm getting this type error in my app after updating
Nothing a @ts-expect-error
can't fix, though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oooops. Sorry. The building way was wrong. Fixed it and released the new version. Try v0.2.1
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries. It happens. And it was kind of my fault, anyway.
Thanks!
return { | ||
context: { | ||
cloudflare: { | ||
env: c.env, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure about this one. Do we want c.env
which will return
{
"MY_VAR": "My Valuez",
"ASSETS": {},
"eventContext": {
"request": {},
"functionPath": "/",
"params": {},
"data": {},
"env": {
"MY_VAR": "My Valuez",
"ASSETS": {}
}
}
}
or c.env.eventContext.env
, which would only return
{
"MY_VAR": "My Valuez",
"ASSETS": {}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is also used in the dev server: https://github.com/yusukebe/hono-remix-adapter/pull/6/files#diff-34d76a96faa930c4b9909c055beb422e601e2267f72ce12d6f15a1c2cc6ea914R23
It should be c.env
to get MY_VAR
correctly since it does not have eventContext
.
Hey @jfsiii Thank you for the comments. Some remain, but I'll merge this now and release the new version (maybe v0.2.0) that includes this update! |
This PR enables users can access the Cloudflare values -
cf
,ctx
, andcaches
on Remix routes.Closes #5