Skip to content

Commit

Permalink
feat(sitemap): Add a new configuration to discard invalid patterns fr…
Browse files Browse the repository at this point in the history
…om sitemap and avoid duplicate content
  • Loading branch information
Baptiste LEULLIETTE committed Jul 11, 2024
1 parent 1124474 commit 3eca325
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,16 @@ To see all the types you can choose from, run `strapi content-types:list`.

> `required:` NO | `type:` array
### Exclude invalid relations relational objects
This setting allow you to exclude invalid entries when the pattern is not valid for the entry

Example : You have added a `slug` property to the configuration entry `allowedFields`.
If a content doesn't have the field `slug` filled, no entry in the sitemap will be generated for this content (to avoid duplicate content)

###### Key: `discardInvalidRelations `

> `required:` NO | `type:` boolean
## 🤝 Contributing

Feel free to fork and make a pull request of this plugin. All the input is welcome!
Expand Down
2 changes: 2 additions & 0 deletions server/services/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const getLanguageLinks = async (config, page, contentType, defaultURL) => {

const { pattern } = config.contentTypes[contentType]['languages'][locale];
const translationUrl = await strapi.plugins.sitemap.services.pattern.resolvePattern(pattern, translation);
if (!translationUrl) return null;
let hostnameOverride = config.hostname_overrides[translation.locale] || '';
hostnameOverride = hostnameOverride.replace(/\/+$/, '');
links.push({
Expand Down Expand Up @@ -112,6 +113,7 @@ const getSitemapPageData = async (config, page, contentType) => {

const { pattern } = config.contentTypes[contentType]['languages'][locale];
const path = await strapi.plugins.sitemap.services.pattern.resolvePattern(pattern, page);
if (!path) return null;
let hostnameOverride = config.hostname_overrides[page.locale] || '';
hostnameOverride = hostnameOverride.replace(/\/+$/, '');
const url = `${hostnameOverride}${path}`;
Expand Down
18 changes: 16 additions & 2 deletions server/services/pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,33 @@ const getRelationsFromPattern = (pattern) => {

const resolvePattern = async (pattern, entity) => {
const fields = getFieldsFromPattern(pattern);
let errorInPattern = false

Check warning on line 127 in server/services/pattern.js

View workflow job for this annotation

GitHub Actions / lint (18)

Missing semicolon

Check warning on line 127 in server/services/pattern.js

View workflow job for this annotation

GitHub Actions / lint (20)

Missing semicolon

fields.map((field) => {
const relationalField = field.split('.').length > 1 ? field.split('.') : null;

if (!relationalField) {
pattern = pattern.replace(`[${field}]`, entity[field] || '');
const replacement = entity[field] || ''

Check warning on line 133 in server/services/pattern.js

View workflow job for this annotation

GitHub Actions / lint (18)

Missing semicolon

Check warning on line 133 in server/services/pattern.js

View workflow job for this annotation

GitHub Actions / lint (20)

Missing semicolon
if (strapi.config.get('plugin.sitemap.discardInvalidRelations') && !replacement) {
errorInPattern = true;

Check warning on line 135 in server/services/pattern.js

View workflow job for this annotation

GitHub Actions / lint (18)

Expected indentation of 8 spaces but found 10

Check warning on line 135 in server/services/pattern.js

View workflow job for this annotation

GitHub Actions / lint (20)

Expected indentation of 8 spaces but found 10
return

Check warning on line 136 in server/services/pattern.js

View workflow job for this annotation

GitHub Actions / lint (18)

Expected indentation of 8 spaces but found 10

Check warning on line 136 in server/services/pattern.js

View workflow job for this annotation

GitHub Actions / lint (18)

Missing semicolon

Check warning on line 136 in server/services/pattern.js

View workflow job for this annotation

GitHub Actions / lint (20)

Expected indentation of 8 spaces but found 10

Check warning on line 136 in server/services/pattern.js

View workflow job for this annotation

GitHub Actions / lint (20)

Missing semicolon
}
pattern = pattern.replace(`[${field}]`, replacement);
} else if (Array.isArray(entity[relationalField[0]])) {
strapi.log.error(logMessage('Something went wrong whilst resolving the pattern.'));
} else if (typeof entity[relationalField[0]] === 'object') {
pattern = pattern.replace(`[${field}]`, entity[relationalField[0]] && entity[relationalField[0]][relationalField[1]] ? entity[relationalField[0]][relationalField[1]] : '');
const replacement = entity[relationalField[0]] && entity[relationalField[0]][relationalField[1]] ? entity[relationalField[0]][relationalField[1]] : ''

Check warning on line 142 in server/services/pattern.js

View workflow job for this annotation

GitHub Actions / lint (18)

Missing semicolon

Check warning on line 142 in server/services/pattern.js

View workflow job for this annotation

GitHub Actions / lint (20)

Missing semicolon
if (strapi.config.get('plugin.sitemap.discardInvalidRelations') && !replacement) {
errorInPattern = true

Check warning on line 144 in server/services/pattern.js

View workflow job for this annotation

GitHub Actions / lint (18)

Expected indentation of 8 spaces but found 10

Check warning on line 144 in server/services/pattern.js

View workflow job for this annotation

GitHub Actions / lint (18)

Missing semicolon

Check warning on line 144 in server/services/pattern.js

View workflow job for this annotation

GitHub Actions / lint (20)

Expected indentation of 8 spaces but found 10

Check warning on line 144 in server/services/pattern.js

View workflow job for this annotation

GitHub Actions / lint (20)

Missing semicolon
return

Check warning on line 145 in server/services/pattern.js

View workflow job for this annotation

GitHub Actions / lint (18)

Expected indentation of 8 spaces but found 10

Check warning on line 145 in server/services/pattern.js

View workflow job for this annotation

GitHub Actions / lint (18)

Trailing spaces not allowed

Check warning on line 145 in server/services/pattern.js

View workflow job for this annotation

GitHub Actions / lint (20)

Expected indentation of 8 spaces but found 10

Check warning on line 145 in server/services/pattern.js

View workflow job for this annotation

GitHub Actions / lint (20)

Trailing spaces not allowed
}

pattern = pattern.replace(`[${field}]`, replacement);
}
});

if (errorInPattern) return null // Return null if there was an error in the pattern due to invalid relation and avoid duplicate content

pattern = pattern.replace(/\/+/g, '/'); // Remove duplicate forward slashes.
pattern = pattern.startsWith('/') ? pattern : `/${pattern}`; // Make sure we only have on forward slash.
return pattern;
Expand Down

0 comments on commit 3eca325

Please sign in to comment.