0

I'm building a single page application with a timeline and loading those objects in from a paginated REST API. I'd like to delete objects that don't exist in the backend anymore or that have been edited by other users, but the solution I can think of seems incredibly naive and frankly expensive.

What I'm thinking of doing is iterating through the loaded objects and checking if they still exist in the backend (or passing all of the object IDs to the backend and getting a list of items that don't exist anymore). This feels like a bad idea.

Webhooks seem like they'd fail if a user isn't online and getting a list of items that have been deleted since the last time the user requested a page load feels incomplete, clunky, and prone to failure.

I feel like part of this could be handled by modifying the frontend's objects on a successful edit/delete response, but that still doesn't handle the cases where another user makes an edit and I need to update every other user's timeline.

Is there a best practice for updating a frontend when objects have been deleted on the backend?

2 Answers 2

1

No, this is not about a 'best practice'. This is about finding a solution that meets your requirements and needs.

Outlook has something like this. Sometimes, when I open an email in a separate window and then I accidentally delete the email in the main window of Outlook, deleting the email in the other window results in an error message like "email is already deleted". That's one solution to this problem: do not update the frontend, but give a message when the user tries to delete or modify an object that is already deleted in the backend.

If it is really critical that a deletion in the backend is communicated immediately with the frontend, a solution might be using a library like SignalR. But the drawback of using this kind of solution is that it takes a lot of effort to design and implement. The decision whether this solution is worth the effort is up to you (or your Product Owner). But this is another solution to your problem: using some library that communicates changes in the backend to the frontend.

There might be other solutions to this problem as well. Important is that you distinguish between solutions and best practices. A solution solves a problem and best practices is about the best way to implement or use a solution.

1

I'm far from a front-end expert, but it sounds like what you need to research is responsive front-end frameworks and/or how to use Observables. There are lots of free resources out there to learn about the latter.

Here is a link that may help you.

In short, you can use an Observable to set up a stream between your back and front end. As your back-end changes so should your front-end. This approach is an alternative to continuously polling the REST API for updates, which is sort of what you described in your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.