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: support getLoadContext #6

Merged
merged 6 commits into from
Sep 23, 2024

Conversation

yusukebe
Copy link
Owner

This PR enables users can access the Cloudflare values - cf, ctx, and caches on Remix routes.

Closes #5

@yusukebe
Copy link
Owner Author

yusukebe commented Sep 22, 2024

Hey @jfsiii !

I've tried to implement regarding the issue. Can you review it?

@jfsiii
Copy link

jfsiii commented Sep 22, 2024

@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:

If you'd like to add additional properties to the load context, you should export a getLoadContext function from a shared module so that load context in Vite, Wrangler, and Cloudflare Pages are all augmented in the same way:

The official handler from Remix also accepts the getLoadContext

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.

@yusukebe
Copy link
Owner Author

@jfsiii Thank you for the comment.

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.

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?

@yusukebe
Copy link
Owner Author

I'm working on supporting getLoadContext. Please review it later.

@yusukebe yusukebe changed the title feat: add remaining Cloudflare values to contexts feat: support getLoadContext Sep 22, 2024
@yusukebe
Copy link
Owner Author

@jfsiii

I've added the code to support getLoadContext with user values.

For dev server, you can add getLoadContext in vite.config.ts: https://github.com/yusukebe/hono-remix-adapter/pull/6/files#diff-6671dd857b3a02ae74576b82e0e7088e90b9c2220259fe16cf78a5833a2c6e10R24

For Cloudflare Pages, add it here: https://github.com/yusukebe/hono-remix-adapter/pull/6/files#diff-26d9f92c12cdb12bbf15be7f110c94f8bd7557c13b4929cb240829e6d4d13f68R7

What do you think about this?

Copy link

@jfsiii jfsiii left a 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,
Copy link

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.

Copy link

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

Copy link
Owner Author

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.

src/cloudflare-pages.ts Outdated Show resolved Hide resolved
src/cloudflare-pages.ts Outdated Show resolved Hide resolved
Comment on lines +7 to +9
// Relaxing the type definition
// eslint-disable-next-line @typescript-eslint/no-explicit-any
cloudflare: any
Copy link

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

Suggested change
// 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
}

Comment on lines +13 to +14
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const defaultGetLoadContext = ({ context }: any) => {
Copy link

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}) => {

Copy link
Owner Author

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.

Copy link

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

Screenshot 2024-09-23 at 7 46 38 AM Screenshot 2024-09-23 at 7 40 38 AM

Copy link
Owner Author

Choose a reason for hiding this comment

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

@jfsiii

Oooops. Sorry. The building way was wrong. Fixed it and released the new version. Try v0.2.1.

Copy link

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,
Copy link

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": {}
        }

Copy link
Owner Author

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.

@yusukebe
Copy link
Owner Author

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!

@yusukebe yusukebe merged commit 3db87a4 into main Sep 23, 2024
2 checks passed
@yusukebe yusukebe deleted the feat/add-remaining-cf-values-to-context branch September 23, 2024 07:08
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.

Allow users to supply their own getLoadContext
2 participants