Skip to content

Commit

Permalink
docs: Add example of using AsyncLocalStorage to README (#47)
Browse files Browse the repository at this point in the history
* Add example of using AsyncLocalStorage to README

* Change AsyncLocalStorage to an example of using Context Storage Middleware

* Change get from context to the correct description

* Remove description of saving Headers from the context usage example

* Fix type definition for Variables
  • Loading branch information
chimame authored Dec 10, 2024
1 parent dabf6e0 commit 6e61599
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,49 @@ export const getLoadContext: GetLoadContext = ({ context }) => {
}
```

## AsyncLocalStorage

You can use AsyncLocalStorage, which is supported by Node.js, Cloudflare Workers, etc.
You can easily store context using Hono's Context Storage Middleware.

```ts
// server/index.ts
import { Hono } from 'hono'
import { contextStorage } from 'hono/context-storage'

export interface Env {
Variables: {
message: string
// db: DatabaseConnection // It's also a good idea to store database connections, etc.
}
}

const app = new Hono<Env>()

app.use(contextStorage())

app.use(async (c, next) => {
c.set('message', 'Hello!')

await next()
})

export default app
```

You can retrieve and process the context saved in Hono from Remix as follows:

```ts
// app/routes/_index.tsx
import type { Env } from 'server'
import { getContext } from 'hono/context-storage' // It can be called anywhere for server-side processing.
export const loader = () => {
const message = getContext<Env>().var.message
...
}
```

## Auth middleware for Remix routes

If you want to add Auth Middleware, e.g. Basic Auth middleware, please be careful that users can access the protected pages with SPA tradition. To prevent this, add a `loader` to the page:
Expand Down

0 comments on commit 6e61599

Please sign in to comment.