Files
sousa-gecko/testing/web-platform/tests/speculation-rules/resources/utils.js
T
Sam Sneddon 5bbaff2951 Bug 2032349 [wpt PR 59217] - Speculation Rules prerender tests should check for support, a=testonly
Automatic update from web-platform-tests
Speculation Rules prerender tests should check for support

We're seeing timeouts on the STP bots, because spending over an hour
timing out on speculation rules tests just eats into the timeout.

--

wpt-commits: 9f7999f4b3dd412a338f3231228e64230d6a6531
wpt-pr: 59217
2026-04-23 10:14:37 +00:00

62 lines
2.4 KiB
JavaScript

globalThis.assertSpeculationRulesIsSupported = (...types) => {
assert_implements(
'supports' in HTMLScriptElement,
'HTMLScriptElement.supports must be supported');
assert_implements(
HTMLScriptElement.supports('speculationrules'),
'<script type="speculationrules"> must be supported');
for (const type of types) {
if (type === 'prerender') {
assert_implements(
'prerendering' in document,
'Prerendering must be supported (document.prerendering)');
}
}
};
// If you want access to these, be sure to include
// /html/browsers/browsing-the-web/remote-context-helper/resources/remote-context-helper.js.
// So as to avoid requiring everyone to do that, we only conditionally define this infrastructure.
if (globalThis.RemoteContextHelper) {
class PreloadingRemoteContextWrapper extends RemoteContextHelper.RemoteContextWrapper {
/**
* Starts preloading a page with this `PreloadingRemoteContextWrapper` as the
* referrer, using `<script type="speculationrules">`.
*
* @param {"prefetch"|"prerender"} preloadType - The preload type to use.
* @param {object} [extrasInSpeculationRule] - Additional properties to add
* to the speculation rule JSON.
* @param {RemoteContextConfig|object} [extraConfig] - Additional remote
* context configuration for the preloaded context.
* @returns {Promise<PreloadingRemoteContextWrapper>}
*/
addPreload(preloadType, { extrasInSpeculationRule = {}, ...extraConfig } = {}) {
const referrerRemoteContext = this;
return this.helper.createContext({
executorCreator(url) {
return referrerRemoteContext.executeScript((url, preloadType, extrasInSpeculationRule) => {
const script = document.createElement("script");
script.type = "speculationrules";
script.textContent = JSON.stringify({
[preloadType]: [
{
source: "list",
urls: [url],
...extrasInSpeculationRule
}
]
});
document.head.append(script);
}, [url, preloadType, extrasInSpeculationRule]);
},
extraConfig
});
}
}
globalThis.PreloadingRemoteContextHelper = class extends RemoteContextHelper {
static RemoteContextWrapper = PreloadingRemoteContextWrapper;
};
}