Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extension points / hooks. #165

Open
ilnytskyi opened this issue Apr 3, 2024 · 1 comment
Open

Add extension points / hooks. #165

ilnytskyi opened this issue Apr 3, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@ilnytskyi
Copy link
Contributor

ilnytskyi commented Apr 3, 2024

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

#109
#60

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;
};
@boazpoolman boazpoolman added the enhancement New feature or request label Apr 3, 2024
@boazpoolman
Copy link
Member

Agreed. This should be added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants