I am looking for a pattern that would allow me to better the UX for my users. I have a REST server running behind CloudFront being consumed from a plain React application on the frontend.
I'll simplify my example to illustrate my issue.
I have an endpoint called GET /posts/<id>. When the browser asks for it, it comes with a max=age=180 which means it would get stored in the browser's cache and any subsequent call to GET /posts/<id> will be served from the browser's cache for the duration of those 180 seconds, after which it will hit the CDN again to try and obtain a fresh copy.
That is okay for most users. I don't mind if updates to any post to delay up to 3 minutes before they're propagated to all the users. But there is one user who's the author of this post. That user can make changes to this post using PATCH /posts/<id>. Let's call that user The Editor.
Here's a scenario I have right now:
- The Editor loads up the post page which then calls
GET /posts/5 - The CDN serves the latest copy to the front end.
- the Editor then makes a change to the post and submits it to be back end via
PATCH /posts/5. - The editor then refreshes his browser tab using Command-R (or CTRL-R).
- As a result, the front end then requests
GET /posts/5again -- but gets the stale copy from before the changes because 180 seconds haven't passed yet since the lastGETand theGETissued after thePATCH
What I'd like the experience to be is:
- The Editor loads up the post page which then calls
GET /posts/5 - The CDN serves the latest copy to the front end.
- The editor then makes a change to the post and submits it to be back end via
PATCH /posts/5. - After a Command-R browser tab refresh the
GET /posts/5brings back a copy of the data with the changes the editor made withPATCHright away, regardless of the 180 seconds ofttlbefore a fresh copy can be obtained. - As for the rest of the users, it's perfectly okay for them to wait up to 180 seconds before the change in the post propagates to them when the
GET /posts/5
I am using Axios, but I do not that SWR and React-Query support mutations. To my understanding this would allow the editor to declare a mutation for the object he just PATCH'ed on the server, so that any subsequent calls he makes to GET /posts/5 will be served from there, until a fresher version can be obtained from the backend.
My questions are:
- Can SWR with "mutations" serve the mutated object via the
GET /posts/5transparently? - Will the mutation survive a hard browser tab refresh? or a browser closure, re-opening and subsequent
/GET posts/5? - Is there another pattern/best practice to solve that?