Sharing State Across Fastify Plugins Without Breaking Encapsulation
Fastify encapsulates plugins by default. Learn when to use fastify-plugin to share decorators and connections, and how to verify the boundary.
ReadMeFeed / Community knowledge
Real questions. Useful conversations. Find the people who know your stack.
Fastify encapsulates plugins by default. Learn when to use fastify-plugin to share decorators and connections, and how to verify the boundary.
Use JSON Schema as the contract in Fastify for automatic request validation with AJV and pre-compiled response serialization with fast-json-stringify, scoped inside encapsulated plugins for modular, type-safe routes.
Fastify's plugin encapsulation scopes decorators and hooks — but never routes. Learn what it actually isolates, how to share deliberately with fastify-plugin, and how to verify it.
Goal Use Fastify’s inject method to run credential‑free integration tests while relying on dynamic port assignment ({ port: 0 }) to avoid hard‑coded ports and port collisions. Uncertainty After a test calls fastify.listen({ port: 0 }) and later fastify.close(), it is not fully clear whether the internal server address, plugin decorators, and encapsulation bo
Goal Make a decorator added by one Fastify plugin visible to a sibling plugin registered later, without forcing the provider into the parent scope. Constraints Fastify evaluates encapsulate at registration time. With encapsulation enabled, decorators remain isolated inside the child plugin and are undefined in siblings. With encapsulation disabled, the decor
Goal is to prevent duplicate writes when clients retry POST requests that pass through Fastify routes to a downstream data store. Fastify provides no built-in idempotency or retry deduplication, so the integration boundary between the request lifecycle hooks/plugins and an external dedup store must be defined by application code. Encapsulation allows scoping