Skip to content

Commit d028d9e

Browse files
build(docs): context-management app route (#89)
1 parent 5806934 commit d028d9e

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

docs/pages/docs/core-concepts/context-management.mdx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,44 @@ app.get('/hello', (ctx, next) => {
4141
})
4242
```
4343

44+
There is a gotcha if you utilize `app.route()` and piggy-back on a Pylon app instance, because there must only be one import of the Pylon `app` instance. The import is the main entry point for the application and it is important that it should only be used once, because the Pylon app registers some global middlewares and the router. If you import it multiple times, it will register the middlewares multiple times and cause conflicts. Example conflicts include:
45+
46+
- Multiple request and response logs in the console.
47+
- Multiple passes of middlewares (`app.use('*')`), which can lead to incorrect variable reads/writes.
48+
49+
To avoid this, all other routers should be created as sub-routers using a new Hono instance.
50+
51+
`/src/authRouter.ts`:
52+
53+
```typescript
54+
import {type Bindings, type Variables} from '@getcronit/pylon'
55+
import {Hono} from 'hono'
56+
57+
const authRouter = new Hono<{
58+
Bindings: Bindings
59+
Variables: Variables
60+
}>()
61+
62+
authRouter.get('/', c => {
63+
return c.text('Hello from Auth')
64+
})
65+
66+
export default authRouter
67+
```
68+
69+
`/src/index.ts`:
70+
71+
```typescript
72+
import {app} from '@getcronit/pylon'
73+
import authRouter from './authRouter'
74+
75+
app.route('/auth', authRouter)
76+
77+
export const graphql = {...}
78+
79+
export default app
80+
```
81+
4482
For more detailed information on utilizing the Hono app instance, refer to the [Hono documentation](https://hono.dev/getting-started/basic).
4583

4684
## Accessing Context in Service Functions

0 commit comments

Comments
 (0)