javascriptnext.jsstrapinetlify

Anyone knows how to use lifecycle hooks in Strapi 5?


I didn't find the documentation that explains this in Strapi 5, so i don't know if it changed since Strapi 4.

path of the code: src/api/post/content-types/post/lifecycle.js

const axios = require('axios');

module.exports = {
    async afterCreate(event) {
        await axios.post('https://api.netlify.com/build_hooks/key');
    },

    async afterUpdate(event) {
        await axios.post('https://api.netlify.com/build_hooks/key');
    },
}

When you use the method post on this link, netlify will automatic build your application with the new content. but it's not working


Solution

  • In Strapi v5, lifecycle hooks work similarly to Strapi v4, Possible flaws/fixes in your case might be :

    1. Its lifecycles.js and not lifecycle.js in src/api/post/content-types/post/lifecycle.js
    2. Axios post request body and structure.
    3. Use try catch block and console the error in catch block.

    Hope it works