0

I have a Django project with a view that has a lot of python code to calculate data for specific table in its template (2-4 seconds).

To improve user experience I decided to load template immediately with empty table and get necessary data via jQuery $.getJSON request.

$(document).ready(function() {
    $(function() {
       $.getJSON(devices_list_JSON, function(data){
         // fill table data
         ...
       }
    }
}

I've done so and it works fine except of one issue.

When I go to some page and back to my view using browser Back button, browser make JSON request again. And I want to use cached version as it was in a view that calculated all data in its python code.

How can I do so?

(I use Google Chrome 68.0.3440.106 browser)

kostr22
  • 576
  • 7
  • 27
  • in your `ajax` call, you can set `cache: true`, the request will however still be sent, but you get a cached answer. – toffler Aug 23 '18 at 13:32
  • @toffler Should I do it through "$.ajaxSetup({ cache: true });"? Cause in this case cached answer isn't used somehow. Also I don't want to make JSON request completely to free server from extra recalculation – kostr22 Aug 23 '18 at 13:50
  • yes, `$.ajaxSetup({ cache: true});` should do the work. You can test if it works with measuring the response time. First measure the time if you just reload the page, then measure the time if you reload it with `ctrl + F5`, which will reset your cache – toffler Aug 23 '18 at 13:55
  • @toffler I've travelled to different page. While I was there, I've changed my database to make json response differnet. Then I hit "Back" button and in my table I've got updated data. So it seems like cached version isn't even used – kostr22 Aug 23 '18 at 14:13

0 Answers0