Lifecycle functions
The content of this page has not been updated to Strapi v5 yet.
The ./src/index.js
file (or ./src/index.ts
file in a TypeScript-based project) includes global register, bootstrap and destroy 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
}
};
export default {
register() {
// some sync code
},
bootstrap() {
// some sync code
},
destroy() {
// some sync code
}
};
Asynchronous function
- JavaScript
- TypeScript
module.exports = {
async register() {
// some async code
},
async bootstrap() {
// some async code
},
async destroy() {
// some async code
}
};
export default {
async register() {
// some async code
},
async bootstrap() {
// some async code
},
async destroy() {
// some async code
}
};
Function returning a promise
- JavaScript
- TypeScript
module.exports = {
register() {
return new Promise(/* some code */);
},
bootstrap() {
return new Promise(/* some code */);
},
destroy() {
return new Promise(/* some code */);
}
};
export default {
register() {
return new Promise(/* some code */);
},
bootstrap() {
return new Promise(/* some code */);
},
destroy() {
return new Promise(/* some code */);
}
};
Register
The register
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
- extend content-types programmatically
- load some environment variables
- register a custom field that would be used only by the current Strapi application.
Bootstrap
The bootstrap
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 the Role-Based Access Control (RBAC) feature
Destroy
The destroy
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:
- stop services that are running
- clean up plugin actions (e.g. close connections, remove listeners, etc.)