stale-script.html (1737B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <title>Tests Stale While Revalidate works for scripts</title> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <script src="/common/utils.js"></script> 7 <body> 8 <script> 9 10 const request_token = token(); 11 const script_src = "./resources/stale-script.py?token=" + request_token; 12 13 // The script above will call report() via a uniquely generated ID on the 14 // subresource. If it is a cache hit, the ID will be the same and 15 // |last_modified_count| won't be incremented. 16 let last_modified; 17 let last_modified_count = 0; 18 function report(mod) { 19 if (last_modified == mod) 20 return; 21 last_modified = mod; 22 last_modified_count++; 23 } 24 25 let loadScript = async () => { 26 let script = document.createElement("script"); 27 let script_loaded = new Promise(r => script.onload = r); 28 script.src = script_src; 29 document.body.appendChild(script); 30 await script_loaded; 31 }; 32 33 promise_test(async t => { 34 await new Promise(r => window.onload = r); 35 36 await loadScript(); 37 assert_equals(last_modified_count, 1, '(initial version loaded)'); 38 39 await loadScript(); 40 assert_equals(last_modified_count, 1, '(stale version loaded)'); 41 42 // Query the server again and again. At some point it must have received the 43 // revalidation request. We poll, because we don't know when the revalidation 44 // will occur. 45 while(true) { 46 await new Promise(r => step_timeout(r, 25)); 47 let response = await fetch(script_src + "&query"); 48 let count = response.headers.get("Count"); 49 if (count == '2') 50 break; 51 } 52 53 await loadScript(); 54 assert_equals(last_modified_count, 2, '(revalidated version loaded)'); 55 56 }, 'Cache returns stale resource'); 57 58 </script> 59 </body>