packages/async-context-middleware/README.md
Request-scoped async context middleware for Remix. It stores each request context in AsyncLocalStorage so utilities can access it anywhere in the same async call stack.
node:async_hooks AsyncLocalStoragenpm i remix
Simply use the asyncContext() middleware at the router level to make the request context available to all functions in the same async execution context. Get access to the context using the getContext() function.
import { createRouter } from 'remix/fetch-router'
import { asyncContext, getContext } from 'remix/async-context-middleware'
let router = createRouter({
middleware: [asyncContext()],
})
router.get('/users/:id', async () => {
// Access context from anywhere in the async call stack
let context = getContext()
let userId = context.params.id
return new Response(`User ${userId}`)
})
Note: This middleware requires support for node:async_hooks.
fetch-router - Router for the web Fetch APISee LICENSE