Skip to content

Commit 8ea1738

Browse files
authored
[docs-infra] Migrate to next/font (#47347)
1 parent f6e2d52 commit 8ea1738

File tree

5 files changed

+50
-82
lines changed

5 files changed

+50
-82
lines changed

docs/babel.config.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

docs/pages/_app.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { CodeVariantProvider } from 'docs/src/modules/utils/codeVariant';
2222
import DocsStyledEngineProvider from 'docs/src/modules/utils/StyledEngineProvider';
2323
import createEmotionCache from 'docs/src/createEmotionCache';
2424
import findActivePage from 'docs/src/modules/utils/findActivePage';
25-
import { pathnameToLanguage } from 'docs/src/modules/utils/helpers';
2625
import getProductInfoFromUrl from 'docs/src/modules/utils/getProductInfoFromUrl';
2726
import { DocsProvider } from '@mui/docs/DocsProvider';
2827
import { mapTranslations } from '@mui/docs/i18n';
@@ -32,8 +31,22 @@ import SvgMuiLogomark, {
3231
} from 'docs/src/icons/SvgMuiLogomark';
3332
import './global.css';
3433
import '../public/static/components-gallery/base-theme.css';
34+
import { Inter, Roboto } from 'next/font/google';
3535
import * as config from '../config';
3636

37+
const inter = Inter({
38+
weight: ['300', '400', '500', '600', '700'],
39+
subsets: ['latin'],
40+
});
41+
42+
const roboto = Roboto({
43+
weight: ['300', '400', '500', '700'],
44+
style: ['normal', 'italic'],
45+
subsets: ['latin'],
46+
});
47+
48+
export const fontClasses = `${inter.className} ${roboto.className}`;
49+
3750
// Remove the license warning from demonstration purposes
3851
LicenseInfo.setLicenseKey(process.env.NEXT_PUBLIC_MUI_LICENSE);
3952

@@ -298,20 +311,10 @@ function AppWrapper(props) {
298311
};
299312
}, [productId, productCategoryId, productIdentifier, router.pathname]);
300313

301-
let fonts = [];
302-
if (pathnameToLanguage(router.asPath).canonicalAs.match(/onepirate/)) {
303-
fonts = [
304-
'https://fonts.googleapis.com/css2?family=Roboto+Condensed:wght@700&family=Work+Sans:wght@300;400&display=swap',
305-
];
306-
}
307-
308314
return (
309315
<React.Fragment>
310316
<NextHead>
311317
<meta name="viewport" content="initial-scale=1, width=device-width" />
312-
{fonts.map((font) => (
313-
<link rel="stylesheet" href={font} key={font} />
314-
))}
315318
<meta name="mui:productId" content={productId} />
316319
<meta name="mui:productCategoryId" content={productCategoryId} />
317320
</NextHead>

docs/pages/_document.js

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import JoyInitColorSchemeScript from '@mui/joy/InitColorSchemeScript';
99
import { pathnameToLanguage } from 'docs/src/modules/utils/helpers';
1010
import createEmotionCache from 'docs/src/createEmotionCache';
1111
import { getMetaThemeColor } from '@mui/docs/branding';
12+
import { fontClasses } from './_app';
1213

1314
const PRODUCTION_GA =
1415
process.env.DEPLOY_ENV === 'production' || process.env.DEPLOY_ENV === 'staging';
@@ -49,48 +50,6 @@ export default class MyDocument extends Document {
4950
}${canonicalAsServer}`}
5051
/>
5152
<link rel="alternate" href={`https://mui.com${canonicalAsServer}`} hrefLang="x-default" />
52-
{/*
53-
Preconnect allows the browser to setup early connections before an HTTP request
54-
is actually sent to the server.
55-
This includes DNS lookups, TLS negotiations, TCP handshakes.
56-
*/}
57-
<link href="https://fonts.gstatic.com" rel="preconnect" crossOrigin="anonymous" />
58-
<link rel="preconnect" href="https://fonts.googleapis.com" />
59-
<link
60-
href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Roboto:ital,wght@0,300;0,400;0,500;0,700;1,400&display=swap"
61-
rel="stylesheet"
62-
/>
63-
{/* ========== Font preload (prevent font flash) ============= */}
64-
<link
65-
rel="preload"
66-
// optimized for english characters (40kb -> 6kb)
67-
href="/static/fonts/GeneralSans-Semibold-subset.woff2"
68-
as="font"
69-
type="font/woff2"
70-
crossOrigin="anonymous"
71-
/>
72-
<style
73-
// the above <link> does not work in mobile device, this inline <style> fixes it without blocking resources
74-
// eslint-disable-next-line react/no-danger
75-
dangerouslySetInnerHTML={{
76-
__html: `@font-face{font-family:'General Sans';font-style:normal;font-weight:600;font-display:swap;src:url('/static/fonts/GeneralSans-Semibold-subset.woff2') format('woff2');}`,
77-
}}
78-
/>
79-
<link
80-
rel="preload"
81-
// optimized for english characters (40kb -> 6kb)
82-
href="/static/fonts/IBMPlexSans-Regular-subset.woff2"
83-
as="font"
84-
type="font/woff2"
85-
crossOrigin="anonymous"
86-
/>
87-
<style
88-
// the above <link> does not work in mobile device, this inline <style> fixes it without blocking resources
89-
// eslint-disable-next-line react/no-danger
90-
dangerouslySetInnerHTML={{
91-
__html: `@font-face{font-family:'IBM Plex Sans';font-style:normal;font-weight:400;font-display:swap;src:url('/static/fonts/IBMPlexSans-Regular-subset.woff2') format('woff2');}`,
92-
}}
93-
/>
9453
{/* =========================================================== */}
9554
<style
9655
// Loads General Sans: Regular (400), Medium (500), SemiBold (600), Bold (700)
@@ -166,7 +125,7 @@ export default class MyDocument extends Document {
166125
}}
167126
/>
168127
</Head>
169-
<body>
128+
<body className={fontClasses}>
170129
<MuiInitColorSchemeScript defaultMode="system" />
171130
<JoyInitColorSchemeScript defaultMode="system" />
172131
<Main />

