Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Why does the afterRead hook run multiple times for just one page load?

discord user avatar
alessiogr
2 years ago
11

It's run a total of 15 (!!) times for just one page reload, even though I have attached that afterRead hook to just one single field. Is it supposed to work that way?



The collection I'm using:

https://paste.gg/p/anonymous/6ba997cb994d4a32bbe29d0f75bb87b9

First 5 times siblingdata works as usual. Last 10 times siblingdata is empty.

  • default discord avatar
    jessrynkar2 years ago

    Hey

    @360823574644129795

    , I just tried this out and got 6 times... which seems a lot, all of them had siblingData however

  • discord user avatar
    alessiogr
    2 years ago

    hey hey, is there a reason why it runs multiple times? Or should it only run one time.



    Will test it out later again - maybe it got better in a newer version of payload

  • default discord avatar
    jessrynkar2 years ago

    Okay I've done a bit of learning from the team - if you are reloading the page in the admin UI it is not unexpected behavior, this is because there are many other variables that can cause it to run extra times including versions, preferences, access control, doc relationships etc.



    If you hit the api route directly, you should see it only run once (which I found to be the case when I tested it).



    If you get a chance to test again let me know what you find

  • default discord avatar
    notchrlast year
    @360823574644129795

    I have a collection with multiple items. The collection config has a field with an afterRead hook



    Is it normal that it fires for every item on the collection when viewing the items of the collection



    / Is there a way to make it only run when viewing a particular item



    like not a specific item, but just when editing an item

  • default discord avatar
    tinoutilast year

    Out of curiosity, what's your use case

    @1049775120559898725

    ? 👀

  • discord user avatar
    alessiogr
    last year

    With items, do you mean documents?

  • default discord avatar
    notchrlast year
    @360823574644129795

    Yess



    @233209174069084161

    We have a separate API that stores some user information



    We want to call that API, pass an id stored on the payload user, and get back an array of some data



    For each item in the array, we want to display a field

  • default discord avatar
    tinoutilast year

    Interesting, definitely sounds like the perfect application for afterRead field hook 😅



    Is there anything in

    req

    that might be different when on the edit page maybe, that you could use to condition the hook? 🤔

  • default discord avatar
    hristo600411 months ago
    @486710146651652106

    sorry for bumping this old topic, but did you find a solution on why is it firing up in the list view? I have the same issue,

    afterRead

    fires for every item in the collection and it's not necessary. I'd like it to fire only when I open the item for editing. Did you find a way to fix this? Thank you. 🙂

  • default discord avatar
    maykonoliveira.11 months ago

    I was talking about that point yesterday on 3.0 feedback channel. My hook runs 3 times if I force a reload, but if I do a http request it runs only once.

  • default discord avatar
    hristo600411 months ago

    Thanks

    @535474572586254338

    In my hooks I'm using the local API to create and update collection items. The updating part is what's causing issues, it's executed every time I open the item itself or the collection list view. I'll try to do it over HTTP.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.