How to trigger a page refresh in the Admin UI after a Lifecycle Hook?

System Information
  • Strapi Version: 4.3.4
  • Operating System: macOS
  • Database: PostgreSQL
  • Node Version: 14.20.0
  • NPM Version: 6.14.17
  • Yarn Version: 1.22.19

I’ve implemented an afterUpdate hook on Collection Type A that creates new items in Collection Type B, during the execution of the function, it creates relationships between the item in Collection Type A and the new items in Collection Type B.

However, the UI will not show updates to the data (including the new relationships) without the user refreshing the page (not very intuitive). How do I tell the Admin UI from the lifecycle hook that a refresh is required?

I’ve seen the UI show a message when the server needs to restart, so I’m hoping this has been solved already.

Have you find the solution to this problem ?
@DMehaffy @Eventyret Can you please help here ?

1 Like

I haven’t found a solution to this problem as of yet

There isn’t really a way for the afterX lifecycles as this part is done after the response is already being sent to the user

Thanks for your reply @DMehaffy,

Is there by any chance a hook or another strategy in the admin/frontend for actions after a response is received?

@dallasclark

Like @DMehaffy said afterX runs after the response has been sent.
Depending on your “willingness” you can workaround that issue by simply spin up a loading circle after the user saves your entity and wait for a second until you perform the refresh yourself.

The better but not as simple solution would be to let your afterCreate fill an attribute that onSave is always getting nulled.
That way you can follow up a short waiting time until you fetch the entry again, check for your afterCreateDone-Attribute and if this is set you can safely perform a refresh.

When I use the afterUpdate lifecycle, the query to the API does not return a response until this lifecycle hook is completed. My afterUpdate lifecycle hook takes roughly 50-60 seconds to complete so the UI freezes whilst the “Save” or “Publish” buttons are showing the loading animation.

When an afterUpdate lifecycle hook fails, the response fails. So I’m not confident the answers above about the response is already sent to the user before the lifecycle hook is triggered are accurate.

It would be great if I can either:

  • Send back a response that will trigger an update to the UI (if this frontend logic is already implemented), OR
  • Create frontend logic to refresh/update the data in the UI after every publish or save request

The problem is Strapi uses CSR (React) and we can’t have the frontend constantly pinging. I would suggest implementing a try/catch in your afterX to handle any errors so the response is returned on time.

@DMehaffy there are no errors …

The afterUpdate lifecycle makes connections to external services that are processing the data. The front end is showing loading animations for the full duration whilst this lifecycle is processing the request.

Did anyone here ever get this to work? If so, could you provide a code example?

Unfortunately not … yet