docs/src/pages/premium-themes/onepirate/modules/withRoot.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
import * as React from 'react';
22
import { ThemeProvider } from '@mui/material/styles';
33
import CssBaseline from '@mui/material/CssBaseline';
4+
import { Roboto_Condensed, Work_Sans } from 'next/font/google';
45
import theme from './theme';
56

7+
const robotoCondensed = Roboto_Condensed({
8+
weight: ['700'],
9+
subsets: ['latin'],
10+
});
11+
const workSans = Work_Sans({
12+
weight: ['300', '400'],
13+
subsets: ['latin'],
14+
});
15+
616
export default function withRoot(Component) {
717
function WithRoot(props) {
818
return (
9-
<ThemeProvider theme={theme}>
10-
{/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
11-
<CssBaseline />
12-
<Component {...props} />
13-
</ThemeProvider>
19+
<div className={`${robotoCondensed.className} ${workSans.className}`}>
20+
<ThemeProvider theme={theme}>
21+
{/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
22+
<CssBaseline />
23+
<Component {...props} />
24+
</ThemeProvider>
25+
</div>
1426
);
1527
}
1628

docs/src/pages/premium-themes/onepirate/modules/withRoot.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
import * as React from 'react';
22
import { ThemeProvider } from '@mui/material/styles';
33
import CssBaseline from '@mui/material/CssBaseline';
4+
import { Roboto_Condensed, Work_Sans } from 'next/font/google';
45
import theme from './theme';
56

7+
const robotoCondensed = Roboto_Condensed({
8+
weight: ['700'],
9+
subsets: ['latin'],
10+
});
11+
const workSans = Work_Sans({
12+
weight: ['300', '400'],
13+
subsets: ['latin'],
14+
});
15+
616
export default function withRoot<P extends React.JSX.IntrinsicAttributes>(
717
Component: React.ComponentType<P>,
818
) {
919
function WithRoot(props: P) {
1020
return (
11-
<ThemeProvider theme={theme}>
12-
{/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
13-
<CssBaseline />
14-
<Component {...props} />
15-
</ThemeProvider>
21+
<div className={`${robotoCondensed.className} ${workSans.className}`}>
22+
<ThemeProvider theme={theme}>
23+
{/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
24+
<CssBaseline />
25+
<Component {...props} />
26+
</ThemeProvider>
27+
</div>
1628
);
1729
}
1830

0 commit comments

Comments
 (0)