@@ -36,10 +36,34 @@ const renderWithNext = FEATURE_NEXTJS
3636 ]
3737 : [ ]
3838
39+ function modifyOutput ( req , text ) {
40+ return addColorMode ( req , addCsrf ( req , text ) )
41+ }
42+
3943function addCsrf ( req , text ) {
4044 return text . replace ( '$CSRFTOKEN$' , req . csrfToken ( ) )
4145}
4246
47+ function addColorMode ( req , text ) {
48+ let colorMode = 'auto'
49+ let darkTheme = 'dark'
50+ let lightTheme = 'light'
51+
52+ try {
53+ const cookieValue = JSON . parse ( decodeURIComponent ( req . cookies . color_mode ) )
54+ colorMode = encodeURIComponent ( cookieValue . color_mode ) || colorMode
55+ darkTheme = encodeURIComponent ( cookieValue . dark_theme . name ) || darkTheme
56+ lightTheme = encodeURIComponent ( cookieValue . light_theme . name ) || lightTheme
57+ } catch ( e ) {
58+ // do nothing
59+ }
60+
61+ return text
62+ . replace ( '$COLORMODE$' , colorMode )
63+ . replace ( '$DARKTHEME$' , darkTheme )
64+ . replace ( '$LIGHTTHEME$' , lightTheme )
65+ }
66+
4367module . exports = async function renderPage ( req , res , next ) {
4468 const page = req . context . page
4569
@@ -49,7 +73,7 @@ module.exports = async function renderPage (req, res, next) {
4973 console . error ( `\nTried to redirect to ${ req . context . redirectNotFound } , but that page was not found.\n` )
5074 }
5175 return res . status ( 404 ) . send (
52- addCsrf (
76+ modifyOutput (
5377 req ,
5478 await liquid . parseAndRender ( layouts [ 'error-404' ] , req . context )
5579 )
@@ -90,7 +114,7 @@ module.exports = async function renderPage (req, res, next) {
90114
91115 console . log ( `Serving from cached version of ${ originalUrl } ` )
92116 statsd . increment ( 'page.sent_from_cache' )
93- return res . send ( addCsrf ( req , cachedHtml ) )
117+ return res . send ( modifyOutput ( req , cachedHtml ) )
94118 }
95119 }
96120
@@ -158,7 +182,7 @@ module.exports = async function renderPage (req, res, next) {
158182
159183 // First, send the response so the user isn't waiting
160184 // NOTE: Do NOT `return` here as we still need to cache the response afterward!
161- res . send ( addCsrf ( req , output ) )
185+ res . send ( modifyOutput ( req , output ) )
162186
163187 // Finally, save output to cache for the next time around
164188 if ( isCacheable ) {
0 commit comments