onRegister hook
Runs when a new encapsulated plugin context is created.
Syntax
app.addHook('onRegister', (instance, opts) => {}) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
instance | FastifyInstance | No | The newly created child instance. |
opts | object | No | The options passed to register(). |
Returns
void — Synchronous; inspect or decorate the child.
Examples
import Fastify from 'fastify';
const app = Fastify();
app.addHook('onRegister', (instance, opts) => {
instance.log.info({ prefix: opts.prefix }, 'plugin registered');
});
Notes
Triggered only for plugins that create their own encapsulation context
(not those wrapped with fastify-plugin). Useful for per-plugin setup,
diagnostics, or scoped decoration.