G
G
gleendo2017-08-01 16:17:07
JavaScript
gleendo, 2017-08-01 16:17:07

How to make xhr requests not cached?

At the moment, the chat (from Ilya Kantor's screencast) does not work quite correctly. Messages are sent in turn, then to the interlocutor, then to me. The comments suggested that because xhr requests are cached.
Solution: add a unique id to the request path.

xhr.open("GET", "/subscribe?id=" + Math.random().toString().split('.')[1], true);

Is it possible somehow without this generation of a pseudo-random identifier? Is there another way?
In the code on github, Ilya has a slightly modified version of the chat, which is shown in the video. Everything works exactly the same with errors.
But there is one notable line
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // Чтобы что то не кэшировалось... К чему именно это относится, хз. голова уже кругом от непонимания всего и вся.

I don't know if this has anything to do with the above xhr request, but without generating a unique request path, caching occurs.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pushkarev, 2017-08-01
@evgeniy8705

xhr.open("GET", "/subscribe?_=" + new Date().getTime(), true);
// или
xhr.setRequestHeader('Cache-Control', 'no-cache');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question