Auto slugify a field for any content type
A plugin for Strapi that provides the ability to auto slugify a field for any content type. It also provides a findOne by slug endpoint as a utility.
The installation requirements are the same as Strapi itself and can be found in the documentation on the Quick Start page in the Prerequisites info card.
NOTE: While this plugin may work with the older Strapi versions, they are not supported, it is always recommended to use the latest version of Strapi.
npm install strapi-plugin-slugify
# or
yarn add strapi-plugin-slugify
The plugin configuration is stored in a config file located at ./config/plugins.js
.
Please note that the field referenced in the configuration file must exist. You can add it using the Strapi Admin UI. Also note that adding a field at a later point in time will require you to unpublish, change, save and republish the entry/entries in order for this plugin to work correctly.
A sample configuration
1module.exports = ({ env }) => ({
2 // ...
3 slugify: {
4 enabled: true,
5 config: {
6 contentTypes: {
7 article: {
8 field: 'slug',
9 references: 'title',
10 },
11 },
12 },
13 },
14 // ...
15});
This will listen for any record created or updated in the article content type and set a slugified value for the slug field automatically based on the title field.
Note that if you want to rewrite the same field (so
title
is both a reference and a slug) then you just puttitle
for both thefield
andreferences
properties.
IMPORTANT NOTE: Make sure any sensitive data is stored in env files.
Per #35 please ensure that the slugify plugin configuration is placed before the graphql plugin configuration.
Property | Description | Type | Default | Required |
---|---|---|---|---|
contentTypes | The Content Types to add auto slugification and search findOne by slug search utility to | Object | {} | No |
contentTypesmodelName | The model name of the content type (it is the singularName in the model schema) | String | N/A | Yes |
contentTypesmodelNamefield | The name of the field to add the slug | String | N/A | Yes |
contentTypesmodelNamereferences | The name(s) of the field(s) used to build the slug. If an array of fields is set it will result in a compound slug | String or Array | N/A | Yes |
slugifyWithCount | Duplicate strings will have their occurrence appended to the end of the slug | Boolean | false | No |
shouldUpdateSlug | Allow the slug to be updated after initial generation. | Boolean | false | No |
skipUndefinedReferences | Skip reference fields that have no data. Mostly applicable to compound slug | Boolean | false | No |
slugifyOptions | The options to pass the the slugify function. All options can be found in the slugify docs | Object | {} | No |
Once the plugin has been installed, configured and enabled the configured content types will have the following additional functionality
Any time the respective content types have an entity created or updated the slug field defined in the settings will be auto generated based on the provided reference field.
Hitting the /api/slugify/slugs/:modelName/:slug
endpoint for any configured content types will return the entity type that matches the slug in the url. Additionally the endpoint accepts any of the parameters that can be added to the routes built into Strapi.
IMPORTANT The modelName is case sensitive and must match exactly with the name defined in the configuration.
Like all other created API endpoints the findSlug
route must be allowed under User & Permissions -> Roles -> Public/Authenticated
for the user to be able to access the route.
Making the following request with the sample configuration will look as follows
1await fetch(`${API_URL}/api/slugify/slugs/article/lorem-ipsum-dolor`);
2// GET /api/slugify/slugs/article/lorem-ipsum-dolor
1{
2 findSlug(modelName:"article",slug:"lorem-ipsum-dolor"){
3 ... on ArticleEntityResponse{
4 data{
5 id
6 attributes{
7 title
8 }
9 }
10 }
11 }
12}
Additionally if draftAndPublish
is enabled for the content-type a publicationState
arg can be passed to the GraphQL query that accepts either preview
or live
as input.
IMPORTANT Please beware that the request for an entry in preview
will return both draft entries & published entries as per Strapi default.
1{
2 findSlug(modelName:"article",slug:"lorem-ipsum-dolor",publicationState:"preview"){
3 ... on ArticleEntityResponse{
4 data{
5 id
6 attributes{
7 title
8 }
9 }
10 }
11 }
12}
If an article with the slug of lorem-ipsum-dolor
exists the response will look the same as a single entity response
1{
2 "data": {
3 "id": 1,
4 "attributes":{
5 "title": "lorem ipsum dolor",
6 "slug": "lorem-ipsum-dolor",
7 "createdAt": "2022-02-17T01:49:31.961Z",
8 "updatedAt": "2022-02-17T03:47:09.950Z",
9 "publishedAt": null
10 }
11 }
12}
To be inline with Strapi's default behavior for single types if an article with the slug of lorem-ipsum-dolor
does not exist a 404 error will be returned.
1{
2 "data": null,
3 "error": {
4 "status": 404,
5 "name": "NotFoundError",
6 "message": "Not Found",
7 "details": {}
8 }
9}
1{
2 "data": {
3 "findSlug": {
4 "data": {
5 "id": "1",
6 "attributes": {
7 "title": "lorem ipsum dolor"
8 }
9 }
10 }
11 }
12}
To be inline with Strapi's default behavior for single types if an article with the slug of lorem-ipsum-dolor
does not exist the data will be null.
1{
2 "data": {
3 "findSlug": {
4 "data": null
5 }
6 }
7}
If any bugs are found please report them as a Github Issue
npm install strapi-plugin-slugify
Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.