Issue with Auto-Populating canonicalURL in SEO Component Using Lifecycle Hooks (Strapi v4.16)

Hello Strapi Community,
I am encountering an issue with auto-populating the canonicalURL field in the SEO component of my blog-details content type using lifecycle hooks in Strapi v4.16. Despite following the suggested approach, the canonicalURL field remains null. I would appreciate any guidance or suggestions to resolve this issue.
Here are the details:
1. Problem Description:
I have a blog-details content type with an SEO component that includes a canonicalURL field.
I want to automatically populate the canonicalURL field based on the slug field when creating or updating a blog-details entry.
The canonicalURL field remains null even after attempting to set it via lifecycle hooks.
2. Strapi Version:
Strapi v4.16

3. File Structure and Code:

File Path: src/api/blog-details/content-types/blog-details/lifecycles.js

**Code in lifecycles.js:
**
`// src/api/blog-details/content-types/blog-details/lifecycles.js

module.exports = {
async beforeCreate(event) {
const { params } = event;

if (params.data.slug) {
  // Ensure SEO component exists
  if (params.data.SEO) {
    params.data.SEO.canonicalURL = `https://yourwebsite.com/${params.data.slug}`;
  } else {
    // Create SEO object if it doesn't exist
    params.data.SEO = {
      canonicalURL: `https://yourwebsite.com/${params.data.slug}`,
    };
  }
}

},

async beforeUpdate(event) {
const { params } = event;

if (params.data.slug) {
  // Ensure SEO component exists
  if (params.data.SEO) {
    params.data.SEO.canonicalURL = `https://yourwebsite.com/${params.data.slug}`;
  } else {
    // Create SEO object if it doesn't exist
    params.data.SEO = {
      canonicalURL: `https://yourwebsite.com/${params.data.slug}`,
    };
  }
}

},
};
I would appreciate any help or suggestions to resolve this issue. Thank you!

This topic has been created from a Discord post (1278733881259855990) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord