@@ -26,6 +26,7 @@ import { data_fr_scheme, data_fr_theme, $colorScheme } from "./colorScheme";
2626import type { ColorScheme } from "./colorScheme" ;
2727import { assert } from "tsafe/assert" ;
2828import { is } from "tsafe/is" ;
29+ import { createStatefulObservable } from "./tools/StatefulObservable" ;
2930
3031const fontUrlByFileBasename = {
3132 "Marianne-Light" : marianneLightWoff2Url ,
@@ -47,6 +48,45 @@ export type Params = startDsfrReactParams & {
4748 preloadFonts ?: ( keyof typeof fontUrlByFileBasename ) [ ] ;
4849} ;
4950
51+ const $overwriteGetInitialProps = createStatefulObservable < ( ( ) => void ) | undefined > (
52+ ( ) => undefined
53+ ) ;
54+
55+ export function getDsfrDocumentApi ( ) {
56+ $overwriteGetInitialProps . current ?.( ) ;
57+
58+ $overwriteGetInitialProps . subscribe ( overwriteGetInitialProps => overwriteGetInitialProps ?.( ) ) ;
59+
60+ function getDocumentDsfrInitialProps ( ctx : DocumentContext ) {
61+ const colorScheme : ColorScheme | undefined = ( ( ) => {
62+ const cookie = ctx . req ?. headers . cookie ;
63+
64+ return cookie === undefined ? undefined : readColorSchemeInCookie ( cookie ) ;
65+ } ) ( ) ;
66+
67+ return { colorScheme } ;
68+ }
69+
70+ function getDsfrHtmlAttributes ( props : DocumentProps ) {
71+ assert ( is < ReturnType < typeof getDocumentDsfrInitialProps > > ( props ) ) ;
72+
73+ const { colorScheme } = props ;
74+
75+ if ( colorScheme === undefined ) {
76+ return { } ;
77+ }
78+
79+ $colorScheme . current = colorScheme ;
80+
81+ return {
82+ [ data_fr_scheme ] : colorScheme ,
83+ [ data_fr_theme ] : colorScheme
84+ } ;
85+ }
86+
87+ return { getDocumentDsfrInitialProps, getDsfrHtmlAttributes } ;
88+ }
89+
5090export function withAppDsfr < AppComponent extends NextComponentType < any , any , any > > (
5191 App : AppComponent ,
5292 params : Params
@@ -91,67 +131,46 @@ export function withAppDsfr<AppComponent extends NextComponentType<any, any, any
91131 staticMethod => ( ( AppWithDsfr as any ) [ staticMethod ] = ( App as any ) [ staticMethod ] )
92132 ) ;
93133
94- AppWithDsfr . getInitialProps = async ( appContext : AppContext ) => {
95- if ( ! isBrowser ) {
96- /*
97- $colorScheme.current = (() => {
98-
99- const cookie = appContext.ctx.req?.headers.cookie
100-
101- return cookie === undefined ? undefined : readColorSchemeInCookie(cookie);
102-
103- })() ?? "light";
104- */
105-
106- const colorScheme = ( ( ) => {
107- const cookie = appContext . ctx . req ?. headers . cookie ;
108-
109- return cookie === undefined ? undefined : readColorSchemeInCookie ( cookie ) ;
110- } ) ( ) ;
111-
112- console . log (
113- "(server) App.getInitialProps, we read the colorScheme from cookie: " ,
114- colorScheme
134+ $overwriteGetInitialProps . current = ( ) => {
135+ ( AppWithDsfr as any ) . getInitialProps = async ( appContext : AppContext ) => {
136+ const initialProps = await ( App . getInitialProps ?? DefaultApp . getInitialProps ) (
137+ appContext
115138 ) ;
116139
117- $colorScheme . current = colorScheme ?? "light" ;
118- }
119-
120- return { ...( await ( App . getInitialProps ?? DefaultApp . getInitialProps ) ( appContext ) ) } ;
140+ if ( ! isBrowser ) {
141+ /*
142+ $colorScheme.current = (() => {
143+
144+ const cookie = appContext.ctx.req?.headers.cookie
145+
146+ return cookie === undefined ? undefined : readColorSchemeInCookie(cookie);
147+
148+ })() ?? "light";
149+ */
150+
151+ const colorScheme = ( ( ) => {
152+ const cookie = appContext . ctx . req ?. headers . cookie ;
153+
154+ return cookie === undefined ? undefined : readColorSchemeInCookie ( cookie ) ;
155+ } ) ( ) ;
156+
157+ console . log (
158+ "(server) App.getInitialProps, we read the colorScheme from cookie: " ,
159+ colorScheme
160+ ) ;
161+
162+ $colorScheme . current = colorScheme ?? "light" ;
163+ }
164+
165+ return { ...initialProps } ;
166+ } ;
121167 } ;
122168
123169 AppWithDsfr . displayName = AppWithDsfr . name ;
124170
125171 return AppWithDsfr as any ;
126172}
127173
128- export function getDocumentDsfrInitialProps ( ctx : DocumentContext ) {
129- const colorScheme : ColorScheme | undefined = ( ( ) => {
130- const cookie = ctx . req ?. headers . cookie ;
131-
132- return cookie === undefined ? undefined : readColorSchemeInCookie ( cookie ) ;
133- } ) ( ) ;
134-
135- return { colorScheme } ;
136- }
137-
138- export function getDsfrHtmlAttributes ( props : DocumentProps ) {
139- assert ( is < ReturnType < typeof getDocumentDsfrInitialProps > > ( props ) ) ;
140-
141- const { colorScheme } = props ;
142-
143- if ( colorScheme === undefined ) {
144- return { } ;
145- }
146-
147- $colorScheme . current = colorScheme ;
148-
149- return {
150- [ data_fr_scheme ] : colorScheme ,
151- [ data_fr_theme ] : colorScheme
152- } ;
153- }
154-
155174function readColorSchemeInCookie ( cookie : string ) {
156175 const parsedCookies = Object . fromEntries (
157176 cookie
0 commit comments