Webhooks in config?

default discord avatar
SuddenDev
2 years ago
1 1

So hi again. I need to use webhooks to re-deploy my repo on vercel and fetch the latest data. I tried to use the afterChange hook in the payload.config file, but unfortunately it did not work.

[22:09:40] INFO (payload): Starting Payload...
[22:09:42] ERROR (payload): There were 1 errors validating your Payload config
[22:09:42] ERROR (payload): 1: "hooks.afterChange" is not allowed
[nodemon] app crashed - waiting for file changes before starting...

The documentation is a bit fuzzy on that one, What can I use? My code looks like this:

payload.config.js
-------------------

...
const afterChange = async ({ operation }) => {
  console.log(operation);
};
...

...
  hooks: {
    afterChange: [afterChange]
  },
...

Any ideas or recomendations on how to do that?
I do think it makes the most sense to have a global hook or something to detect changes.

Have a nice weekend!
Cheers

  • discord user avatar
    jmikrut
    Payload Team
    2 years ago

    That hook is specific to fields, collections, and globals. My suspicion is that you might be applying it to the top level config. Try it on a collection basis?

    4 replies
  • default discord avatar
    SuddenDev
    2 years ago

    Oh I see. I just thought instead of adding the same function to all my collections / globals I could just add it once to the global config. What hooks are actually available in the config then? The docs are a bit too vague on that imo.

  • discord user avatar
    jmikrut
    Payload Team
    2 years ago

    Totally see what you're saying. That could be possible as a future enhancement. Top-level config hooks are not documented right now because there is only 1 at the moment (afterError) which needs a bit more testing before we put it in the docs.

    In cases like yours, we suggest writing your hook in a separate file which can then be imported / used in all relevant collections. In our projects, this is typically preferable because you might not need that hook to be run on all collections. For example, if you had a Users or Orders collection which do not need to trigger a frontend rebuild.

    If you still would prefer a Payload-wide afterChange hook I am sure we could figure out how to implement it though!

  • default discord avatar
    SuddenDev
    2 years ago

    Thanks for your quick answers! Most CMS' that I know use like a global setting for hooks, but I do absolutly see your point with your User / Orders collections example.
    For now I'll just continue with your suggestion, since I just need to do that for a couple of files.

    I do think it would be good to improve the docs about the hooks still experimental (or a future feature idea) in the main config.payload.js. Right now it reads like you can just use whatever the other types (collections, globals, fields) accept.
    Thanks!

  • discord user avatar
    jmikrut
    Payload Team
    2 years ago

    Totally. We'll shore up those docs ASAP and we'll likely just go ahead and add some documentation around that one Payload-wide hook we do have now as well.

    Thanks for pointing that out!

Open the post
Continue the discussion in GitHub
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.