From 7a882942e62cb0f8dcda0eeff3da6d7b14bba406 Mon Sep 17 00:00:00 2001 From: pancakes Date: Fri, 13 Dec 2024 01:14:17 +1000 Subject: [PATCH] Use localStorage for green --- src/routes/+layout.server.js | 7 ------- src/routes/+layout.svelte | 7 +++++-- src/routes/green/+page.js | 16 ++++++++++++++++ src/routes/green/+page.server.js | 13 ------------- src/routes/green/+page.svelte | 1 + 5 files changed, 22 insertions(+), 22 deletions(-) delete mode 100644 src/routes/+layout.server.js create mode 100644 src/routes/green/+page.js delete mode 100644 src/routes/green/+page.server.js create mode 100644 src/routes/green/+page.svelte diff --git a/src/routes/+layout.server.js b/src/routes/+layout.server.js deleted file mode 100644 index 94490b7..0000000 --- a/src/routes/+layout.server.js +++ /dev/null @@ -1,7 +0,0 @@ -export async function load({ cookies }) { - const green = cookies.get("green"); - - return { - green: green === "true" - }; -} \ No newline at end of file diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 889ba69..1de353a 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -1,14 +1,17 @@ - {#if data.green} + {#if green} {/if} diff --git a/src/routes/green/+page.js b/src/routes/green/+page.js new file mode 100644 index 0000000..515adec --- /dev/null +++ b/src/routes/green/+page.js @@ -0,0 +1,16 @@ +import { browser } from '$app/environment'; +import { redirect } from '@sveltejs/kit'; + +export async function load() { + if (!browser) return; + + const green = window.localStorage.getItem("green"); + + if (green === "true") { + window.localStorage.removeItem("green"); + } else { + window.localStorage.setItem("green", "true"); + } + + redirect(302, "/"); +} diff --git a/src/routes/green/+page.server.js b/src/routes/green/+page.server.js deleted file mode 100644 index 422ecfe..0000000 --- a/src/routes/green/+page.server.js +++ /dev/null @@ -1,13 +0,0 @@ -import { redirect } from '@sveltejs/kit'; - -export async function load({ cookies }) { - const green = cookies.get("green"); - - if (green === "true") { - cookies.delete("green", { path: "/" }); - } else { - cookies.set("green", "true", { path: "/", sameSite: "strict" }); - } - - redirect(302, "/"); -} diff --git a/src/routes/green/+page.svelte b/src/routes/green/+page.svelte new file mode 100644 index 0000000..c9b1c57 --- /dev/null +++ b/src/routes/green/+page.svelte @@ -0,0 +1 @@ +<p>Toggling green...</p>