@@ -26,6 +26,7 @@ import { data_fr_scheme, data_fr_theme, $colorScheme } from "./lib/darkMode";
2626import type { ColorScheme } from "./lib/darkMode" ;
2727import { createStatefulObservable } from "./lib/tools/StatefulObservable" ;
2828import { symToStr } from "tsafe/symToStr" ;
29+ import DefaultDocument from "next/document" ;
2930
3031const fontUrlByFileBasename = {
3132 "Marianne-Light" : marianneLightWoff2Url ,
@@ -62,37 +63,6 @@ export function getColorSchemeSsrUtils() {
6263
6364 let isNextTickCleared = false ;
6465
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-
9666 function getColorSchemeHtmlAttributes ( ) {
9767 isNextTickCleared = true ;
9868
@@ -108,7 +78,52 @@ export function getColorSchemeSsrUtils() {
10878 } ;
10979 }
11080
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 } ;
112127}
113128
114129export function withAppDsfr < AppComponent extends NextComponentType < any , any , any > > (
0 commit comments