Skip to content

Commit 550fc87

Browse files
committed
Reduce bundle size without introducing API changes
1 parent 74d160c commit 550fc87

File tree

6 files changed

+28
-24
lines changed

6 files changed

+28
-24
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
"remixicon": "^2.5.0",
109109
"storybook-dark-mode": "^1.1.2",
110110
"ts-node": "^10.9.1",
111-
"tss-react": "^4.7.0",
111+
"tss-react": "^4.7.1",
112112
"type-route": "^1.0.1",
113113
"typescript": "^4.9.1-beta",
114114
"vitest": "^0.24.3"

src/next-pagesdir.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ import { fontUrlByFileBasename } from "./next-appdir/fontUrlByFileBasename";
1414
import AppleTouchIcon from "./dsfr/favicon/apple-touch-icon.png";
1515
import FaviconSvg from "./dsfr/favicon/favicon.svg";
1616
import FaviconIco from "./dsfr/favicon/favicon.ico";
17-
import DefaultDocument from "next/document";
1817
import { getAssetUrl } from "./tools/getAssetUrl";
1918
import { getColors } from "./fr/colors";
2019
import { start } from "./start";
2120
import type { RegisterLink, RegisteredLinkProps } from "./link";
2221
import { setLink } from "./link";
2322
import { setUseLang } from "./i18n";
2423
import Script from "next/script";
24+
import { assert } from "tsafe/assert";
2525
import "./dsfr/dsfr.css";
2626
import "./dsfr/utility/icons/icons.css";
2727

@@ -233,9 +233,14 @@ export function createNextDsfrIntegrationApi(
233233
}
234234

235235
function augmentDocumentForDsfr(Document: NextComponentType<any, any, any>): void {
236-
const super_getInitialProps =
237-
Document.getInitialProps?.bind(Document) ??
238-
DefaultDocument.getInitialProps.bind(DefaultDocument);
236+
let super_getInitialProps = Document.getInitialProps?.bind(Document);
237+
238+
if (super_getInitialProps === undefined) {
239+
import("next/document").then(
240+
({ default: DefaultDocument }) =>
241+
(super_getInitialProps = DefaultDocument.getInitialProps.bind(DefaultDocument))
242+
);
243+
}
239244

240245
(Document as any).getInitialProps = async (documentContext: DocumentContext) => {
241246
const { isDark } = (() => {
@@ -275,6 +280,11 @@ export function createNextDsfrIntegrationApi(
275280
});
276281
}
277282

283+
assert(
284+
super_getInitialProps !== undefined,
285+
"Default document not yet loaded. Please submit an issue to the tss-react repo"
286+
);
287+
278288
const initialProps = await super_getInitialProps(documentContext);
279289

280290
return { ...initialProps, [isDarkPropKey]: isDark };

test/integration/next-pagesdir/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"next-transpile-modules": "^10.0.0",
1919
"@emotion/react": "^11.10.5",
2020
"@emotion/server": "^11.10.0",
21-
"tss-react": "^4.7.0",
21+
"tss-react": "^4.7.1",
2222
"@mui/material": "^5.11.0",
2323
"@mui/icons-material": "^5.11.0",
2424
"@emotion/styled": "^11.10.5",

test/integration/next-pagesdir/pages/_document.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import DefaultDocument, { Html, Head, Main, NextScript } from "next/document";
1+
import { Html, Head, Main, NextScript } from "next/document";
22
import type { DocumentProps } from "next/document";
33
import { dsfrDocumentApi, augmentDocumentWithEmotionCache } from "./_app";
44

@@ -16,12 +16,6 @@ export default function Document(props: DocumentProps) {
1616
);
1717
}
1818

19-
augmentDocumentForDsfr({
20-
DefaultDocument,
21-
Document
22-
});
19+
augmentDocumentForDsfr(Document);
2320

24-
augmentDocumentWithEmotionCache({
25-
DefaultDocument,
26-
Document
27-
});
21+
augmentDocumentWithEmotionCache(Document);

test/integration/next-pagesdir/yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
to-fast-properties "^2.0.0"
8080

8181
"@codegouvfr/react-dsfr@file:../../../dist":
82-
version "0.35.1"
82+
version "0.36.0"
8383
dependencies:
8484
tsafe "^1.4.0"
8585

@@ -2406,10 +2406,10 @@ tslib@^2.4.0:
24062406
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.4.1.tgz#0d0bfbaac2880b91e22df0768e55be9753a5b17e"
24072407
integrity sha512-tGyy4dAjRIEwI7BzsB0lynWgOpfqjUdq91XXAlIWD2OwKBH7oCl/GZG/HT4BOHrTlPMOASlMQ7veyTqpmRcrNA==
24082408

2409-
tss-react@^4.7.0:
2410-
version "4.7.0"
2411-
resolved "https://registry.yarnpkg.com/tss-react/-/tss-react-4.7.0.tgz#fdc7925c4b8b2842d37f8e54ae20aa5980fd6e44"
2412-
integrity sha512-8U1Jgthn7FFvVgpyaLu4rIZYxcmdlohX3oGGxHpgQbRxt8gR1B+LoKVhFi+fL6fvM0daIB7ZMrwEoPuvzUmg5g==
2409+
tss-react@^4.7.1:
2410+
version "4.7.1"
2411+
resolved "https://registry.yarnpkg.com/tss-react/-/tss-react-4.7.1.tgz#52f973924e6e4468fb67b4ed73881d8fb139d5d0"
2412+
integrity sha512-xlKTTkN232rK16nBPlyvfesVBDbyk17oo+clEB3Wm77VyrBChAakYJ8NcqmzXHzW6QbvyqiSS6AR2Zzd/Ul1ew==
24132413
dependencies:
24142414
"@emotion/cache" "*"
24152415
"@emotion/serialize" "*"

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10983,10 +10983,10 @@ tslib@^2.0.0, tslib@^2.0.1, tslib@^2.0.3, tslib@^2.1.0, tslib@^2.4.0:
1098310983
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.4.1.tgz#0d0bfbaac2880b91e22df0768e55be9753a5b17e"
1098410984
integrity sha512-tGyy4dAjRIEwI7BzsB0lynWgOpfqjUdq91XXAlIWD2OwKBH7oCl/GZG/HT4BOHrTlPMOASlMQ7veyTqpmRcrNA==
1098510985

10986-
tss-react@^4.7.0:
10987-
version "4.7.0"
10988-
resolved "https://registry.yarnpkg.com/tss-react/-/tss-react-4.7.0.tgz#fdc7925c4b8b2842d37f8e54ae20aa5980fd6e44"
10989-
integrity sha512-8U1Jgthn7FFvVgpyaLu4rIZYxcmdlohX3oGGxHpgQbRxt8gR1B+LoKVhFi+fL6fvM0daIB7ZMrwEoPuvzUmg5g==
10986+
tss-react@^4.7.1:
10987+
version "4.7.1"
10988+
resolved "https://registry.yarnpkg.com/tss-react/-/tss-react-4.7.1.tgz#52f973924e6e4468fb67b4ed73881d8fb139d5d0"
10989+
integrity sha512-xlKTTkN232rK16nBPlyvfesVBDbyk17oo+clEB3Wm77VyrBChAakYJ8NcqmzXHzW6QbvyqiSS6AR2Zzd/Ul1ew==
1099010990
dependencies:
1099110991
"@emotion/cache" "*"
1099210992
"@emotion/serialize" "*"

0 commit comments

Comments
 (0)