Skip to content

Commit

Permalink
[ResourceTiming] Add cache busting to ResourceTiming WPTs
Browse files Browse the repository at this point in the history
In order to reliably force loading a given resource, the helper
functions in resource-loaders.js now add a "cache_bust" search
parameter to URLs before loading them. This way, cache state
won't leak into and interfere with running multiple ResourceTiming
tests.

Bug: 1171767
Change-Id: I9927963b0c467122c478cb6b137b7113487219d9
GithubIssue: w3c/resource-timing#254
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2826596
Reviewed-by: Yoav Weiss <[email protected]>
Reviewed-by: Nicolás Peña Moreno <[email protected]>
Commit-Queue: Tom McKee <[email protected]>
Cr-Commit-Position: refs/heads/master@{#877940}
NOKEYCHECK=True
GitOrigin-RevId: 677c649ca83de2ef8a5acb96520a04edcf398806
  • Loading branch information
tommckee1 authored and copybara-github committed Apr 30, 2021
1 parent 6b38c6d commit 2f849bc
Showing 1 changed file with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
const load = {
_cache_bust_value: Math.random().toString().substr(2),
cache_bust: path => {
let url = new URL(path, location.origin);
url.hash += `cache_bust=${load._cache_bust_value++}`;
return url.href;
},

// Returns a promise that settles once the given path has been fetched as an
// image resource.
image: path => {
return new Promise(resolve => {
const img = new Image();
img.onload = img.onerror = resolve;
img.src = path;
img.src = load.cache_bust(path);
});
},

Expand All @@ -17,7 +24,7 @@ const load = {
<style>
@font-face {
font-family: ahem;
src: url('${path}');
src: url('${load.cache_bust(path)}');
}
</style>
<div style="font-family: ahem;">This fetches ahem font.</div>
Expand All @@ -34,7 +41,7 @@ const load = {
const link = document.createElement("link");
link.rel = "stylesheet";
link.type = "text/css";
link.href = path;
link.href = load.cache_bust(path);

const loaded = new Promise(resolve => {
link.onload = link.onerror = resolve;
Expand All @@ -52,7 +59,7 @@ const load = {
const loaded = new Promise(resolve => {
frame.onload = frame.onerror = resolve;
});
frame.src = path;
frame.src = load.cache_bust(path);
document.body.appendChild(frame);
await loaded;
document.body.removeChild(frame);
Expand All @@ -65,7 +72,7 @@ const load = {
const loaded = new Promise(resolve => {
script.onload = script.onerror = resolve;
});
script.src = path;
script.src = load.cache_bust(path);
document.body.appendChild(script);
await loaded;
document.body.removeChild(script);
Expand Down

0 comments on commit 2f849bc

Please sign in to comment.