design-switch.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. xhr.open('GET', 'config.json', false);
  36. xhr.send(null);
  37. // Check for a successful response, but not 304 Not Modified, which can have an empty response body
  38. if (xhr.status >= 200 && xhr.status < 300) {
  39. const config = JSON.parse(xhr.responseText);
  40. if (config.useNewDesign === true) {
  41. redirectToNewDesign();
  42. } else {
  43. redirectToOldDesign();
  44. }
  45. } else {
  46. // Config not found or error - default to old design
  47. redirectToOldDesign();
  48. }
  49. } catch (error) {
  50. // If there's any error (e.g., network, JSON parse), default to old design
  51. console.log('Using default (old) design:', error.message || 'config error');
  52. redirectToOldDesign();
  53. }
  54. function redirectToNewDesign() {
  55. // Preserve any URL parameters when redirecting
  56. const currentParams = window.location.search;
  57. window.location.href = 'index-modern.html' + currentParams;
  58. }
  59. function redirectToOldDesign() {
  60. // Preserve any URL parameters when redirecting
  61. const currentParams = window.location.search;
  62. window.location.href = 'index-classic.html' + currentParams;
  63. }
  64. })();