Skip to content

Commit 4cc4a48

Browse files
committed
Do things by the book, avoid any next error
1 parent e317d52 commit 4cc4a48

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

src/lib/start.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { startObservingColorSchemeHtmlAttribute, data_fr_theme, data_fr_scheme }
44
import { assert } from "tsafe/assert";
55
import { symToStr } from "tsafe/symToStr";
66
import { setLangToUseIfProviderNotUsed } from "./i18n";
7+
import { createStatefulObservable } from "./tools/StatefulObservable";
78

89
export type Params = {
910
defaultColorScheme: ColorScheme | "system";
@@ -38,9 +39,6 @@ export async function startDsfrReact(params: Params) {
3839

3940
const isNextJs = (window as any).__NEXT_DATA__ !== undefined;
4041

41-
const isNextJsDevEnvironnement =
42-
isNextJs && (window as any).__NEXT_DATA__.buildId === "development";
43-
4442
set_html_color_scheme_attributes: {
4543
if (document.documentElement.getAttribute(data_fr_theme) !== null) {
4644
//NOTE: Is has been set by SSR
@@ -49,7 +47,7 @@ export async function startDsfrReact(params: Params) {
4947

5048
document.documentElement.setAttribute(data_fr_scheme, defaultColorScheme);
5149

52-
if (isNextJsDevEnvironnement) {
50+
if (isNextJs) {
5351
break set_html_color_scheme_attributes;
5452
}
5553

@@ -85,7 +83,26 @@ export async function startDsfrReact(params: Params) {
8583

8684
await import("../dsfr/dsfr.module" as any);
8785

86+
const { dsfr } = window as unknown as { dsfr: { start: () => void } };
87+
8888
if (!isNextJs) {
89-
(window as any).dsfr.start();
89+
dsfr.start();
90+
}
91+
92+
if ($hadFirstEffect.current) {
93+
dsfr.start();
94+
return;
95+
}
96+
97+
$hadFirstEffect.subscribe(() => dsfr.start());
98+
}
99+
100+
const $hadFirstEffect = createStatefulObservable(() => false);
101+
102+
export function notifyEffect() {
103+
if ($hadFirstEffect.current) {
104+
return;
90105
}
106+
107+
$hadFirstEffect.current = true;
91108
}

src/next.tsx

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { NextComponentType } from "next";
44
import DefaultApp from "next/app";
55
import type { AppProps, AppContext } from "next/app";
66
import type { DocumentProps, DocumentContext } from "next/document";
7-
import { startDsfrReact } from "./lib/start";
7+
import { startDsfrReact, notifyEffect } from "./lib/start";
88
import type { Params as StartDsfrReactParams } from "./lib/start";
99
import { isBrowser } from "./lib/tools/isBrowser";
1010
import { objectKeys } from "tsafe/objectKeys";
@@ -152,20 +152,7 @@ export function createNextDsfrIntegrationApi(params: Params): NextDsfrIntegratio
152152
}
153153

154154
useEffect(() => {
155-
if (!isBrowser) {
156-
return;
157-
}
158-
const { dsfr } = window as any;
159-
160-
const isStarted = "isStarted";
161-
162-
if (dsfr[isStarted] === true) {
163-
return;
164-
}
165-
166-
dsfr.start();
167-
168-
dsfr[isStarted] = true;
155+
notifyEffect();
169156
}, []);
170157

171158
return (

0 commit comments

Comments
 (0)