11import React from "react" ;
22import Head from "next/head" ;
33import type { NextComponentType } from "next" ;
4- import type { AppProps } from "next/app" ;
4+ import DefaultApp from "next/app" ;
5+ import type { AppProps , AppContext } from "next/app" ;
56import { startDsfrReact } from "./start" ;
67import type { Params as startDsfrReactParams } from "./start" ;
78import { isBrowser } from "./tools/isBrowser" ;
@@ -20,6 +21,11 @@ import appleTouchIcon from "../dsfr/favicon/apple-touch-icon.png";
2021import faviconSvg from "../dsfr/favicon/favicon.svg" ;
2122import faviconIco from "../dsfr/favicon/favicon.ico" ;
2223import faviconWebmanifestUrl from "../dsfr/favicon/manifest.webmanifest" ;
24+ import type { DocumentContext , DocumentProps } from "next/document" ;
25+ import { data_fr_scheme , data_fr_theme , $colorScheme } from "./colorScheme" ;
26+ import type { ColorScheme } from "./colorScheme" ;
27+ import { assert } from "tsafe/assert" ;
28+ import { is } from "tsafe/is" ;
2329
2430const fontUrlByFileBasename = {
2531 "Marianne-Light" : marianneLightWoff2Url ,
@@ -41,7 +47,7 @@ export type Params = startDsfrReactParams & {
4147 preloadFonts ?: ( keyof typeof fontUrlByFileBasename ) [ ] ;
4248} ;
4349
44- export function withDsfr < AppComponent extends NextComponentType < any , any , any > > (
50+ export function withAppDsfr < AppComponent extends NextComponentType < any , any , any > > (
4551 App : AppComponent ,
4652 params : Params
4753) : AppComponent {
@@ -85,7 +91,90 @@ export function withDsfr<AppComponent extends NextComponentType<any, any, any>>(
8591 staticMethod => ( ( AppWithDsfr as any ) [ staticMethod ] = ( App as any ) [ staticMethod ] )
8692 ) ;
8793
94+ AppWithDsfr . getInitialProps = async ( appContext : AppContext ) => {
95+ console . log ( "here here here" ) ;
96+
97+ if ( ! isBrowser ) {
98+ /*
99+ $colorScheme.current = (() => {
100+
101+ const cookie = appContext.ctx.req?.headers.cookie
102+
103+ return cookie === undefined ? undefined : readColorSchemeInCookie(cookie);
104+
105+ })() ?? "light";
106+ */
107+
108+ const colorScheme = ( ( ) => {
109+ const cookie = appContext . ctx . req ?. headers . cookie ;
110+
111+ return cookie === undefined ? undefined : readColorSchemeInCookie ( cookie ) ;
112+ } ) ( ) ;
113+
114+ console . log (
115+ "(server) App.getInitialProps, we read the colorScheme from cookie: " ,
116+ colorScheme
117+ ) ;
118+
119+ $colorScheme . current = colorScheme ?? "light" ;
120+ }
121+
122+ return { ...( await ( App . getInitialProps ?? DefaultApp . getInitialProps ) ( appContext ) ) } ;
123+ } ;
124+
88125 AppWithDsfr . displayName = AppWithDsfr . name ;
89126
90127 return AppWithDsfr as any ;
91128}
129+
130+ export function getDocumentDsfrInitialProps ( ctx : DocumentContext ) {
131+ const colorScheme : ColorScheme | undefined = ( ( ) => {
132+ const cookie = ctx . req ?. headers . cookie ;
133+
134+ return cookie === undefined ? undefined : readColorSchemeInCookie ( cookie ) ;
135+ } ) ( ) ;
136+
137+ return { colorScheme } ;
138+ }
139+
140+ export function getDsfrHtmlAttributes ( props : DocumentProps ) {
141+ assert ( is < ReturnType < typeof getDocumentDsfrInitialProps > > ( props ) ) ;
142+
143+ const { colorScheme } = props ;
144+
145+ if ( colorScheme === undefined ) {
146+ return { } ;
147+ }
148+
149+ $colorScheme . current = colorScheme ;
150+
151+ return {
152+ [ data_fr_scheme ] : colorScheme ,
153+ [ data_fr_theme ] : colorScheme
154+ } ;
155+ }
156+
157+ function readColorSchemeInCookie ( cookie : string ) {
158+ const parsedCookies = Object . fromEntries (
159+ cookie
160+ . split ( / ; * / )
161+ . map ( line => line . split ( "=" ) )
162+ . map ( ( [ key , value ] ) => [ key , decodeURIComponent ( value ) ] )
163+ ) ;
164+
165+ if ( ! ( data_fr_theme in parsedCookies ) ) {
166+ return undefined ;
167+ }
168+
169+ const colorScheme = parsedCookies [ data_fr_theme ] ;
170+
171+ return ( ( ) => {
172+ switch ( colorScheme ) {
173+ case "light" :
174+ case "dark" :
175+ return colorScheme ;
176+ default :
177+ return undefined ;
178+ }
179+ } ) ( ) ;
180+ }
0 commit comments