How to force XMLHttpRequest from callback to affect caching?

When the browser receives the following HTML header, the Cache-control header is set by the browser for XHTTP requests made in the main area, but not for a request made from a timeout callback. This leads to the fact that the second resource is always loaded from the cache if the cache is absent. Why does including a request in a callback affect cache headers like this?

<!DOCTYPE HTML> <html> <body> <script type="text/javascript"> var get = function (url) { var xhttp = new XMLHttpRequest(); xhttp.open("GET", url, true); xhttp.send(); } get("resource1.html"); // Cache-control set setTimeout(function () { get("resource2.html"); // Cache-control not set }, 10); get("resource3.html"); // Cache-control set </script> </body> </html> 

(I tested this on all computers and browsers that I have, and the results are consistent. The only exception is that Firefox seems to set the Cache-control header for the callback resource if the timeout is set to 0 , which has not yet exists in other browsers).

+6
source share
1 answer

Unable to play now, but try to return home ...

I tested with:

app.js

 router.get(/resource./, function(req, res) { res.setHeader('Cache-Control', 'public, max-age=90'); res.send('<hr/>'); }); router.get('/', function(req, res) { res.render('index'); }); 

index.html

 <!DOCTYPE HTML> <html> <body> <script type="text/javascript"> var get = function (url) { var xhttp = new XMLHttpRequest(); xhttp.open("GET", url, true); xhttp.send(); } get("resource1.html"); // Cache-control set setTimeout(function () { get("resource2.html"); // Cache-control not set }, 10); get("resource3.html"); // Cache-control set </script> </body> </html> 

They all get 200 ...

enter image description here

And cache management

 HTTP/1.1 200 OK X-Powered-By: Express Cache-Control: public, max-age=90 Content-Type: text/html; charset=utf-8 Content-Length: 5 ETag: W/"5-mkFFtL4+3G6hWYdNAMJUPw" Date: Thu, 03 Mar 2016 16:09:48 GMT Connection: keep-alive 

In addition, the magazine:

1st time

 Listening on port 3000 GET / 200 12.544 ms - 499 /resource1.html undefined GET /resource1.html 200 1.389 ms - 5 /resource3.html undefined GET /resource3.html 200 0.353 ms - 5 /resource2.html undefined GET /resource2.html 200 0.233 ms - 5 

2nd time

 GET / 200 1.627 ms - 499 /resource1.html no-cache GET /resource1.html 200 0.427 ms - 5 /resource3.html no-cache GET /resource3.html 200 0.160 ms - 5 /resource2.html no-cache GET /resource2.html 200 0.408 ms - 5 
-1
source

All Articles