claude hallucinated which was the expected outcome, will come back to demo 4 later
This commit is contained in:
parent
a75cab7e70
commit
bfc926c473
1 changed files with 13 additions and 70 deletions
|
@ -1,71 +1,14 @@
|
|||
self.addEventListener('fetch', event => {
|
||||
const url = new URL(event.request.url);
|
||||
|
||||
// Only handle /set-theme endpoint
|
||||
if (url.pathname.endsWith('/set-theme')) {
|
||||
event.respondWith(handleThemeToggle(event.request));
|
||||
}
|
||||
// For all other requests, let the browser handle them normally
|
||||
});
|
||||
// Cannot use Set-Cookie header for security reasons, use IndexedDB instead
|
||||
|
||||
async function handleThemeToggle(request) {
|
||||
// Get the referrer URL for redirect
|
||||
const referrer = request.headers.get('Referer') || '/';
|
||||
|
||||
// Parse existing cookies from the request
|
||||
const cookieHeader = request.headers.get('Cookie') || '';
|
||||
const cookies = parseCookies(cookieHeader);
|
||||
|
||||
// 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
|
||||
// });
|
||||
// })());
|
||||
//})
|
||||
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
|
||||
});
|
||||
})());
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue