Functions
Page summary:
src/indexhosts global register, bootstrap, and destroy functions to run logic during application lifecycle.
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,
- register a custom provider for the Users & Permissions plugin.
register() is the very first thing that happens when a Strapi application is starting. This happens before any setup process and you don't have any access to database, routes, policies, or any other backend server elements within the register() function.
You can use register() to front-load security tasks such as loading secrets, rotating API keys, or registering authentication providers before the app finishes initializing.
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
The bootstrap() function is run before the back-end server starts but after the Strapi application has setup, so you have access to anything from the strapi object.
You can use bootstrap() to support editorial workflows, for example by seeding starter content, attaching webhooks, or scheduling cron jobs at startup.
You can run yarn strapi console (or npm run strapi console) in the terminal and interact with the strapi object.
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.)
You can use destroy() to handle operational clean-up, such as closing database or queue connections and removing listeners so the application can shut down cleanly.
All 3 lifecycle functions can be put together to configure custom behavior during application startup and shutdown.
- Decide when your logic should run.
- Add initialization-only tasks (e.g. registering a custom field or provider) in
register(). - Add startup tasks that need full Strapi access (e.g. seeding or attaching webhooks) in
bootstrap(). - Add cleanup logic (e.g. closing external connections) in
destroy().
- Add initialization-only tasks (e.g. registering a custom field or provider) in
- Place the code in
src/index.js|ts. Keepregister()lean because it runs before Strapi is fully set up. - Restart Strapi to confirm each lifecycle executes in sequence.
Example to register a custom field, seed content and clean up
export default {
register({ strapi }) {
strapi.customFields.register({
name: 'color',
type: 'string',
plugin: 'color-picker',
});
},
async bootstrap({ strapi }) {
const entryCount = await strapi.db.query('api::palette.palette').count();
if (entryCount === 0) {
await strapi.db.query('api::palette.palette').create({
data: { name: 'Default palette', primary: '#4945FF' },
});
}
},
async destroy({ strapi }) {
await strapi.db.connection?.destroy?.();
},
};
You might find additional information in this blog article about registering lifecycle functions.