Worried about making repeated requests for same resource slowing down your site? >You need not

Before I make things clear, let me present you the situation I faced, its Angular, but the solution in generic.

I have two components(pages) in Angular, which needs same data, so generally we fetch it in service, we get observable or promise and provide to components when needed.

In component use Async pipe, we can show the data, however the async pipe makes a request to promise or observer every time we navigate to that page from other page, this made me worry, about making another http request when I had the data Already.

Solution is we are living with browser and it is smart, it knows we are requesting same resource again and we have the response in cache, so in the request to server it adds another question, whether the cache data has been modified or not since last request.

If the data is not modified since last call, server sends 304 response without response body and browser uses its cached response.

However we are still making a request, and that can be avoided if the the server follows best practices and specify Cache-Control or Expires directives on its responses so that the client knows how long they are valid and can skip the validation step and simply use the cached response immediately without making a request at all :).

I love learning