speedtest/design-switch.js
Copilot aa84692df3
Some checks failed
Docker / e2e (push) Has been cancelled
Docker / build (./Dockerfile, , ghcr.io/${{ github.repository }}) (push) Has been cancelled
Docker / build (./Dockerfile.alpine, -alpine, ghcr.io/${{ github.repository }}) (push) Has been cancelled
Make USE_NEW_DESIGN deterministic by cache-busting config.json in design switcher (#858)
* Initial plan

* Fix design switch config caching for USE_NEW_DESIGN

Co-authored-by: sstidl <12804296+sstidl@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: sstidl <12804296+sstidl@users.noreply.github.com>
2026-09-27 09:50:52 +02:00

73 lines
2.5 KiB
JavaScript

/**
* Feature switch for enabling the new LibreSpeed design
*
* This script checks for:
* 1. URL parameter: ?design=new or ?design=old
* 2. Configuration file: config.json with useNewDesign flag
*
* Default behavior: Shows the old design
*
* Note: This script is only loaded on the root index.html
*/
(function () {
'use strict';
// Don't run this script if we're already on a specific design page
// This prevents infinite redirect loops
const currentPath = window.location.pathname;
if (currentPath.includes('index-classic.html') || currentPath.includes('index-modern.html')) {
return;
}
// Check URL parameters first (they override config)
const urlParams = new URLSearchParams(window.location.search);
const designParam = urlParams.get('design');
if (designParam === 'new') {
redirectToNewDesign();
return;
}
if (designParam === 'old' || designParam === 'classic') {
redirectToOldDesign();
return;
}
// Check config.json for design preference
try {
const xhr = new XMLHttpRequest();
// Use a synchronous request to prevent a flash of the old design before redirecting
// Bypass stale browser caches so Docker env changes are reflected immediately
xhr.open('GET', 'config.json?_=' + Date.now(), false);
xhr.send(null);
// Check for a successful response, but not 304 Not Modified, which can have an empty response body
if (xhr.status >= 200 && xhr.status < 300) {
const config = JSON.parse(xhr.responseText);
if (config.useNewDesign === true) {
redirectToNewDesign();
} else {
redirectToOldDesign();
}
} else {
// Config not found or error - default to old design
redirectToOldDesign();
}
} catch (error) {
// If there's any error (e.g., network, JSON parse), default to old design
console.log('Using default (old) design:', error.message || 'config error');
redirectToOldDesign();
}
function redirectToNewDesign() {
// Preserve any URL parameters when redirecting
const currentParams = window.location.search;
window.location.href = 'index-modern.html' + currentParams;
}
function redirectToOldDesign() {
// Preserve any URL parameters when redirecting
const currentParams = window.location.search;
window.location.href = 'index-classic.html' + currentParams;
}
})();