mattmilburn/strapi-plugin-preview-button

Preview button not appearing if you extend the plugin with async code

bskuratov opened this issue · 3 comments

Hello! I could not extend the plugin with the async code using the new "plugin/preview-button/before-build-url" hook.

Sync code works perfectly fine, but Strapi currently returns a minimal amount of fields for the relations. So to overcome that, we need to fetch particular fields of relations. If I add async code, the URL returned within the hook is correct but Live View button doesn't show on the admin FE. I have briefly checked the plugin admin code and the URL is returned as "undefined" assume because it's not ready to handle async code.

Here is the implementation:

 // ...
  bootstrap(app) {
    app.registerHook("plugin/preview-button/before-build-url", async ({ state, data }) => {
      const url = await parseUrlRelation(state, data);

      return {
        state: {
          ...state,
          url,
        },
      };
    });
  },
 // ...

And in "parseUrlRelation":

const parseUrlRelation = async (config, data) => {
  // ...

  for (const relation of relations) {
    // ...
    
    // get relations data and modify url
    const entity = await request(`/get-my-relation-data`, {
      fields: [field]
    });
    url = url.replace(relation, entity[field]);
  }

  return url;
};

Am I missing something or new hook just can't handle it? If so, any idea how to achieve that? Thanks!

Hi! I need the same feature. In data Strapi is not sending enough information, so I must fetch more info about a specific entity.

Hi @bskuratov @benrychter Thank you for posting this issue. This is now fixed in the latest version 1.1.3 🎉 Let me know how it goes for you.

Hi @mattmilburn Thanks so much for the update, just tested it, all works perfectly.