Skip to content

Commit 472bd31

Browse files
committed
Fix next dev mode when SSR color scheme enabled
1 parent ef198da commit 472bd31

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/lib/start.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,20 @@ export async function startDsfrReact(params: Params) {
3131

3232
isStarted = true;
3333

34-
//NOTE: It's non null if we are in a SSR setup
35-
if (document.documentElement.getAttribute(data_fr_theme) === null) {
36-
console.log("(client) Start: There was NO HTML tag");
34+
const isNextJsDevEnvironnement = (window as any).__NEXT_DATA__?.buildId === "development";
3735

38-
//NOTE: SPA or SSG
36+
set_html_color_scheme_attributes: {
37+
if (document.documentElement.getAttribute(data_fr_theme) !== null) {
38+
//NOTE: Is has been set by SSR
39+
break set_html_color_scheme_attributes;
40+
}
3941

4042
document.documentElement.setAttribute(data_fr_scheme, defaultColorScheme);
4143

44+
if (isNextJsDevEnvironnement) {
45+
break set_html_color_scheme_attributes;
46+
}
47+
4248
document.documentElement.setAttribute(
4349
data_fr_theme,
4450
(() => {
@@ -63,8 +69,6 @@ export async function startDsfrReact(params: Params) {
6369
: "light";
6470
})()
6571
);
66-
} else {
67-
console.log("(client) Start: Html tags are present");
6872
}
6973

7074
startObservingColorSchemeHtmlAttribute();
@@ -73,7 +77,7 @@ export async function startDsfrReact(params: Params) {
7377

7478
await import("@gouvfr/dsfr/dist/dsfr.module");
7579

76-
if ((window as any).__NEXT_DATA__?.buildId === "development") {
80+
if (isNextJsDevEnvironnement) {
7781
// NOTE: @gouvfr/dsfr/dist/dsfr.module.js is not isomorphic, it can't run on the Server.",
7882
// We set an artificial delay before starting the module otherwise to avoid getting",
7983
// Hydration error from Next.js

0 commit comments

Comments
 (0)