Answer the question
In order to leave comments, you need to log in
How to periodically reset the state? How to avoid unnecessary requests to the API server?
There is a small application, in fact a content site.
In order not to send a request for JSON with each transition along the route, I do a check, like
if (!store.state.articles.length) {
axios.get...
store.commit('SET_ARTICLES', data)
}
Answer the question
In order to leave comments, you need to log in
1. Periodic request to the server
2. Web sockets, if you need to receive data in real time
You should configure your backend to return 304 Not Modified status if the data hasn't changed.
That is, you send a request to the server every time ("every time you go through the route"). If the data has changed, then the server returns new data to you, if not, it returns an empty response with the status “No changes”. How exactly to do this is the task of backend development.
Here is more on this topic: https://ruhighload.com/post/%D0%9A%D0%B5%D1%88%D0%...
If you need to update without switching to another page instantly (for example, for chat messages), then you need to use the WebSockets protocol, with which you will subscribe to changes, and the backend will independently notify the frontend about data changes. There is a lot of material about websockets, the implementation depends on the language. About JS: https://learn.javascript.ru/websockets
You can set up simple caching on the backend - the request will be processed, but the database will not be accessed, so it's not scary that you will send requests often.
Periodic (by timer) requests to the server is the most inelegant solution, you should not use it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question