<div id="scr-lazy-host"></div>
<script>
(() => {
"use strict";
if (window.__scrLazyLoaderStarted) return;
window.__scrLazyLoaderStarted = true;
const host = document.getElementById("scr-lazy-host");
const endpoint = "/store-country-redirect/content";
const keepPath = false;
const cacheVersion = "1.20";
const sourceUrl = window.location.pathname + window.location.search;
const hasHandoff = new URLSearchParams(window.location.search).has("scr-store-handoff");
const cacheScope = keepPath || hasHandoff ? sourceUrl : window.location.host;
const cacheKey = "scr.lazy.html." + cacheVersion + "." + cacheScope;
const cacheLifetimeMs = 4 * 60 * 1000;
function mount(html) {
if (!host || !html) return;
const template = document.createElement("template");
template.innerHTML = html;
const scripts = Array.from(template.content.querySelectorAll("script"));
scripts.forEach(script => script.remove());
host.appendChild(template.content);
scripts.forEach(source => {
const script = document.createElement("script");
Array.from(source.attributes).forEach(attribute =>
script.setAttribute(attribute.name, attribute.value));
script.textContent = source.textContent;
host.appendChild(script);
});
}
function readCache() {
if (hasHandoff) return null;
try {
const item = JSON.parse(window.sessionStorage.getItem(cacheKey));
if (!item || item.expiresAt <= Date.now() || typeof item.html !== "string") {
window.sessionStorage.removeItem(cacheKey);
return null;
}
return item.html;
} catch {
return null;
}
}
function writeCache(html) {
if (hasHandoff) return;
try {
window.sessionStorage.setItem(cacheKey, JSON.stringify({
expiresAt: Date.now() + cacheLifetimeMs,
html: html
}));
} catch {}
}
async function load() {
const cached = readCache();
if (cached !== null) {
mount(cached);
return;
}
const controller = new AbortController();
const timeout = window.setTimeout(() => controller.abort(), 12000);
try {
const separator = endpoint.includes("?") ? "&" : "?";
const response = await fetch(endpoint + separator + "returnUrl=" + encodeURIComponent(sourceUrl), {
credentials: "same-origin",
headers: {
"Accept": "text/html",
"X-Requested-With": "XMLHttpRequest",
"X-Store-Country-Redirect": "1"
},
signal: controller.signal
});
if (!response.ok && response.status !== 204) return;
const html = response.status === 204 ? "" : await response.text();
writeCache(html);
mount(html);
} catch (error) {
if (error?.name !== "AbortError")
console.debug("Store Country Redirect lazy load skipped.");
} finally {
window.clearTimeout(timeout);
}
}
const schedule = () => {
if ("requestIdleCallback" in window)
window.requestIdleCallback(load, { timeout: 1500 });
else
window.setTimeout(load, 250);
};
if (document.readyState === "loading")
document.addEventListener("DOMContentLoaded", schedule, { once: true });
else
schedule();
})();
</script>