diff options
Diffstat (limited to 'src/hooks.server.js')
| -rw-r--r-- | src/hooks.server.js | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/hooks.server.js b/src/hooks.server.js new file mode 100644 index 00000000..dca44977 --- /dev/null +++ b/src/hooks.server.js @@ -0,0 +1,29 @@ + +export const handle = (async ({ event, resolve }) => { + let theme = "dark"; + + const newTheme = event.url.searchParams.get("theme"); + const cookieTheme = event.cookies.get("colortheme"); + + if (newTheme) { + theme = newTheme; + } else if (cookieTheme) { + theme = cookieTheme; + } + + if (theme) { + return await resolve(event, { + transformPageChunk: ({ html }) => { + const modifiedHtml = html.replace(/<html[^>]*>/, (match) => { + // Ensure the class attribute exists or add it if missing + return match.includes('class="') + ? match.replace(/class="[^"]*"/, `class="${theme}"`) + : match.replace('>', ` class="${theme}">`); + }); + return modifiedHtml; + } + }); + } + + return await resolve(event); +})
\ No newline at end of file |
