claude hallucinated which was the expected outcome, will come back to demo 4 later

This commit is contained in:
Pagwin 2025-08-22 14:29:29 -04:00
parent a75cab7e70
commit bfc926c473

View file

@ -1,71 +1,14 @@
self.addEventListener('fetch', event => { // Cannot use Set-Cookie header for security reasons, use IndexedDB instead
const url = new URL(event.request.url);
// Only handle /set-theme endpoint self.addEventListener("fetch", event =>{
if (url.pathname.endsWith('/set-theme')) {
event.respondWith(handleThemeToggle(event.request));
}
// For all other requests, let the browser handle them normally
});
async function handleThemeToggle(request) { //TODO: make an endpoint to set the localStorage up then set the form to go to there and have it HTTP 302 or 307
// Get the referrer URL for redirect event.respondWith((async ()=>{
const referrer = request.headers.get('Referer') || '/'; const resp = await fetch(event.request);
const body = await resp.text();
// Parse existing cookies from the request return new Response(body.replace("<body>", '<body class="toggled">'), {
const cookieHeader = request.headers.get('Cookie') || ''; headers:resp.headers,
const cookies = parseCookies(cookieHeader); status: resp.status
});
// Get current theme cookie value })());
const currentTheme = cookies['Theme-Toggled']; })
// Toggle the value: if no cookie exists, set to 'yes'
// If exists, swap between 'yes' and 'no'
let newThemeValue;
if (!currentTheme) {
newThemeValue = 'yes';
} else if (currentTheme === 'yes') {
newThemeValue = 'no';
} else {
newThemeValue = 'yes';
}
// Create response with redirect and updated cookie
const response = new Response(null, {
status: 307, // Temporary Redirect - preserves request method and body
headers: {
'Location': referrer,
'Set-Cookie': `Theme-Toggled=${newThemeValue}; Path=/; HttpOnly; SameSite=Strict`
}
});
return response;
}
function parseCookies(cookieHeader) {
const cookies = {};
if (!cookieHeader) return cookies;
cookieHeader.split(';').forEach(cookie => {
const [name, ...rest] = cookie.trim().split('=');
if (name && rest.length > 0) {
cookies[name] = rest.join('=');
}
});
return cookies;
}
//self.addEventListener("fetch", event =>{
//
// //TODO: make an endpoint to set the localStorage up then set the form to go to there and have it HTTP 302 or 307
// event.respondWith((async ()=>{
// const resp = await fetch(event.request);
// const body = await resp.text();
// return new Response(body.replace("<body>", '<body class="toggled">'), {
// headers:resp.headers,
// status: resp.status
// });
// })());
//})