The Plugin System and the Boot Process
A Fastify plugin is an essential tool at the disposal of a developer. Every functionality except the root instance of the server should be wrapped in a plugin. Plugins are the key to reusability, code sharing, and achieving proper encapsulation between Fastify instances.
Fastify’s root instance will load all registered plugins asynchronously following the registration order during the boot sequence. Furthermore, a plugin can depend on others, and Fastify checks these dependencies and exits the boot sequence with an error if it finds missing ones.
This chapter starts with the declaration of a simple plugin and then, step by step, adds more layers to it. We will learn why the options
parameter is crucial and how the Fastify register method uses it during the boot sequence. The final goal is to understand how plugins interact with each other thanks to encapsulation.
To understand this challenging topic, we will introduce and learn...