Strapi plugin logo for Slugify

Slugify
Plugin verified by Strapi

Auto slugify a field for any content type

strapi-plugin-slugify

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.

Downloads Install size Package version

Requirements

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.

Supported Strapi versions

  • v4.x.x

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.

Installation

npm install strapi-plugin-slugify

# or

yarn add strapi-plugin-slugify

Configuration

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 put title for both the field and references properties.

IMPORTANT NOTE: Make sure any sensitive data is stored in env files.

Additional Requirement for GraphQL

Per #35 please ensure that the slugify plugin configuration is placed before the graphql plugin configuration.

The Complete Plugin Configuration Object

PropertyDescriptionTypeDefaultRequired
contentTypesThe Content Types to add auto slugification and search findOne by slug search utility toObject{}No
contentTypesmodelNameThe model name of the content type (it is the singularName in the model schema)StringN/AYes
contentTypesmodelNamefieldThe name of the field to add the slugStringN/AYes
contentTypesmodelNamereferencesThe name(s) of the field(s) used to build the slug. If an array of fields is set it will result in a compound slugString or ArrayN/AYes
slugifyWithCountDuplicate strings will have their occurrence appended to the end of the slugBooleanfalseNo
shouldUpdateSlugAllow the slug to be updated after initial generation.BooleanfalseNo
skipUndefinedReferencesSkip reference fields that have no data. Mostly applicable to compound slugBooleanfalseNo
slugifyOptionsThe options to pass the the slugify function. All options can be found in the slugify docsObject{}No

Usage

Once the plugin has been installed, configured and enabled the configured content types will have the following additional functionality

Slugification

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.

Find One by Slug

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.

Additional Requirements

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.

Examples

Example Request

Making the following request with the sample configuration will look as follows

REST

1await fetch(`${API_URL}/api/slugify/slugs/article/lorem-ipsum-dolor`);
2// GET /api/slugify/slugs/article/lorem-ipsum-dolor

GraphQL

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}

Example Response

If an article with the slug of lorem-ipsum-dolor exists the response will look the same as a single entity response

REST

Successful 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}
Error Response

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}

GraphQL

Successful Response
1{
2  "data": {
3    "findSlug": {
4      "data": {
5        "id": "1",
6        "attributes": {
7          "title": "lorem ipsum dolor"
8        }
9      }
10    }
11  }
12}
Error Response

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}

Bugs

If any bugs are found please report them as a Github Issue

Install now

npm install strapi-plugin-slugify

STATS

45 GitHub stars3053 weekly downloads

Last updated

371 days ago

Strapi Version

Unavailable

Author

github profile image for @ComfortablyCoding
@ComfortablyCoding

Useful links

Create your own plugin

Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.