Skip to content

Commit

Permalink
Add test that loads twice from the same preloaded URL
Browse files Browse the repository at this point in the history
This test is meant to support the discussion in
whatwg/fetch#590.

The test accesses a URL that returns an integer that gets incremented
with each request (starting with 0).

The test preloads it once, and then loads it twice.

In Firefox, the test returns 0,0
In Chrome, the test returns 0,1 (preload is used for one request)
In Safari, the test returns 1,2 (preload makes an unused request)
  • Loading branch information
noamr committed Sep 27, 2021
1 parent 13f67ce commit b52a8f3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
48 changes: 48 additions & 0 deletions preload/preload-edge-cases.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<title>Test cases for scenarios not defined in the preload spec</title>
<script src="/resources/testharness.js" a></script>
<script src="/resources/testharnessreport.js"></script>
<script>
const serveUrl = './resources/inc.py'
function uuidv4() {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}

async function load(key) {
const response = await fetch(`${serveUrl}?key=${key}`, {cache: 'force-cache'})
return await response.text()
}

function preload(key) {
const link = document.createElement('link')
link.rel = "preload"
link.href = `${serveUrl}?key=${key}`
link.as = "fetch"
link.setAttribute("crossorigin", "crossorigin")
document.head.appendChild(link)
return new Promise(resolve => {
link.addEventListener("load", resolve)
})
}

promise_test(async() => {
const key = uuidv4()
const p = await preload(key)
const results = [await load(key), await load(key)]

const expected = {
// Preload result is used for all subsequent requests
gecko: ["0", "0"],

// Preload is used once
blink: ["0", "1"],

// Preload is not used in subsequent requests
webkit: ["1", "2"]
}

assert_array_equals(results, expected.gecko)
})
</script>
14 changes: 14 additions & 0 deletions preload/resources/inc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from datetime import datetime

# datetime object containing current date and time
def main(request, response):
stash = request.server.stash
key = request.GET[b"key"]
prev = stash.take(key)
value = 0
if prev is not None:
value = prev + 1
stash.put(key, value)
headers = [(b"Content-Type", b"text/plain"),
(b"Cache-Control", b"cache")]
return headers, str(value)

0 comments on commit b52a8f3

Please sign in to comment.