File tree Expand file tree Collapse file tree 5 files changed +40
-12
lines changed
examples/cloudflare-pages Expand file tree Collapse file tree 5 files changed +40
-12
lines changed Original file line number Diff line number Diff line change @@ -4,11 +4,12 @@ import { useLoaderData } from '@remix-run/react'
44export 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
1011export 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}
Original file line number Diff line number Diff line change @@ -18,6 +18,9 @@ test('Should return 200 response - /', async ({ page }) => {
1818
1919 const contentH4 = await page . textContent ( 'h4' )
2020 expect ( contentH4 ) . toBe ( 'Extra is stuff' )
21+
22+ const contentH5 = await page . textContent ( 'h5' )
23+ expect ( contentH5 ) . toBe ( 'Var in Variables is My variable set in c.set' )
2124} )
2225
2326test ( 'Should return 200 response - /api' , async ( { page } ) => {
Original file line number Diff line number Diff line change 11import type { AppLoadContext } from '@remix-run/cloudflare'
2+ import type { Context } from 'hono'
23import type { PlatformProxy } from 'wrangler'
34
4- interface Env {
5- MY_VAR : string
5+ type Env = {
6+ Bindings : {
7+ MY_VAR : string
8+ }
9+ Variables : {
10+ MY_VAR_IN_VARIABLES : string
11+ }
612}
713
8- type Cloudflare = Omit < PlatformProxy < Env > , 'dispose' >
14+ type Cloudflare = Omit < PlatformProxy < Env [ 'Bindings' ] > , 'dispose' >
915
1016declare module '@remix-run/cloudflare' {
1117 interface AppLoadContext {
1218 cloudflare : Cloudflare
1319 extra : string
20+ hono : {
21+ context : Context < Env >
22+ }
1423 }
1524}
1625
1726type GetLoadContext = ( args : {
1827 request : Request
19- context : { cloudflare : Cloudflare }
28+ context : {
29+ cloudflare : Cloudflare
30+ hono : { context : Context < Env > }
31+ }
2032} ) => AppLoadContext
2133
2234// Shared implementation compatible with Vite, Wrangler, and Cloudflare Pages
Original file line number Diff line number Diff line change @@ -5,19 +5,22 @@ const app = new Hono<{
55 Bindings : {
66 MY_VAR : string
77 }
8+ Variables : {
9+ MY_VAR_IN_VARIABLES : string
10+ }
811} > ( )
912
10- app . use ( async ( c , next ) => {
13+ 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} )
1418
1519app . get ( '/api' , ( c ) => {
1620 return c . json ( {
1721 message : 'Hello' ,
18- var : c . env . MY_VAR
22+ var : c . env . MY_VAR ,
1923 } )
2024} )
2125
22-
2326export default app
Original file line number Diff line number Diff line change 11import type { AppLoadContext } from '@remix-run/cloudflare'
22import type { Context } from 'hono'
33
4- export type GetLoadContext = ( args : {
4+ type GetLoadContextArgs = {
55 request : Request
66 context : {
77 // Relaxing the type definition
88 // eslint-disable-next-line @typescript-eslint/no-explicit-any
99 cloudflare : any
10+ hono : {
11+ context : Context
12+ }
1013 }
11- } ) => AppLoadContext | Promise < AppLoadContext >
14+ }
15+
16+ export type GetLoadContext = ( args : GetLoadContextArgs ) => AppLoadContext | Promise < AppLoadContext >
1217
1318// eslint-disable-next-line @typescript-eslint/no-explicit-any
1419export const defaultGetLoadContext = ( { context } : any ) : AppLoadContext => {
@@ -17,7 +22,7 @@ export const defaultGetLoadContext = ({ context }: any): AppLoadContext => {
1722 }
1823}
1924
20- export const createGetLoadContextArgs = ( c : Context ) => {
25+ export const createGetLoadContextArgs = ( c : Context ) : GetLoadContextArgs => {
2126 return {
2227 context : {
2328 cloudflare : {
@@ -29,6 +34,9 @@ export const createGetLoadContextArgs = (c: Context) => {
2934 // @ts -expect-error globalThis.caches is not typed
3035 caches : globalThis . caches ? caches : undefined ,
3136 } ,
37+ hono : {
38+ context : c ,
39+ } ,
3240 } ,
3341 request : c . req . raw ,
3442 }
You can’t perform that action at this time.
0 commit comments