2017-10-10 11:15:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								# Hooks
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-07-05 03:05:36 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The hooks are modules that add functionality to the core. They are loaded during the server boot. For example, if your project needs to work with a SQL database, you will have to add the hook `strapi-hook-bookshelf`  to be able to connect your app with your database.
							 
						 
					
						
							
								
									
										
										
										
											2017-10-10 11:15:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-18 11:25:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								**File structure**
							 
						 
					
						
							
								
									
										
										
										
											2019-07-18 19:28:52 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-10-10 11:15:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const fs = require('fs');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const path = require('path');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								module.exports = strapi => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const hook = {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    /**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								     *  Default options
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								     */
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    defaults: {
							 
						 
					
						
							
								
									
										
										
										
											2018-10-18 11:25:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      // config object
							 
						 
					
						
							
								
									
										
										
										
											2017-10-10 11:15:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    },
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    /**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								     *  Initialize the hook
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								     */
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    initialize: cb => {
							 
						 
					
						
							
								
									
										
										
										
											2018-10-18 11:25:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      // Write your code here.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      // this.defaults['your_config'] to access to your configs.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      cb();
							 
						 
					
						
							
								
									
										
										
										
											2019-07-18 19:28:52 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    },
							 
						 
					
						
							
								
									
										
										
										
											2017-10-10 11:15:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return hook;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-10-11 15:17:08 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								-  `defaults`  (object): Contains the defaults configurations. This object is merged to `strapi.config.hook.settings.**` . 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  `initialize`  (function): Called during the server boot. The callback `cb`  needs to be called. Otherwise, the hook won't be loaded. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-18 11:25:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The hooks are accessible through the `strapi.hook`  variable.
							 
						 
					
						
							
								
									
										
										
										
											2017-10-10 11:15:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-10-11 15:17:08 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Structure
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-18 11:25:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Node modules
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Every folder that follows this name pattern `strapi-hook-*`  in your `./node_modules`  folder will be loaded as a hook.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-10-11 15:17:08 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								A hook needs to follow the structure below:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-18 11:25:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								/strapi-hook-[...]
							 
						 
					
						
							
								
									
										
										
										
											2018-06-08 23:05:48 +10:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								└─── lib
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								     -  index.js
							 
						 
					
						
							
								
									
										
										
										
											2017-10-11 15:17:08 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								-  LICENSE.md 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  package.json 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  README.md 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `index.js`  is the entry point to your hook. It should look like the example above.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-18 11:25:09 +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.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-18 11:25:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/project
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								└─── admin
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								└─── api
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								└─── config
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								└─── hooks
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								│   └─── strapi-documentation
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								│        - index.js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								│   └─── strapi-server-side-rendering
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								│        - index.js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								└─── plugins
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								└─── public
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  favicon.ico 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  package.json 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  server.js 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Configuration and activation
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-07-05 18:48:02 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								To activate and configure your hook with custom options, you need to edit your `./config/hook.json`  file in your Strapi app.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-18 19:28:52 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-18 11:25:09 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  "hook-name": {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    "enabled": true,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-10-10 11:15:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								## Dependencies
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-07-11 16:23:14 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								It happens that a hook has a dependency to another one. For example, the `strapi-hook-bookshelf`  has a dependency to `strapi-hook-knex` . Without it, the `strapi-hook-bookshelf`  can't work correctly. It also means that the `strapi-hook-knex`  hook has to be loaded before.
							 
						 
					
						
							
								
									
										
										
										
											2017-10-10 11:15:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								To handle this case, you need to update the `package.json`  at the root of your hook.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```json
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{
							 
						 
					
						
							
								
									
										
										
										
											2018-07-11 16:23:14 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  "name": "strapi-hook-bookshelf",
							 
						 
					
						
							
								
									
										
										
										
											2017-10-10 11:15:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  "version": "x.x.x",
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  "description": "Bookshelf hook for the Strapi framework",
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  "dependencies": {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  },
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  "strapi": {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    "dependencies": [
							 
						 
					
						
							
								
									
										
										
										
											2018-07-11 16:23:14 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      "strapi-hook-knex"
							 
						 
					
						
							
								
									
										
										
										
											2017-10-10 11:15:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    ]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
									
										
										
										
											2018-07-11 16:23:14 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2017-10-10 11:15:24 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```