-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathdesign-switch.js
More file actions
72 lines (64 loc) · 2.4 KB
/
design-switch.js
File metadata and controls
72 lines (64 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* 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
xhr.open('GET', 'config.json', 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;
}
})();