1.0.8 • Published 2 years ago
@x3m-group/strapi-plugin-auto-links v1.0.8
strapi-plugin-auto-links
A plugin for Strapi that auto generate links for resources (self, canonical, alternates, ...)
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
Installation
npm install @x3m-group/strapi-plugin-auto-links
# or
yarn add @x3m-group/strapi-plugin-auto-linksConfiguration
The plugin configuration is stored in a config file located at ./config/plugins.js or ./config/plugins.ts.
A minimal configuration
module.exports = ({ env }) => ({
// ...
"auto-links": {
enabled: true,
config: {
contentTypes: {
article: {},
},
},
},
// ...
});Custom Canonical configuration
module.exports = ({ env }) => ({
// ...
"auto-links": {
enabled: true,
config: {
canonical: {
host: "https://{item.locale}.example.com",
path: "/resources/{model.info.pluralName}/{item.slug}",
},
contentTypes: {
author: {},
article: {},
tag: {
canonical: {
host: "https://tags.example.com",
path: "/{item.slug}?q=all",
},
},
},
},
},
// ...
});Example result
http://localhost:1337/api/articles?populate=*
{
"data": [
{
"id": 1,
"attributes": {
"title": "My First Article English",
"slug": "my-first-article-english",
"locale": "en",
"localizations": {
"data": [
...
]
},
// newly auto generated links
"links": {
"self": "http:/localhost:1337/api/articles/1",
"canonical": "https:/example.com/articles/my-first-article-english",
"alternates": [
{
"locale": "nl",
"self": "http:/localhost:1337/api/articles/2",
"canonical": "https:/example.com/articles/mijn-eerste-artikel-nederlands"
}
]
}
// ...
}
}
],
"meta": {
...
}
}Configuration Object
| Property | Description | Type | Default | Required |
|---|---|---|---|---|
| self | global self configuration | Object | {} | No |
| self.enabled | enable self url rendering | Boolean | true | No |
| self.host | default global self host pattern | String | env SELF_HOST or http://localhost:1337 | No |
| self.path | default global self path pattern | String | /api/{model.info.pluralName}/{item.id} | No |
| canonical | global canonical configuration | Object | {} | No |
| canonical.enabled | enable canonical url rendering | Boolean | true | No |
| canonical.host | default global canonical host pattern | String | env CANONICAL_HOST or https://example.com | No |
| canonical.path | default global canonical path pattern | String | /{item.locale}/{model.info.pluralName}/{item.slug} | No |
| alternates | global alternates configuration | Object | {} | No |
| alternates.enabled | enable alternates url rendering (localized links) | Boolean | true | No |
| contentTypes | The Content Types to add auto links | Object | {} | No |
| contentTypesmodelName | The model name of the content type (it is the singularName in the model schema) | Object | {} | Yes |
| contentTypesmodelName.self | override global self configuration | Object | {} | No |
| contentTypesmodelName.self.enabled | enable self url rendering | Boolean | true | No |
| contentTypesmodelName.self.host | self host pattern | String | global value | No |
| contentTypesmodelName.self.path | self path pattern | String | global value | No |
| contentTypesmodelName.canonical | override global canonical configuration | Object | {} | No |
| contentTypesmodelName.canonical.enabled | enable canonical url rendering | Boolean | true | No |
| contentTypesmodelName.canonical.host | canonical host pattern | String | global value | No |
| contentTypesmodelName.canonical.path | canonical path pattern | String | global value | No |
| contentTypesmodelName.alternates | override global alternates configuration | Object | {} | No |
| contentTypesmodelName.alternates.enabled | enable alternates url rendering (localized links) | Boolean | true | No |
Context Vars
| Property | Description | Type |
|---|---|---|
| item | the current resource item | Object |
| item.* | all available properties of the resource item (must be selected/populated) | Any |
| model | the current resource schema information | Object |
| model.kind | defines if the content-type is collectionType or singleType | String |
| model.collectionName | database table name in which the data should be stored | String |
| model.info | strapi model information | Object |
| model.info.displayName | default name to use in the admin panel | String |
| model.info.singularName | singular form of the content-type name | String |
| model.info.pluralName | plural form of the content-type name | String |