Skip to content

Commit 6c6196a

Browse files
ogadrayusukebe
andauthored
chore: update the example for cloudflare-workers (#29)
* feat: add example for cloudflare-workers * Align variable name and value with cloudflare-pages * format --------- Co-authored-by: Yusuke Wada <yusuke@kamawada.com>
1 parent 1c50620 commit 6c6196a

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

examples/cloudflare-workers/app/routes/_index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import { useLoaderData } from '@remix-run/react'
44
export const loader = (args: LoaderFunctionArgs) => {
55
const extra = args.context.extra
66
const cloudflare = args.context.cloudflare
7-
return { cloudflare, extra }
7+
const myVarInVariables = args.context.hono.context.get('MY_VAR_IN_VARIABLES')
8+
return { cloudflare, extra, myVarInVariables }
89
}
910

1011
export default function Index() {
11-
const { cloudflare, extra } = useLoaderData<typeof loader>()
12+
const { cloudflare, extra, myVarInVariables } = useLoaderData<typeof loader>()
1213
return (
1314
<div>
1415
<h1>Remix and Hono</h1>
@@ -19,6 +20,7 @@ export default function Index() {
1920
{cloudflare.caches ? 'caches are available' : ''}
2021
</h3>
2122
<h4>Extra is {extra}</h4>
23+
<h5>Var in Variables is {myVarInVariables}</h5>
2224
</div>
2325
)
2426
}

examples/cloudflare-workers/load-context.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1+
import type { Context } from 'hono'
12
import type { PlatformProxy } from 'wrangler'
23

3-
interface Env {
4-
MY_VAR: string
4+
type Env = {
5+
Bindings: {
6+
MY_VAR: string
7+
}
8+
Variables: {
9+
MY_VAR_IN_VARIABLES: string
10+
}
511
}
612

713
type GetLoadContextArgs = {
814
request: Request
915
context: {
10-
cloudflare: Omit<PlatformProxy<Env>, 'dispose' | 'caches' | 'cf'> & {
16+
cloudflare: Omit<PlatformProxy<Env['Bindings']>, 'dispose' | 'caches' | 'cf'> & {
1117
caches: PlatformProxy<Env>['caches'] | CacheStorage
1218
cf: Request['cf']
1319
}
20+
hono: {
21+
context: Context<Env>
22+
}
1423
}
1524
}
1625

@@ -19,6 +28,9 @@ declare module '@remix-run/cloudflare' {
1928
interface AppLoadContext extends ReturnType<typeof getLoadContext> {
2029
// This will merge the result of `getLoadContext` into the `AppLoadContext`
2130
extra: string
31+
hono: {
32+
context: Context<Env>
33+
}
2234
}
2335
}
2436

examples/cloudflare-workers/server/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ const app = new Hono<{
55
Bindings: {
66
MY_VAR: string
77
}
8+
Variables: {
9+
MY_VAR_IN_VARIABLES: string
10+
}
811
}>()
912

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

0 commit comments

Comments
 (0)