0

I don't know how to ask this question but, i am developing a single page application(SPA) using nodejs on the server side and whenever the data gets updated the user gets informed, however if the user has refreshed wouldn't the json data and every script file just vanish an get requested from the server again?

How can i prevent the javascript files and specifically the file that has the json data from being requested again on page refresh?

is there a way to solve this problem?

user1492051
  • 886
  • 8
  • 22
  • Only the client-side things will be refreshed, server-side files will do whatever the server says to do. – Matt Ellen Jan 12 '14 at 15:06
  • Have you actually observed and experienced a problem with this? – JAL Jan 12 '14 at 15:07
  • @JAL no, i am just learning and thinking about it i thought page refresh means request all files again – user1492051 Jan 12 '14 at 15:09
  • It sounds like you're modifying 'the JSON data' in te browser and expecting it to be persistent without saving to localstorage or persisting on the server via AJAX, or something. So your question is a bit unclear. Hope you got the answer you needed, anyway. – JAL Jan 12 '14 at 16:48
  • @JAL yes, but the data doesnt change often, on average once every 3-5 minutes, and json file is realtively too big, and i dont want paging that way the apllications feels like its on desktop even though it loads the data remotely, so what i wanted is load all data at once and never reload it on refresh – user1492051 Jan 12 '14 at 17:15

4 Answers4

1

JavaScript files are not special. Just like images, style sheets, and HTML files, they get re-requested as necessary by the browser.

And so the same techniques for minimizing re-retrieval of them apply. The browser can reuse its cached copy of the file if you configure your web server to set appropriate caching headers when responding with the file data (provided the browser still has a copy).

You can see an example of this on the Google Libraries site. If you request a specific version of a library file (say, jQuery 1.10.1) when your web console open to the network tab, you'll see that Google returns it with these headers (irrelevant ones omitted):

Age:           238894
Cache-Control: public, max-age=31536000
Date:          Thu, 09 Jan 2014 20:47:08 GMT
Expires:       Fri, 09 Jan 2015 20:47:08 GMT
Last-Modified: Tue, 09 Jul 2013 11:31:25 GMT

Note that the file is allowed to be cached, without revalidation, for a year. So if the user refreshes the page, the browser can reuse its cached copy (if it has one). (This is not what Google does if you use one of the wildcard "any version of jQuery 1.10" URLs, because of course the tip version changes...)

Some browsers may bypass their cache with a refresh (particularly a "force" refresh like Ctrl+F5). In that case, they may at least send an If-Modified-Since request.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

If you want to prevent cache and reload javascript at every request, make suer you use the correct header:

Cache-Control:max-age=0 

Your browser will undertand to refresh all resources at every request.

For a better understanding about cache, please give a look at this A/Q

Community
  • 1
  • 1
StarsSky
  • 6,721
  • 6
  • 38
  • 63
1

As for json data, you can save it in local storage (http://www.w3schools.com/html/html5_webstorage.asp)

1

Perhaps you can try Application Cache and Local storage.