Skip to content

Commit 78e4659

Browse files
committed
add load-context for the node example
1 parent 4dfcced commit 78e4659

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1+
import type { LoaderFunctionArgs } from '@remix-run/node'
2+
import { useLoaderData } from '@remix-run/react'
3+
4+
export const loader = (args: LoaderFunctionArgs) => {
5+
const extra = args.context.extra
6+
const url = args.context.url
7+
return { extra, url }
8+
}
9+
110
export default function Index() {
11+
const { extra, url } = useLoaderData<typeof loader>()
212
return (
313
<div>
414
<h1>Remix and Hono</h1>
15+
<h2>URL is {url}</h2>
16+
<h3>Extra is {extra}</h3>
517
</div>
618
)
719
}

examples/node/load-context.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
type GetLoadContextArgs = {
2+
request: Request
3+
}
4+
5+
declare module '@remix-run/node' {
6+
interface AppLoadContext extends ReturnType<typeof getLoadContext> {
7+
url: string
8+
extra: string
9+
}
10+
}
11+
12+
export function getLoadContext(args: GetLoadContextArgs) {
13+
return {
14+
url: args.request.url,
15+
extra: 'stuff',
16+
}
17+
}

examples/node/main.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { serve } from '@hono/node-server'
33
import { serveStatic } from '@hono/node-server/serve-static'
44
import handle from 'hono-remix-adapter/node'
55
import * as build from './build/server'
6+
import { getLoadContext } from './load-context'
67
import server from './server'
78

89
server.use(
@@ -11,4 +12,6 @@ server.use(
1112
})
1213
)
1314

14-
serve(handle(build, server))
15+
const handler = handle(build, server, { getLoadContext })
16+
17+
serve({ fetch: handler.fetch, port: 3010 })

0 commit comments

Comments
 (0)