Stop sending theme to server

This commit is contained in:
Evert Prants 2024-06-11 16:53:31 +03:00
parent 1b090be2bf
commit 4d19e6809d
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
5 changed files with 14 additions and 23 deletions

View File

@ -21,14 +21,12 @@ if (AUTO_MIGRATE === 'true') {
await runSeeds();
const handleThemeHook = (async ({ resolve, event }) => {
// Take the theme from the query parameters or from the cookies
const newTheme = event.url.searchParams.get('themeMode') as ThemeModeType;
// Take the theme from the cookies
const cookieTheme = event.cookies.get('themeMode') as ThemeModeType;
const theme: ThemeModeType = newTheme || cookieTheme;
if (theme) {
if (cookieTheme) {
return await resolve(event, {
transformPageChunk: ({ html }) => html.replace(/theme-base=""/g, `theme-base="${theme}"`)
transformPageChunk: ({ html }) => html.replace('theme-base=""', `theme-base="${cookieTheme}"`)
});
}

View File

@ -21,8 +21,7 @@
class="page-button page-prev {firstPage ? 'disabled' : ''}"
href={`?${makePageUrl($page.url.searchParams, pageNum - 1)}`}
tabindex={firstPage ? -1 : 0}
aria-label={$t('common.previous')}
aria-disabled={firstPage}>&lt;</a
aria-label={$t('common.previous')}>&lt;</a
>
{#each pageButtons as buttonNumber}
@ -31,8 +30,7 @@
class="page-button page-link {active ? 'disabled' : ''}"
href={`?${makePageUrl($page.url.searchParams, buttonNumber)}`}
tabindex={active ? -1 : 0}
aria-label={`${$t('common.page')} ${buttonNumber}`}
aria-disabled={active}>{buttonNumber}</a
aria-label={`${$t('common.page')} ${buttonNumber}`}>{buttonNumber}</a
>
{/each}
@ -40,8 +38,7 @@
class="page-button page-prev {lastPage ? 'disabled' : ''}"
tabindex={lastPage ? -1 : 0}
href={`?${makePageUrl($page.url.searchParams, pageNum + 1)}`}
aria-label={$t('common.next')}
aria-disabled={lastPage}>&gt;</a
aria-label={$t('common.next')}>&gt;</a
>
</nav>

View File

@ -2,3 +2,4 @@ export const ALLOWED_IMAGES = ['image/png', 'image/jpg', 'image/jpeg'];
export const OAUTH2_MAX_REDIRECTS = 5;
export const OAUTH2_MAX_URLS = 1;
export const MAX_FILE_SIZE_MB = 10;
export const THEME_COOKIE_MAX_AGE = 60 * 60 * 24 * 30;

View File

@ -1,6 +1,7 @@
import { browser } from '$app/environment';
import { onMount } from 'svelte';
import { writable } from 'svelte/store';
import { THEME_COOKIE_MAX_AGE } from './constants';
export type ThemeModeType = 'light' | 'dark';
@ -14,15 +15,8 @@ export const themeMode = writable<ThemeModeType>('light', (set) => {
* Send a theme to the server so that it can be saved as a cookie.
* @param theme - theme to send to server
*/
export const forwardThemeToServer = async (theme: ThemeModeType) =>
fetch(`/?/setTheme&themeMode=${theme}`, {
method: 'POST',
body: new FormData(),
credentials: 'include',
headers: {
'x-sveltekit-action': 'true'
}
});
export const setThemeCookie = async (theme: ThemeModeType) =>
(document.cookie = `themeMode=${theme}; path=/; SameSite=Lax; Max-Age=${THEME_COOKIE_MAX_AGE}`);
export const forwardInitialTheme = () =>
onMount(() => {
@ -40,7 +34,7 @@ export const forwardInitialTheme = () =>
const unsubscribeForward = themeMode.subscribe(async (theme) => {
document.documentElement.setAttribute('theme-base', theme);
await forwardThemeToServer(theme);
await setThemeCookie(theme);
});
uaTheme?.addEventListener?.('change', uaSetter);

View File

@ -1,4 +1,5 @@
import { env } from '$env/dynamic/private';
import { THEME_COOKIE_MAX_AGE } from '$lib/constants.js';
import { redirect } from '@sveltejs/kit';
export const actions = {
@ -7,8 +8,8 @@ export const actions = {
const themeMode = url.searchParams.get('themeMode');
if (themeMode) {
cookies.set('themeMode', themeMode, {
maxAge: 60 * 60 * 24 * 365,
httpOnly: true,
maxAge: THEME_COOKIE_MAX_AGE,
httpOnly: false,
secure: env.SESSION_SECURE === 'true',
sameSite: 'lax',
path: '/'