Skip to content

Commit 4748a0b

Browse files
committed
Add warning about withAppDsfr requirement to be the last transformation
1 parent d6c3909 commit 4748a0b

File tree

2 files changed

+37
-38
lines changed

2 files changed

+37
-38
lines changed

src/next.tsx

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export function getColorSchemeSsrUtils() {
126126
return { getColorSchemeHtmlAttributes, augmentDocumentByReadingColorSchemeFromCookie };
127127
}
128128

129+
/** the App returned by witAppDsfr should be directly exported default as is */
129130
export function withAppDsfr<AppComponent extends NextComponentType<any, any, any>>(
130131
App: AppComponent,
131132
params: Params
@@ -170,40 +171,38 @@ export function withAppDsfr<AppComponent extends NextComponentType<any, any, any
170171
);
171172
}
172173

173-
Object.keys(App).forEach(
174-
staticMethod => ((AppWithDsfr as any)[staticMethod] = (App as any)[staticMethod])
175-
);
176-
177-
$overwriteGetInitialProps.current = () => {
174+
{
178175
const super_getInitialProps =
179-
(AppWithDsfr as any).getInitialProps.bind(AppWithDsfr) ??
180-
App.getInitialProps?.bind(App) ??
181-
DefaultApp.getInitialProps.bind(DefaultApp);
182-
183-
(AppWithDsfr as any).getInitialProps = async (appContext: AppContext) => {
184-
const initialProps = await super_getInitialProps(appContext);
185-
186-
if (!isBrowser) {
187-
$colorScheme.current =
188-
(() => {
189-
const cookie = appContext.ctx.req?.headers.cookie;
190-
191-
return cookie === undefined ? undefined : readColorSchemeInCookie(cookie);
192-
})() ??
193-
(() => {
194-
switch (startDsfrReactParams.defaultColorScheme) {
195-
case "dark":
196-
case "light":
197-
return startDsfrReactParams.defaultColorScheme;
198-
case "system":
199-
return "light";
200-
}
201-
})();
202-
}
203-
204-
return { ...initialProps };
176+
App.getInitialProps?.bind(App) ?? DefaultApp.getInitialProps.bind(DefaultApp);
177+
178+
$overwriteGetInitialProps.current = () => {
179+
(AppWithDsfr as any).getInitialProps = async (appContext: AppContext) => {
180+
const initialProps = await super_getInitialProps(appContext);
181+
182+
if (!isBrowser) {
183+
$colorScheme.current =
184+
(() => {
185+
const cookie = appContext.ctx.req?.headers.cookie;
186+
187+
return cookie === undefined
188+
? undefined
189+
: readColorSchemeInCookie(cookie);
190+
})() ??
191+
(() => {
192+
switch (startDsfrReactParams.defaultColorScheme) {
193+
case "dark":
194+
case "light":
195+
return startDsfrReactParams.defaultColorScheme;
196+
case "system":
197+
return "light";
198+
}
199+
})();
200+
}
201+
202+
return { ...initialProps };
203+
};
205204
};
206-
};
205+
}
207206

208207
AppWithDsfr.displayName = AppWithDsfr.name;
209208

test/integration/next/yarn.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
regenerator-runtime "^0.13.10"
4040

4141
"@codegouvfr/react-dsfr@file:../../../dist":
42-
version "0.0.19"
42+
version "0.0.33"
4343
dependencies:
44-
"@gouvfr/dsfr" "1.7.2"
44+
"@gouvfr/dsfr" "1.8.1"
4545
remixicon "^2.5.0"
4646
tsafe "^1.1.1"
4747

@@ -60,10 +60,10 @@
6060
minimatch "^3.0.4"
6161
strip-json-comments "^3.1.1"
6262

63-
"@gouvfr/dsfr@1.7.2":
64-
version "1.7.2"
65-
resolved "https://registry.yarnpkg.com/@gouvfr/dsfr/-/dsfr-1.7.2.tgz#c522fce21a14989a10e30583160118c39c9c05d1"
66-
integrity sha512-hPNtz+gHcc8m7ZPANxSOFMz4Ap+M9FHOudqoMR/+Kjl5FCOqwA6u/aoYnMJ8KqedS1k5XThFMp7jiktr53qXYw==
63+
"@gouvfr/dsfr@1.8.1":
64+
version "1.8.1"
65+
resolved "https://registry.yarnpkg.com/@gouvfr/dsfr/-/dsfr-1.8.1.tgz#2484eb8ad9a73b5f01c5f41f49146d1aa8a71b7b"
66+
integrity sha512-XpVFdvhtalA5jSAhzzNaMd+/Hvf8Ef9jCdAZhuukEEMo2/cWvCgzz9tfbE+9QTJDIVP+EwJ7aCGWcXUSWkOHJg==
6767

6868
"@humanwhocodes/config-array@^0.5.0":
6969
version "0.5.0"

0 commit comments

Comments
 (0)