@@ -21,12 +21,11 @@ import appleTouchIcon from "../dsfr/favicon/apple-touch-icon.png";
2121import faviconSvg from "../dsfr/favicon/favicon.svg" ;
2222import faviconIco from "../dsfr/favicon/favicon.ico" ;
2323import faviconWebmanifestUrl from "../dsfr/favicon/manifest.webmanifest" ;
24- import type { DocumentContext , DocumentProps } from "next/document" ;
24+ import type { DocumentContext } from "next/document" ;
2525import { data_fr_scheme , data_fr_theme , $colorScheme } from "./colorScheme" ;
2626import type { ColorScheme } from "./colorScheme" ;
27- import { assert } from "tsafe/assert" ;
28- import { is } from "tsafe/is" ;
2927import { createStatefulObservable } from "./tools/StatefulObservable" ;
28+ import { symToStr } from "tsafe/symToStr" ;
3029
3130const fontUrlByFileBasename = {
3231 "Marianne-Light" : marianneLightWoff2Url ,
@@ -52,25 +51,38 @@ const $overwriteGetInitialProps = createStatefulObservable<(() => void) | undefi
5251 ( ) => undefined
5352) ;
5453
55- export function getDsfrDocumentApi ( ) {
54+ export function getColorSchemeSsrUtils ( ) {
5655 $overwriteGetInitialProps . current ?.( ) ;
5756
5857 $overwriteGetInitialProps . subscribe ( overwriteGetInitialProps => overwriteGetInitialProps ?.( ) ) ;
5958
60- function getDocumentDsfrInitialProps ( ctx : DocumentContext ) {
61- const colorScheme : ColorScheme | undefined = ( ( ) => {
62- const cookie = ctx . req ?. headers . cookie ;
59+ let colorScheme : ColorScheme | undefined = undefined ;
6360
64- return cookie === undefined ? undefined : readColorSchemeInCookie ( cookie ) ;
65- } ) ( ) ;
61+ let isNextTickCleared = false ;
6662
67- return { colorScheme } ;
68- }
63+ function readColorSchemeFromCookie ( ctx : DocumentContext ) {
64+ const cookie = ctx . req ?. headers . cookie ;
65+
66+ colorScheme = cookie === undefined ? undefined : readColorSchemeInCookie ( cookie ) ;
67+
68+ isNextTickCleared = false ;
6969
70- function getDsfrHtmlAttributes ( props : DocumentProps ) {
71- assert ( is < ReturnType < typeof getDocumentDsfrInitialProps > > ( props ) ) ;
70+ process . nextTick ( ( ) => {
71+ if ( ! isNextTickCleared ) {
72+ console . error (
73+ [
74+ `WARNING: ${ symToStr ( {
75+ getColorSchemeHtmlAttributes
76+ } ) } should be called just after`,
77+ `${ symToStr ( { getColorSchemeSsrUtils } ) } , in the same event loop tick!`
78+ ] . join ( " " )
79+ ) ;
80+ }
81+ } ) ;
82+ }
7283
73- const { colorScheme } = props ;
84+ function getColorSchemeHtmlAttributes ( ) {
85+ isNextTickCleared = true ;
7486
7587 if ( colorScheme === undefined ) {
7688 return { } ;
@@ -84,7 +96,7 @@ export function getDsfrDocumentApi() {
8496 } ;
8597 }
8698
87- return { getDocumentDsfrInitialProps , getDsfrHtmlAttributes } ;
99+ return { readColorSchemeFromCookie , getColorSchemeHtmlAttributes } ;
88100}
89101
90102export function withAppDsfr < AppComponent extends NextComponentType < any , any , any > > (
0 commit comments