-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add remaining Cloudflare values to contexts * support `getLoadContext` * revert `tsup.config.ts` * tweak `cloudflare-pages.ts` * make `getLoadContext` not option for `RemixMiddlewareOptions`
- Loading branch information
Showing
9 changed files
with
134 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,24 @@ | ||
import type { LoaderFunctionArgs } from '@remix-run/cloudflare' | ||
import { useLoaderData } from '@remix-run/react' | ||
|
||
export const loader = ({ context }: LoaderFunctionArgs) => { | ||
const { env } = context.cloudflare as { env: { MY_VAR: string} } | ||
return { | ||
var: env.MY_VAR | ||
} | ||
export const loader = (args: LoaderFunctionArgs) => { | ||
const extra = args.context.extra | ||
const cloudflare = args.context.cloudflare | ||
return { cloudflare, extra } | ||
} | ||
|
||
export default function Index() { | ||
const data = useLoaderData<typeof loader>() | ||
const { cloudflare, extra } = useLoaderData<typeof loader>() | ||
return ( | ||
<div> | ||
<h1>Remix and Hono</h1> | ||
<h2>Var is {data.var}</h2> | ||
<h2>Var is {cloudflare.env.MY_VAR}</h2> | ||
<h3> | ||
{cloudflare.cf ? 'cf,' : ''} | ||
{cloudflare.ctx ? 'ctx,' : ''} | ||
{cloudflare.caches ? 'caches are available' : ''} | ||
</h3> | ||
<h4>Extra is {extra}</h4> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// functions/[[path]].ts | ||
import handle from 'hono-remix-adapter/cloudflare-pages' | ||
import { getLoadContext } from 'load-context' | ||
import * as build from '../build/server' | ||
import server from '../server' | ||
|
||
export const onRequest = handle(build, server) | ||
export const onRequest = handle(build, server, { getLoadContext }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { AppLoadContext } from '@remix-run/cloudflare' | ||
import type { PlatformProxy } from 'wrangler' | ||
|
||
interface Env { | ||
MY_VAR: string | ||
} | ||
|
||
type Cloudflare = Omit<PlatformProxy<Env>, 'dispose'> | ||
|
||
declare module '@remix-run/cloudflare' { | ||
interface AppLoadContext { | ||
cloudflare: Cloudflare | ||
extra: string | ||
} | ||
} | ||
|
||
type GetLoadContext = (args: { | ||
request: Request | ||
context: { cloudflare: Cloudflare } | ||
}) => AppLoadContext | ||
|
||
// Shared implementation compatible with Vite, Wrangler, and Cloudflare Pages | ||
export const getLoadContext: GetLoadContext = ({ context }) => { | ||
return { | ||
...context, | ||
extra: 'stuff', | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { AppLoadContext } from '@remix-run/cloudflare' | ||
import type { Context } from 'hono' | ||
|
||
export type GetLoadContext = (args: { | ||
request: Request | ||
context: { | ||
// Relaxing the type definition | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
cloudflare: any | ||
} | ||
}) => AppLoadContext | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export const defaultGetLoadContext = ({ context }: any) => { | ||
return { | ||
...context, | ||
} | ||
} | ||
|
||
export const createGetLoadContextArgs = (c: Context) => { | ||
return { | ||
context: { | ||
cloudflare: { | ||
env: c.env, | ||
cf: c.req.raw.cf, | ||
ctx: { | ||
...c.executionCtx, | ||
}, | ||
caches, | ||
}, | ||
}, | ||
request: c.req.raw, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters