Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Next.js Nextjs字体SSR_Next.js_Font Face_Server Side Rendering - Fatal编程技术网

Next.js Nextjs字体SSR

Next.js Nextjs字体SSR,next.js,font-face,server-side-rendering,Next.js,Font Face,Server Side Rendering,我在SSR(Nextjs)上注入字体时遇到了问题,正如您从图像中看到的,页面加载时没有特定的字体(在我的例子中是Segoe,我在项目中本地存储),浏览器用一些默认字体替换它。这感觉就像文字在跳跃)所以我的问题是:是否有可能加载带有所需字体的页面。 这是我的密码 字体文件样式。ts: export default ` @font-face { font-display: swap; font-weight: normal; font-family: '

我在SSR(Nextjs)上注入字体时遇到了问题,正如您从图像中看到的,页面加载时没有特定的字体(在我的例子中是Segoe,我在项目中本地存储),浏览器用一些默认字体替换它。这感觉就像文字在跳跃)所以我的问题是:是否有可能加载带有所需字体的页面。 这是我的密码

字体文件样式。ts:

export default `
    @font-face {
      font-display: swap;
      font-weight: normal;
      font-family: 'Segoe-UI';
      src: url('/fonts/Segoe UI/Segoe UI.ttf');
      font-style: normal;
    }
    ...
`;
import fonts from 'public/static/fonts/style.ts';
...

render() {
   <Head>
     ...
     <link rel="preload" href="fonts/Segoe UI/Segoe UI.ttf" as="font" crossOrigin=""/>
     ...
   </Head>
)
}

MyDocument.getInitialProps = async (ctx) => {
    const sheets = new ServerStyleSheet();
    const originalRenderPage = ctx.renderPage;
    try {
        ctx.renderPage = () => originalRenderPage({
            enhanceApp: App => props => sheets.collectStyles(
                <App {...props} />,
            ), 
        });
        const initialProps = await Document.getInitialProps(ctx);

        return {
            ...initialProps,
            styles: (
                <>
                    <style dangerouslySetInnerHTML={{ __html: fonts }}/>
                    {initialProps.styles}
                    {sheets.getStyleElement()}
                </>
            ),
        };
    } finally {
        sheets.seal();
    }
};
文件\u文档:

export default `
    @font-face {
      font-display: swap;
      font-weight: normal;
      font-family: 'Segoe-UI';
      src: url('/fonts/Segoe UI/Segoe UI.ttf');
      font-style: normal;
    }
    ...
`;
import fonts from 'public/static/fonts/style.ts';
...

render() {
   <Head>
     ...
     <link rel="preload" href="fonts/Segoe UI/Segoe UI.ttf" as="font" crossOrigin=""/>
     ...
   </Head>
)
}

MyDocument.getInitialProps = async (ctx) => {
    const sheets = new ServerStyleSheet();
    const originalRenderPage = ctx.renderPage;
    try {
        ctx.renderPage = () => originalRenderPage({
            enhanceApp: App => props => sheets.collectStyles(
                <App {...props} />,
            ), 
        });
        const initialProps = await Document.getInitialProps(ctx);

        return {
            ...initialProps,
            styles: (
                <>
                    <style dangerouslySetInnerHTML={{ __html: fonts }}/>
                    {initialProps.styles}
                    {sheets.getStyleElement()}
                </>
            ),
        };
    } finally {
        sheets.seal();
    }
};
从“public/static/fonts/style.ts”导入字体;
...
render(){
...
...
)
}
MyDocument.getInitialProps=async(ctx)=>{
const sheets=new ServerStyleSheet();
const originalRenderPage=ctx.renderPage;
试一试{
ctx.renderPage=()=>原始renderPage({
增强App:App=>props=>sheets.collectStyles(
,
), 
});
const initialProps=wait Document.getInitialProps(ctx);
返回{
…初始道具,
风格:(
{initialProps.style}
{sheets.getStyleElement()}
),
};
}最后{
表1.seal();
}
};