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.
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.
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.
@jack_tmrrw In the latest version of Strapi (v4.5), we’ve seen improvements in the admin panel when data is modified by the server. When new relationships between one entry are created with other entries on the server, we’ve seen the new relationships load automatically.
However, it’s still not resolved across the rest of the data on the Admin Panel’s form.
I maby found a solution.
You could register a injectContentManagerComponent in a plugin and register a react component to inject into a injection zone inside of the ContentManager edit window. inside of the injection zone you could call window.location.reload() what will trigger a reload of the whole page you should be able to see initialData from useCMEditViewDataManager change as soon as you save it should change.
You should be able to use that to check for when a save happens and call a reload.
Worked for me, thanks a lot! Added some custom logic to lifecycle hooks and needed to refresh the page (list view as well) after that, this was exactly what I was looking for.
it works but definitely not ideal. It makes sense for Strapi not wait for result of afterUpdate by default, but it should be configurable. So webhook will cover async communication, and lifecycle hooks will cover sync communication with other systems
Thanks alot, that solved my problem and I can trigger a reload when certain parameters have changed!
Although the build fails when importing the file in the app.js. So I had to put the AfterUpdateReload component directly into the app.js. Not beautiful, but it works.