Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions examples/cloudflare-workers/app/routes/_index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { useLoaderData } from '@remix-run/react'
export const loader = (args: LoaderFunctionArgs) => {
const extra = args.context.extra
const cloudflare = args.context.cloudflare
return { cloudflare, extra }
const myVarInVariables = args.context.hono.context.get('MY_VAR_IN_VARIABLES')
return { cloudflare, extra, myVarInVariables }
}

export default function Index() {
const { cloudflare, extra } = useLoaderData<typeof loader>()
const { cloudflare, extra, myVarInVariables } = useLoaderData<typeof loader>()
return (
<div>
<h1>Remix and Hono</h1>
Expand All @@ -19,6 +20,7 @@ export default function Index() {
{cloudflare.caches ? 'caches are available' : ''}
</h3>
<h4>Extra is {extra}</h4>
<h5>Var in Variables is {myVarInVariables}</h5>
</div>
)
}
20 changes: 16 additions & 4 deletions examples/cloudflare-workers/load-context.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import type { Context } from 'hono'
import type { PlatformProxy } from 'wrangler'

interface Env {
MY_VAR: string
type Env = {
Bindings: {
MY_VAR: string
}
Variables: {
MY_VAR_IN_VARIABLES: string
}
}

type GetLoadContextArgs = {
request: Request
context: {
cloudflare: Omit<PlatformProxy<Env>, 'dispose' | 'caches' | 'cf'> & {
cloudflare: Omit<PlatformProxy<Env['Bindings']>, 'dispose' | 'caches' | 'cf'> & {
caches: PlatformProxy<Env>['caches'] | CacheStorage
cf: Request['cf']
}
},
hono: {
context: Context<Env>
},
}
}

Expand All @@ -19,6 +28,9 @@ declare module '@remix-run/cloudflare' {
interface AppLoadContext extends ReturnType<typeof getLoadContext> {
// This will merge the result of `getLoadContext` into the `AppLoadContext`
extra: string
hono: {
context: Context<Env>
}
}
}

Expand Down
9 changes: 6 additions & 3 deletions examples/cloudflare-workers/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ import { Hono } from 'hono'
const app = new Hono<{
Bindings: {
MY_VAR: string
}
},
Variables: {
'MY_VAR_IN_VARIABLES': string
},
}>()
Comment on lines 4 to 11
Copy link
Contributor Author

@ogadra ogadra Nov 20, 2024

Choose a reason for hiding this comment

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

I think we can use Env imported from load-context.ts, but I didn't change it because I wasn't sure if I was allowed to.

Suggested change
const app = new Hono<{
Bindings: {
MY_VAR: string
}
},
Variables: {
'MY_VAR_IN_VARIABLES': string
},
}>()
+ import type { Env } from '../load-context'
+ const app = new Hono<Env>()

If I change it, I also need to update cloudflare-pages.


app.use(async(c, next) => {
c.set('MY_VAR_IN_VARIABLES', 'My variable set in c.set')
await next()
c.header('X-Powered-By', 'Remix and Hono')
})

app.get('/api', (c) => {
return c.json({
message: 'Hello',
var: c.env.MY_VAR
var: c.env.MY_VAR,
})
})


export default app