-
-
Notifications
You must be signed in to change notification settings - Fork 54
Description
Feature request
Summary
Add hooks / extension point where userland code can decorate results.
Why is it needed?
Currently it's hard to hook into sitemap generation with userland code.
Suggested solution(s)
Allow core functions overload
or add dedicated extension points
or add registring custom handlers
or all together.
Related issue(s)/PR(s)
The feature is partly related to these PRs/Issues
Currently there is no way to apply different filters or different fields combination for same content type.
Even in the code it's limited by Object.keys(config.contentTypes) so no way to inject custom rule entry
I tried to hook into the code but found it difficult to extend.
Especially this core service:
node_modules/strapi-plugin-sitemap/server/services/core.js
The method createSitemapEntries is called directly and is considered as private for userland code. So there is no way to overload this function.
With some success I managed to hook into customEntries array by overloading getConfig method and appending my additional entries only during sitemap generation by checking stack trace :)
Here is my plugin in strapi-server.ts
I think it would be more handy to have dedicated extension points, so hooking into sitemap generation would be more easy.
module.exports = (plugin) => {
const service = plugin.services.settings;
const inited = service();
const origGetConfig = inited.getConfig;
inited.getConfig = async function MYgetConfig() {
const origResult = await origGetConfig();
const stackTrace = Error().stack;
const isGenerating =
stackTrace.includes("createSitemapEntries") &&
stackTrace.includes("Object.createSitemap");
if (!isGenerating) {
return origResult;
}
const knex = strapi.db.connection;
const benefitLocalityLinks = await knex.raw(`
SELECT DISTINCT '0.6' AS priority,
'daily' AS changefreq,
('/pl/v/' || slug_city || '/' || slug_service) AS url
FROM my_table
ORDER BY slug_city ASC
`);
for (const entryLink of benefitLocalityLinks) {
origResult.customEntries[entryLink.url] = entryLink;
}
return origResult;
};
//override with custom decorator
plugin.services.settings = () => inited;
return plugin;
};