Closed dharmikumbhani closed 3 years ago
Solved:
import Document, {Html, Head, Main, NextScript } from 'next/document'
import { ServerStyleSheet } from 'styled-components'
export default class MyDocument extends Document {
static async getInitialProps(ctx) {
const sheet = new ServerStyleSheet()
const originalRenderPage = ctx.renderPage
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) =>
sheet.collectStyles(<App {...props} />),
})
const initialProps = await Document.getInitialProps(ctx)
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
),
}
} finally {
sheet.seal()
}
}
render() {
return (
<Html lang="en">
<Head>
<link
rel="preload"
href="/fonts/ProximaNova-Bold.woff"
as="font"
type="font/woff"
crossOrigin=""
/>
<link
rel="preload"
href="/fonts/ProximaNova-Regular.woff"
as="font"
type="font/woff"
crossOrigin=""
/>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
Also take care while using font-face inside createGlobalStyles. Make sure your font-family name is different
import { createGlobalStyle } from "styled-components";
const GlobalStyle = createGlobalStyle`
@font-face {
font-family: 'ProximaNova-Regular';
src: url('/fonts/ProximaNova-Regular.woff') format('woff');
font-style: normal;
font-weight: 400;
font-display: swap;
}
@font-face {
font-family: 'ProximaNova-Bold';
src: url('/fonts/ProximaNova-Bold.woff') format('woff');
font-style: bold;
font-weight: 700;
font-display: swap;
}
`;
function MyApp({ Component, pageProps }) {
return (
<>
<GlobalStyle />
<Component {...pageProps} />
</>
);
}
export default MyApp
pay close look at the font-family property you should use the same for example "fontFamily-style" and use the same format while using it for styling inside any styled component.
I added .otf font and its working on Google chrome but not on safari.
package.json
next.config.js is the same as that in the repository
_app.js
index.js
Results :
Google Chrome
Safari
PS: I by mistakenly created this issue in examples admin might. want to delete it from there