@@ -36,10 +36,34 @@ const renderWithNext = FEATURE_NEXTJS
36
36
]
37
37
: [ ]
38
38
39
+ function modifyOutput ( req , text ) {
40
+ return addColorMode ( req , addCsrf ( req , text ) )
41
+ }
42
+
39
43
function addCsrf ( req , text ) {
40
44
return text . replace ( '$CSRFTOKEN$' , req . csrfToken ( ) )
41
45
}
42
46
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
+
43
67
module . exports = async function renderPage ( req , res , next ) {
44
68
const page = req . context . page
45
69
@@ -49,7 +73,7 @@ module.exports = async function renderPage (req, res, next) {
49
73
console . error ( `\nTried to redirect to ${ req . context . redirectNotFound } , but that page was not found.\n` )
50
74
}
51
75
return res . status ( 404 ) . send (
52
- addCsrf (
76
+ modifyOutput (
53
77
req ,
54
78
await liquid . parseAndRender ( layouts [ 'error-404' ] , req . context )
55
79
)
@@ -90,7 +114,7 @@ module.exports = async function renderPage (req, res, next) {
90
114
91
115
console . log ( `Serving from cached version of ${ originalUrl } ` )
92
116
statsd . increment ( 'page.sent_from_cache' )
93
- return res . send ( addCsrf ( req , cachedHtml ) )
117
+ return res . send ( modifyOutput ( req , cachedHtml ) )
94
118
}
95
119
}
96
120
@@ -158,7 +182,7 @@ module.exports = async function renderPage (req, res, next) {
158
182
159
183
// First, send the response so the user isn't waiting
160
184
// 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 ) )
162
186
163
187
// Finally, save output to cache for the next time around
164
188
if ( isCacheable ) {
0 commit comments