P
P
pavelkunyavskiy2018-02-17 23:30:34
Vue.js
pavelkunyavskiy, 2018-02-17 23:30:34

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)
}

Everything works, the application does not climb on the server if there is already a filled state. But what about data updates? For example, a person opened a site, and after about a minute a new article is added. He will not see it, since his data will be cached. The only thing that comes to mind is to periodically reset the state on the client. For example, every 5 minutes.
How to do it right? And what are the other ways? Maybe it’s necessary to request something differently, or some other practices unknown to me.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Kulakov, 2018-02-18
@kulakoff Vue.js

1. Periodic request to the server
2. Web sockets, if you need to receive data in real time

M
markmariner, 2018-02-20
@markmariner

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 question

Ask a Question

731 491 924 answers to any question