← ClaudeAtlas

fastify-best-practiceslisted

This skill should be used when working on any Fastify project or when the user asks about Fastify best practices, conventions, or patterns. Trigger when: working on a Fastify project, creating Fastify plugins, registering routes, using decorators, organizing Fastify application structure, using @fastify/autoload, creating encapsulated contexts, working with fastify-plugin, building Fastify APIs, configuring Fastify server options, using the register API, understanding Fastify encapsulation, project scaffolding with fastify-cli
radesjardins/RAD-Claude-Skills · ★ 3 · Web & Frontend · score 76
Install: claude install-skill radesjardins/RAD-Claude-Skills
# Fastify: Core Architecture & Best Practices ## Core Mental Model — The Plugin Tree Understand that a Fastify application is a **directed acyclic graph (DAG) of encapsulated contexts**, not a flat middleware chain. Every call to `fastify.register()` creates a new child node in this tree. The root Fastify instance is the only context that is not itself a plugin. The DAG is managed internally by the `avvio` library. Avvio guarantees deterministic, async loading in the exact order plugins are registered. Never rely on timing assumptions — rely on registration order. Plugins signal readiness in exactly one of two ways: by returning a Promise, or by calling the `done` callback. Never mix both in the same plugin. If the plugin function is `async`, never accept or call `done` — Fastify will await the returned Promise. If the function is synchronous, call `done()` when setup is complete. ```js // CORRECT: async plugin — no done parameter module.exports = async function myPlugin(fastify, opts) { // setup logic } // CORRECT: callback plugin — call done explicitly module.exports = function myPlugin(fastify, opts, done) { // setup logic done() } // WRONG: mixing async and done — causes hangs or double-resolution module.exports = async function myPlugin(fastify, opts, done) { await someSetup() done() // BUG: never do this in an async function } ``` ## Encapsulation Rules — Hard Invariants These five rules are non-negotiable. Every architectural decision in Fastify flow