With Hooks, you can transform Payload from a traditional CMS into a fully-fledged application framework.
Example uses:
lastModifiedBy
data to a document to track who changed what over timeContactSubmission
s are created from a public websiteOrder
is createdOrder
s that users submit is accurate and validlastLoggedIn
date on a user by adding an afterLogin
hookThere are many more use cases for Hooks and the sky is the limit.
All hooks can be written as either synchronous or asynchronous functions. If the Hook should modify data before a document is updated or created, and it relies on asynchronous actions such as fetching data from a third party, it might make sense to define your Hook as an asynchronous function, so you can be sure that your Hook completes before the operation's lifecycle continues. Async hooks are run in series - so if you have two async hooks defined, the second hook will wait for the first to complete before it starts.
If your Hook simply performs a side-effect, such as updating a CRM, it might be okay to define it synchronously, so the Payload operation does not have to wait for your hook to complete.
Payload Hooks are only triggered on the server. You can safely remove your hooks from your Admin panel's client-side code by customizing the Webpack config, which not only keeps your Admin bundles' filesize small but also ensures that any server-side only code does not cause problems within browser environments.
You can specify hooks in the following contexts: