@@ -26,6 +26,7 @@ import { data_fr_scheme, data_fr_theme, $colorScheme } from "./lib/darkMode";
26
26
import type { ColorScheme } from "./lib/darkMode" ;
27
27
import { createStatefulObservable } from "./lib/tools/StatefulObservable" ;
28
28
import { symToStr } from "tsafe/symToStr" ;
29
+ import DefaultDocument from "next/document" ;
29
30
30
31
const fontUrlByFileBasename = {
31
32
"Marianne-Light" : marianneLightWoff2Url ,
@@ -62,37 +63,6 @@ export function getColorSchemeSsrUtils() {
62
63
63
64
let isNextTickCleared = false ;
64
65
65
- function readColorSchemeFromCookie ( ctx : DocumentContext ) {
66
- const cookie = ctx . req ?. headers . cookie ;
67
-
68
- colorScheme =
69
- ( cookie === undefined ? undefined : readColorSchemeInCookie ( cookie ) ) ??
70
- ( ( ) => {
71
- switch ( defaultColorScheme ) {
72
- case "light" :
73
- case "dark" :
74
- return defaultColorScheme ;
75
- case "system" :
76
- return undefined ;
77
- }
78
- } ) ( ) ;
79
-
80
- isNextTickCleared = false ;
81
-
82
- process . nextTick ( ( ) => {
83
- if ( ! isNextTickCleared ) {
84
- console . error (
85
- [
86
- `WARNING: ${ symToStr ( {
87
- getColorSchemeHtmlAttributes
88
- } ) } should be called just after`,
89
- `${ symToStr ( { getColorSchemeSsrUtils } ) } , in the same event loop tick!`
90
- ] . join ( " " )
91
- ) ;
92
- }
93
- } ) ;
94
- }
95
-
96
66
function getColorSchemeHtmlAttributes ( ) {
97
67
isNextTickCleared = true ;
98
68
@@ -108,7 +78,52 @@ export function getColorSchemeSsrUtils() {
108
78
} ;
109
79
}
110
80
111
- return { readColorSchemeFromCookie, getColorSchemeHtmlAttributes } ;
81
+ function augmentDocumentByReadingColorSchemeFromCookie (
82
+ Document : NextComponentType < any , any , any >
83
+ ) : void {
84
+ const super_getInitialProps =
85
+ Document . getInitialProps ?. bind ( Document ) ??
86
+ DefaultDocument . getInitialProps . bind ( DefaultDocument ) ;
87
+
88
+ ( Document as any ) . getInitialProps = async ( documentContext : DocumentContext ) => {
89
+ const initialProps = await super_getInitialProps ( documentContext ) ;
90
+
91
+ {
92
+ const cookie = documentContext . req ?. headers . cookie ;
93
+
94
+ colorScheme =
95
+ ( cookie === undefined ? undefined : readColorSchemeInCookie ( cookie ) ) ??
96
+ ( ( ) => {
97
+ switch ( defaultColorScheme ) {
98
+ case "light" :
99
+ case "dark" :
100
+ return defaultColorScheme ;
101
+ case "system" :
102
+ return undefined ;
103
+ }
104
+ } ) ( ) ;
105
+
106
+ isNextTickCleared = false ;
107
+
108
+ process . nextTick ( ( ) => {
109
+ if ( ! isNextTickCleared ) {
110
+ console . error (
111
+ [
112
+ `WARNING: react-dsfr, Next.js setup: ${ symToStr ( {
113
+ getColorSchemeHtmlAttributes
114
+ } ) } should be called just after this.`,
115
+ `If you see this error please open an issue https://github.com/codegouvfr/react-dsfr/issues`
116
+ ] . join ( "\n" )
117
+ ) ;
118
+ }
119
+ } ) ;
120
+ }
121
+
122
+ return { ...initialProps } ;
123
+ } ;
124
+ }
125
+
126
+ return { getColorSchemeHtmlAttributes, augmentDocumentByReadingColorSchemeFromCookie } ;
112
127
}
113
128
114
129
export function withAppDsfr < AppComponent extends NextComponentType < any , any , any > > (
0 commit comments