Skip to content

Commit 0f83eb5

Browse files
committed
Breaking change next.js integration: Increasse level of abstraction
1 parent b48e2c3 commit 0f83eb5

File tree

2 files changed

+53
-42
lines changed

2 files changed

+53
-42
lines changed

src/next.tsx

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { data_fr_scheme, data_fr_theme, $colorScheme } from "./lib/darkMode";
2626
import type { ColorScheme } from "./lib/darkMode";
2727
import { createStatefulObservable } from "./lib/tools/StatefulObservable";
2828
import { symToStr } from "tsafe/symToStr";
29+
import DefaultDocument from "next/document";
2930

3031
const 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

114129
export function withAppDsfr<AppComponent extends NextComponentType<any, any, any>>(
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import DefaultDocument, { Html, Head, Main, NextScript } from "next/document";
2-
import type { DocumentContext } from "next/document";
1+
import { Html, Head, Main, NextScript } from "next/document";
32
import { getColorSchemeSsrUtils } from "@codegouvfr/react-dsfr/next";
43

5-
const { readColorSchemeFromCookie, getColorSchemeHtmlAttributes } = getColorSchemeSsrUtils();
4+
const {
5+
getColorSchemeHtmlAttributes,
6+
augmentDocumentByReadingColorSchemeFromCookie
7+
} = getColorSchemeSsrUtils();
68

79
export default function Document() {
810
return (
@@ -16,10 +18,4 @@ export default function Document() {
1618
);
1719
}
1820

19-
Document.getInitialProps = async (ctx: DocumentContext) => {
20-
const initialProps = await DefaultDocument.getInitialProps(ctx);
21-
22-
readColorSchemeFromCookie(ctx);
23-
24-
return { ...initialProps };
25-
};
21+
augmentDocumentByReadingColorSchemeFromCookie(Document);

0 commit comments

Comments
 (0)