payloadcms / plugin-seo

The official SEO plugin for Payload
97 stars 15 forks source link

Access to the local payload API inside a generate function #10

Closed matthijs166 closed 2 years ago

matthijs166 commented 2 years ago

For a generate function I want to get data from the settings page like this.

      generateDescription: async ({ doc, locale }) => {
        const settings = await payload.findGlobal({
          slug: 'settings',
          overrideAccess: true,
          depth: 1,
        });
        return settings?.fields?.defaultDescription?.value;
      }

Thing is that its running client side so is there a way to access the payload api easy in the generate function?

Normaly it would be accesable via the req.payload object.

jacobsfletch commented 2 years ago

Hi @matthijs166 I'm digging into this one. The question really comes down to whether you should import Payload from your callback, or the the React component imports it and sends it through to you.

jacobsfletch commented 2 years ago

Ok @matthijs166 after second thought, this one is pretty simple. We're in a React component, so we won't have access to Payload local API (we're in a browser). Instead, use would just use the REST API or GraphQL as needed.

matthijs166 commented 2 years ago

Thanks! I see that I can use fetch directly without the need of authentication because the auth is set inside a cookie.

Would be cleaner to get the values without an extra fetch but dont think its easy to implement. Or it need to be build in a way like yoast SEO with the use of snippets variabels: https://yoast.com/help/list-available-snippet-variables-yoast-seo/

jacobsfletch commented 2 years ago

@matthijs166 YES! That is exactly what we've got in store for the future of this plugin. This same pattern will apply across a few other plugins as well, like the form builder.