Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created April 5, 2026 11:58
Show Gist options
  • Select an option

  • Save tmcw/2015bda60702697288c08b0f8a70c3ff to your computer and use it in GitHub Desktop.

Select an option

Save tmcw/2015bda60702697288c08b0f8a70c3ff to your computer and use it in GitHub Desktop.
export default async (req) => {
const headers = {
'Content-Type': 'text/css',
'Netlify-Vary': 'cookie=scheme',
'Vary': 'Cookie',
// max-age in seconds = cache for 1 hour
'Netlify-CDN-Cache-Control': 'public, max-age=3600, stale-while-revalidate=120',
'Cache-Control': 'public, max-age=360'
};
if (req.method === 'GET') {
const cookie = req.headers.get('Cookie');
if (cookie?.includes('colorscheme=light')) {
return new Response(`:root { color-scheme: light; }`, { headers });
}
if (cookie?.includes('colorscheme=dark')) {
return new Response(`:root { color-scheme: dark; }`, { headers });
}
return new Response(`:root { color-scheme: light dark; }`, { headers });
}
if (req.method === 'POST') {
const b = await req.formData();
const scheme = b.get('scheme');
const back = `<meta http-equiv="refresh" content="0; url=${req.headers.get('Referer') || 'https://macwright.com/'}">`
if (scheme === 'light' || scheme === 'dark') {
return new Response(back, {
headers: {
'Content-Type': 'text/html',
'Set-Cookie': `colorscheme=${scheme}; SameSite=Lax;`
}
})
} else {
return new Response(back, {
headers: {
'Content-Type': 'text/html',
'Set-Cookie': 'colorscheme=deleted; Expires=Thu, 01 Jan 1970 00:00:00 GMT'
}
})
}
}
return new Response('Method not supported');
}
export const config = { path: "/mode.css" };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment