From 6fa9efcc74a9c36f9e2178ee5427eafb8d470d99 Mon Sep 17 00:00:00 2001 From: Frankie B Date: Mon, 22 Jan 2024 01:16:42 +0000 Subject: [PATCH] Get scheme cookie server-side instead of client-side on page load --- app/Http/Middleware/EncryptCookies.php | 2 +- public/js/{themeSwap.js => schemeSwap.js} | 23 ++--------------------- resources/views/includes/head.blade.php | 7 +++++-- resources/views/layouts/default.blade.php | 2 +- 4 files changed, 9 insertions(+), 25 deletions(-) rename public/js/{themeSwap.js => schemeSwap.js} (67%) diff --git a/app/Http/Middleware/EncryptCookies.php b/app/Http/Middleware/EncryptCookies.php index 867695b..5ee1433 100644 --- a/app/Http/Middleware/EncryptCookies.php +++ b/app/Http/Middleware/EncryptCookies.php @@ -12,6 +12,6 @@ class EncryptCookies extends Middleware * @var array */ protected $except = [ - // + "colorscheme" ]; } diff --git a/public/js/themeSwap.js b/public/js/schemeSwap.js similarity index 67% rename from public/js/themeSwap.js rename to public/js/schemeSwap.js index 18faaee..de5a6b2 100644 --- a/public/js/themeSwap.js +++ b/public/js/schemeSwap.js @@ -1,5 +1,5 @@ /** - * Retrieves a cookie's value + * Retrieves a cookies value * @param {string} cname Cookie name * @returns {string} Cookie value */ @@ -44,29 +44,10 @@ function cookieExists(cname) { /** * Swaps the colorscheme - * @param option + * @param {string} scheme Color scheme ID */ function swapScheme(scheme) { setCookie("colorscheme", scheme, 90); document.getElementById("css-colorscheme").href = `/css/colorschemes/${scheme}.css`; console.log(`Set colorscheme to ${getCookie("colorscheme")}`) } - -function setDefaultScheme() { - if (!cookieExists("colorscheme")) { - setCookie("colorscheme", "catppuccin-macchiato", 90); - console.debug("Set default colorscheme"); - } else { - const scheme = getCookie("colorscheme"); - const schemeselector = document.getElementById("scheme-selector"); - if (scheme && schemeselector) { - for (let option of schemeselector.options) { - if (option.value == scheme) { - option.selected = true; - break; - } - } - } - swapScheme(scheme); - } -} diff --git a/resources/views/includes/head.blade.php b/resources/views/includes/head.blade.php index 3d51672..d6d1280 100644 --- a/resources/views/includes/head.blade.php +++ b/resources/views/includes/head.blade.php @@ -1,13 +1,16 @@ +@php // Get colorscheme from cookie and apply immediately + $colorscheme = request()->cookie('colorscheme', 'catppuccin-macchiato'); +@endphp - + - + {!! (intval(date('n')) == 12) ? '' : '' !!} diff --git a/resources/views/layouts/default.blade.php b/resources/views/layouts/default.blade.php index 144c32b..996d895 100644 --- a/resources/views/layouts/default.blade.php +++ b/resources/views/layouts/default.blade.php @@ -4,7 +4,7 @@ @include('includes.head') - +