Nextjs - How to Build Next.js Application on Docker and Pass API Url
Introduction In this post we will see: How to prepare a docker image for your…
May 02, 2021
In our previous posts, we have seen How to Create Article in Strapi by REST APIs.
For SEO purpose, you would want that the article URL will have some nice english characters, like title.
In this post, we will configure article content type to have a slug field based on the title.
slug
Auto generated slug
After this, we need to write little code to generate slug.
Edit /api/article/models/article.js
, and put below code:
'use strict';
/**
* Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#lifecycle-hooks)
* to customize this model
*/
const slugify = require('slugify');
const getUniqueSlug = async (title, num=0) => {
let input = `${title}`;
if (num > 0) {
input = `${title}-${num}`;
}
const slug = slugify(input, {
lower: true
});
const [article] = await strapi.services.article.find({
slug: slug
});
if (!article){
return slug;
}
else {
return getUniqueSlug(title, num+1);
}
}
module.exports = {
lifecycles: {
beforeCreate: async (data) => {
data.slug = await getUniqueSlug(data.title);
},
},
};
In above code, we are using a new module: slugify
.
Install it via: npm i slugify
Also, I have written a custom function getUniqueSlug
to get a unique slug.
Otherwise, if two users write the same title. My slug will have same string. And, I did not want to give error to users to have unique title across.
Lets look at the examples below:
# title = How to do Exercise
# Slug will be
how-to-do-exercise
# Another user write same title,
# Now the slug will be
how-to-do-exercise-1
# Another user write same title,
# Now the slug will be
how-to-do-exercise-2
Note: Till now we have just populated the slug field. You would not see this slug in the actual URL. For this, now we will need to look at the frontend part.
See out next post for a dive into the frontend with Next.js
Introduction In this post we will see: How to prepare a docker image for your…
Introduction In this post, we will do following: create a Next.js project…
I use drupal-7 in my website. I used to write articles and put images in that…
I have a custom content type, and there are around 2000 number of nodes in my…
Introduction This post is in contuation of our previous post: How to use Draft…
Introduction In this post, we will use in Next.js with strapi. And, we will…
Introduction So you have a Django project, and want to run it using docker image…
Introduction It is very important to introduce few process so that your code and…
Introduction In this post, we will see a sample Jenkin Pipeline Groovy script…
Introduction We often require to execute in timed manner, i.e. to specify a max…
Introduction In some of the cases, we need to specify namespace name along with…
Introduction In most of cases, you are not pulling images from docker hub public…