Source code
Revision control
Copy as Markdown
Other Tools
<!DOCTYPE html>
<meta charset="utf-8">
<title>performance.getSpeculations() - result is plain, serializable data</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/dispatcher/dispatcher.js"></script>
<script src="/common/utils.js"></script>
<script src="/speculation-rules/resources/utils.js"></script>
<script src="/speculation-rules/prefetch/resources/utils.sub.js"></script>
<script src="support/speculation-measurement-utils.js"></script>
<body>
<script>
setup(() => {
assertSpeculationRulesIsSupported();
assert_true(isSpeculationMeasurementEnabled(),
"SpeculationMeasurement feature must be enabled");
});
// performance.getSpeculations() returns dictionaries, so the result and its
// entries are plain objects. Every member is always present, and is null when
// it has no value.
const kSpeculationDataKeys =
['navigationDestinationURL', 'navigations', 'preconnects', 'preloads'];
const kEntryKeys = {
preloads: ['as', 'crossorigin', 'earlyhint', 'url', 'used'],
preconnects: ['crossorigin', 'earlyhint', 'origin'],
navigations: ['eagerness', 'tags', 'type', 'url'],
};
// Records a used and an unused preload, a preconnect and a prefetch, then
// returns the resulting performance.getSpeculations() snapshot.
async function getPopulatedSpeculations(t) {
const {href} = await addPreloadAndWait({t, as: 'style'});
await usePreload({t, as: 'style', href});
await addPreloadAndWait({t, as: 'script'});
await addPreconnect({t, href: uniquePreconnectUrl(),
crossorigin: 'anonymous'});
const prefetchUrl = uniqueNavigationUrl('serialization');
insertSpeculationRules({
prefetch: [{
urls: [prefetchUrl],
eagerness: 'immediate',
tag: 'serialization'
}]
});
await waitForSpeculationMatching(t, prefetchUrl);
return performance.getSpeculations();
}
function assertPlainObjectWithKeys(value, keys, description) {
assert_equals(Object.getPrototypeOf(value), Object.prototype,
`${description} should be a plain object`);
assert_array_equals(Object.keys(value).sort(), keys,
`${description} should have all members as own properties`);
}
promise_test(async t => {
const data = await getPopulatedSpeculations(t);
assertPlainObjectWithKeys(data, kSpeculationDataKeys,
"getSpeculations() result");
assert_equals(data.navigationDestinationURL, null,
"navigationDestinationURL should be null before navigating away");
for (const [name, keys] of Object.entries(kEntryKeys)) {
const entries = data[name];
assert_equals(Object.getPrototypeOf(entries), Array.prototype,
`${name} should be a plain array`);
assert_greater_than(entries.length, 0, `${name} should not be empty`);
for (const entry of entries) {
assertPlainObjectWithKeys(entry, keys, `${name} entry`);
}
}
assert_true(data.preloads.some(p => typeof p.used === 'number'),
"a used preload should have a numeric used timestamp");
assert_true(data.preloads.some(p => p.used === null),
"an unused preload should have a null used timestamp");
}, "getSpeculations() returns plain objects with all members present");
promise_test(async t => {
const data = await getPopulatedSpeculations(t);
assert_object_equals(JSON.parse(JSON.stringify(data)), data,
"JSON round trip should preserve all members");
}, "getSpeculations() result can be serialized with JSON.stringify()");
promise_test(async t => {
const data = await getPopulatedSpeculations(t);
assert_object_equals(structuredClone(data), data,
"structuredClone() should preserve all members");
}, "getSpeculations() result can be structured-cloned");
promise_test(async t => {
const first = await getPopulatedSpeculations(t);
const preloadCount = first.preloads.length;
const preload = first.preloads[0];
const preloadUrl = preload.url;
const navigation = first.navigations.find(n => Array.isArray(n.tags));
assert_not_equals(navigation, undefined, "a navigation with tags is needed");
const navigationTags = [...navigation.tags];
// Mutate the result at every level: top-level members, entries, and arrays
// nested in entries. None of this should leak into later results.
navigation.tags.push('mutated');
first.preloads.length = 0;
const second = performance.getSpeculations();
assert_not_equals(second, first, "each call should return a new object");
assert_equals(second.preloads.length, preloadCount,
"truncating a returned array should not affect later results");
assert_equals(second.navigationDestinationURL, null,
"changing a returned member should not affect later results");
assert_true(second.preloads.some(p => p.url === preloadUrl),
"changing a returned entry should not affect later results");
const secondNavigation =
second.navigations.find(n => n.url === navigation.url);
assert_array_equals(secondNavigation?.tags, navigationTags,
"changing an array nested in a returned entry should not affect later " +
"results");
}, "getSpeculations() returns a fresh copy on each call");
</script>
</body>