Functions
The./src/index.js
file (or./src/index.ts
file in aTypeScript-based project) includes globalregister,bootstrap anddestroy functions that can be used to add dynamic and logic-based configurations.
The functions can be synchronous, asynchronous, or return a promise.
Synchronous function
- JavaScript
- TypeScript
module.exports={
register(){
// some sync code
},
bootstrap(){
// some sync code
},
destroy(){
// some sync code
}
};
exportdefault{
register(){
// some sync code
},
bootstrap(){
// some sync code
},
destroy(){
// some sync code
}
};
Asynchronous function
- JavaScript
- TypeScript
module.exports={
asyncregister(){
// some async code
},
asyncbootstrap(){
// some async code
},
asyncdestroy(){
// some async code
}
};
exportdefault{
asyncregister(){
// some async code
},
asyncbootstrap(){
// some async code
},
asyncdestroy(){
// some async code
}
};
Function returning a promise
- JavaScript
- TypeScript
module.exports={
register(){
returnnewPromise(/* some code */);
},
bootstrap(){
returnnewPromise(/* some code */);
},
destroy(){
returnnewPromise(/* some code */);
}
};
exportdefault{
register(){
returnnewPromise(/* some code */);
},
bootstrap(){
returnnewPromise(/* some code */);
},
destroy(){
returnnewPromise(/* some code */);
}
};
Register
Theregister
lifecycle function, found in./src/index.js
(or in./src/index.ts
), is an asynchronous function that runs before the application is initialized.It can be used to:
- extend plugins
- extendcontent-types programmatically
- load someenvironment variables
- register acustom field that would be used only by the current Strapi application,
- register acustom provider for the Users & Permissions plugin.
register()
is the very first thing that happens when a Strapi application is starting. This happensbefore any setup process and you don't have any access to database, routes, policies, or any other backend server elements within theregister()
function.
Bootstrap
Thebootstrap
lifecycle function, found in./src/index.js
(or in./src/index.ts
), is called at every server start.
It can be used to:
- create an admin user if there isn't one
- fill the database with some necessary data
- declare custom conditions for theRole-Based Access Control (RBAC) feature
Thebootstrapi()
function is runbefore the back-end server starts butafter the Strapi application has setup, so you have access to anything from thestrapi
object.
You can runyarn strapi console
(ornpm run strapi console
) in the terminal and interact with thestrapi
object.
Destroy
Thedestroy
function, found in./src/index.js
(or in./src/index.ts
), is an asynchronous function that runs before the application gets shut down.
It can be used to gracefully:
- stopservices that are running
- clean up plugin actions (e.g. close connections, remove listeners, etc.)
You might find additional information inthis blog article about registering lifecycle functions.