design-switch.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * Feature switch for enabling the new LibreSpeed design
  3. *
  4. * This script checks for:
  5. * 1. URL parameter: ?design=new or ?design=old
  6. * 2. Configuration file: config.json with useNewDesign flag
  7. *
  8. * Default behavior: Shows the old design
  9. *
  10. * Note: This script is only loaded on the root index.html
  11. */
  12. (function () {
  13. 'use strict';
  14. // Don't run this script if we're already on a specific design page
  15. // This prevents infinite redirect loops
  16. const currentPath = window.location.pathname;
  17. if (currentPath.includes('index-classic.html') || currentPath.includes('index-modern.html')) {
  18. return;
  19. }
  20. // Check URL parameters first (they override config)
  21. const urlParams = new URLSearchParams(window.location.search);
  22. const designParam = urlParams.get('design');
  23. if (designParam === 'new') {
  24. redirectToNewDesign();
  25. return;
  26. }
  27. if (designParam === 'old' || designParam === 'classic') {
  28. redirectToOldDesign();
  29. return;
  30. }
  31. // Check config.json for design preference
  32. try {
  33. const xhr = new XMLHttpRequest();
  34. // Use a synchronous request to prevent a flash of the old design before redirecting
  35. // Bypass stale browser caches so Docker env changes are reflected immediately
  36. xhr.open('GET', 'config.json?_=' + Date.now(), false);
  37. xhr.send(null);
  38. // Check for a successful response, but not 304 Not Modified, which can have an empty response body
  39. if (xhr.status >= 200 && xhr.status < 300) {
  40. const config = JSON.parse(xhr.responseText);
  41. if (config.useNewDesign === true) {
  42. redirectToNewDesign();
  43. } else {
  44. redirectToOldDesign();
  45. }
  46. } else {
  47. // Config not found or error - default to old design
  48. redirectToOldDesign();
  49. }
  50. } catch (error) {
  51. // If there's any error (e.g., network, JSON parse), default to old design
  52. console.log('Using default (old) design:', error.message || 'config error');
  53. redirectToOldDesign();
  54. }
  55. function redirectToNewDesign() {
  56. // Preserve any URL parameters when redirecting
  57. const currentParams = window.location.search;
  58. window.location.href = 'index-modern.html' + currentParams;
  59. }
  60. function redirectToOldDesign() {
  61. // Preserve any URL parameters when redirecting
  62. const currentParams = window.location.search;
  63. window.location.href = 'index-classic.html' + currentParams;
  64. }
  65. })();