Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Set theme in middleware
  • Loading branch information
regalijan committed Oct 19, 2023
1 parent 2920b16 commit 07e8101
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion functions/_middleware.ts
Expand Up @@ -106,4 +106,32 @@ async function setHeaders(context: RequestContext) {
return response;
}

export const onRequest = [setAuth, constructHTML, setBody, setHeaders];
async function setTheme(context: RequestContext) {
const cookies = context.request.headers.get("cookie");

if (!cookies) {
context.data.theme = "dark";
return await context.next();
}

const cookieList = cookies.split("; ");

const value = cookieList.find((c) => {
if (!c.startsWith("chakra-ui-color-mode")) return false;

return c.split("=")[1];
});

if (!value) context.data.theme = "dark";
else context.data.theme = value;

return await context.next();
}

export const onRequest = [
setAuth,
setTheme,
constructHTML,
setBody,
setHeaders,
];

0 comments on commit 07e8101

Please sign in to comment.