The selectURL()
method of the WindowSharedStorage
interface executes a URL Selection operation that is registered in a module added to the current origin's SharedStorageWorklet
.
Note: The URL Selection output gate is used to select a URL from a provided list to display to the user, based on shared storage data.
selectURL(name, urls)
selectURL(name, urls, options)
A Promise
that fulfills with a FencedFrameConfig
object or a string representing a URL, depending on the value of the resolveToConfig
option.
function getExperimentGroup() {
return Math.round(Math.random());
}
async function injectContent() {
await window.sharedStorage.worklet.addModule("ab-testing-worklet.js");
window.sharedStorage.set("ab-testing-group", getExperimentGroup(), {
ignoreIfPresent: true,
});
const fencedFrameConfig = await window.sharedStorage.selectURL(
"ab-testing",
[
{ url: `https://your-server.example/content/default-content.html` },
{ url: `https://your-server.example/content/experiment-content-a.html` },
],
{
resolveToConfig: true,
},
);
document.getElementById("content-slot").config = fencedFrameConfig;
}
injectContent();
See the Shared Storage API landing page for a walkthrough of this example and links to other examples.