91 lines
2.0 KiB
Markdown
Raw Normal View History

# Hooks
2019-09-20 12:44:24 +02:00
The hooks are modules that add functionality to the core. They are loaded during the server boot.
2020-04-01 11:48:38 +02:00
## Structure
2019-07-18 19:28:52 +02:00
2020-04-01 11:48:38 +02:00
### File structure
2020-04-01 11:48:38 +02:00
```js
module.exports = strapi => {
const hook = {
/**
* Default options
*/
defaults: {
// config object
},
/**
* Initialize the hook
*/
2019-08-19 09:55:24 +02:00
async initialize() {
// await someAsyncCode()
2020-04-01 11:48:38 +02:00
// const settings = {...this.defaults, ...strapi.config.hook.settings.**};
2019-07-18 19:28:52 +02:00
},
};
return hook;
};
```
2020-04-01 11:48:38 +02:00
- `defaults` (object): Contains the default configurations.
- `initialize` (function): Called during the server boot.
2020-04-01 11:48:38 +02:00
The [configurations](#configuration-and-activation) of the hook are accessible through `strapi.config.hook.settings.**`.
2020-04-01 11:48:38 +02:00
The hooks are accessible through the `strapi.hook` variable.
### Node modules
Every folder that follows this name pattern `strapi-hook-*` in your `./node_modules` folder will be loaded as a hook.
A hook needs to follow the structure below:
```
/strapi-hook-[...]
└─── lib
- index.js
- LICENSE.md
- package.json
- README.md
```
The `index.js` is the entry point to your hook. It should look like the example above.
2020-04-01 11:48:38 +02:00
### Custom hooks
2019-07-05 03:05:36 +02:00
The framework allows to load hooks from the project directly without having to install them from npm. It's a great way to take advantage of the features of the hooks system for code that doesn't need to be shared between apps. To achieve this, you have to create a `./hooks` folder at the root of your project and put the hooks into it.
```
/project
└─── admin
└─── api
└─── config
└─── hooks
│ └─── strapi-documentation
│ - index.js
│ └─── strapi-server-side-rendering
│ - index.js
└─── public
- favicon.ico
- package.json
- server.js
```
## Configuration and activation
2020-04-01 11:48:38 +02:00
To activate and configure your hook with custom options, you need to edit your `./config/environments/**/hook.json` file in your Strapi app.
2019-07-18 19:28:52 +02:00
```javascript
{
...
"hook-name": {
"enabled": true,
...
}
}
```