Storage (shared context)
Storage (shared context) between functions across the execution chain
NEMO provides a storage (shared context) that allows you to share data between functions in the middleware execution chain. This can be useful when you need to pass data between functions or store data that needs to be accessed by multiple functions.
By default it operates using in-memory storage, but you can extend it with your own storage solution or database of choice.
Warning! Be careful using database as storage adapter, as it can slow down the execution chain and make your site's TTFB skyrocket.
Recommendation: Use KV databases/solutions like Redis, Vercel EdgeConfig etc.
Usage example
Below you can see an example of how to use the shared context in your middleware functions.
Remember that each request's middleware execution is a separate event instance, so the context is not shared between different requests.
Custom Storage adapter
You can extend the default in-memory storage with your own storage adapter. To do this, you need to create a class that implements the StorageAdapter
interface.
After creating your storage adapter, you can use it by passing it to the NEMO
constructor.