前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >NextJS x Typescript - Integration & Troubleshooting

NextJS x Typescript - Integration & Troubleshooting

作者头像
szhshp
发布2022-09-21 10:40:13
4190
发布2022-09-21 10:40:13
举报

NextJS x Typescript - Integration & Troubleshooting

"next": "9.4.4"

Deployments

API not work on Git Pages

TLDR: NextJS API needs dynamic server, check Vercel instead of Git Pages

ref: https://github.com/vercel/next.js/issues/9366#issuecomment-552212621

GitHub pages is a hosting provider for static pages. You can't host APIs on it. If you really want to host on GitHub pages you'll want to use https://github.com/zeit/next.js#static-html-export and not use API routes.

ZEIT Now allows you to use both static, dynamic and API routes in one application, so I would recommend having a look at that.

Please make it easy to get up and running with GitHub pages even if you want them to use Now instead.

We can't make GitHub pages support API routes, dynamic rendering etc. It's only for static files. Deploying to GitHub pages / static hosts is covered by next export

Styles

NextJS Custom Font

Include the font

Use next/Head tag to add a font, put your font files in public/fonts _document.tsx

代码语言:javascript
复制
class MyDocument extends Document {
  render() {
    return (
      <Html lang="en">
        <Head>
          // <link
          //   rel="preload"
          //   href="/fonts/noto-sans-v9-latin-regular.woff2"
          //   as="font"
          //   crossOrigin=""
          // />
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}
Prepare the font-face

Pure CSS or Scoped CSS:

代码语言:javascript
复制
@font-face {
  font-family: 'Noto Sans';
  src: url('/fonts/noto-sans-v9-latin-regular.woff2');
  font-weight: bold;
  font-style: normal;
  font-display: swap;
}

Material UI SCSS:

代码语言:javascript
复制
const theme = createMuiTheme({
  overrides: {
    MuiCssBaseline: {
      "@global": {
        "@font-face": {
          fontFamily: "Cascadia",
          src: "url(\"/fonts/Cascadia.woff2\")",
        },
      },
    },
  },
  typography: {
    fontSize: 14,
    fontFamily: [
      "Cascadia", // your custom font here
      "Source Han Sans SC",
      "Noto Sans CJK SC",
      "HanHei SC",
      "-apple-system",
      "BlinkMacSystemFont",
      "\"Segoe UI\"",
      "Roboto",
      "\"Helvetica Neue\"",
      "Arial",
      "sans-serif",
      "\"Apple Color Emoji\"",
      "\"Segoe UI Emoji\"",
      "\"Segoe UI Symbol\"",
    ].join(","),
  },
});

Debugging NextJS in Chrome

代码语言:javascript
复制
"dev": "NODE_OPTIONS='--inspect' next dev"

In Windows, use cross-env instead: See 'NODE_OPTIONS' is not recognized as an internal or external command, operable program or batch file.

Once you open a new tab in Google Chrome and go to chrome://inspect, you should see your Next.js application inside the "Remote Target" section. Now click "inspect" to open a screen that will be your debugging environment from now on.

'NODE_OPTIONS' is not recognized as an internal or external command, operable program or batch file.

Windows 10 Issue

代码语言:javascript
复制
npm i cross-env -S -D
代码语言:javascript
复制
{
    "name": "your-app",
    "version": "0.0.0",
    "scripts": {
    ...
    "start": "cross-env NODE_OPTIONS='--inspect' next dev -p 3001",
    }
}

Troubleshooting

The default export is not a React Component in page

代码语言:javascript
复制
Server Error
Error: The default export is not a React Component in page

This error happened while generating the page. Any console logs will be displayed in the terminal window.

Add a default export instead of normal export:

DO NOT DO THIS:

代码语言:javascript
复制
export const Comics = (): JSX.Element => {
 ...
};

DO THIS:

代码语言:javascript
复制
const Comics = (): JSX.Element => {
 ...
};

export default Comics

Warning: You're using a string directly inside Link .

The standard NextJS Link usage:

代码语言:javascript
复制
<Link href={{pathname:'/',query:{name:'demo',id:1}}}>
    <a>xxx</a>
</Link>

And per ESLint rules you may need href for a tag.

代码语言:javascript
复制
The href attribute is required for an anchor to be keyboard accessible. Provide a valid, navigable address as the href value. If you cannot provide an href, but still need the element to resemble a link, use a button and change it with appropriate styles. Learn more: https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/anchor-is-valid.mdeslintjsx-a11y/anchor-is-valid

You can skip the ESLint validation with below rule:

代码语言:javascript
复制
  "jsx-a11y/anchor-is-valid": [0]

If the child is a custom component that wraps an tag

If we used custom components as child of Link, we may need to pass a passHref

代码语言:javascript
复制
<Link href={chip.link || "#"} passHref>   
  <Chip
    key={title}
    size="small"
    color={chip.title === highlight ? "primary" : "default"}
    label={ `${title} (${count})` }
  />
</Link>

getStaticPaths & getStaticProps

TL'DR:

  • getStaticPaths determines the valid routes
  • getStaticProps determined the business logic after a valid route catchs.
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-07-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • NextJS x Typescript - Integration & Troubleshooting
    • Deployments
      • API not work on Git Pages
    • Styles
      • NextJS Custom Font
    • Debugging NextJS in Chrome
      • 'NODE_OPTIONS' is not recognized as an internal or external command, operable program or batch file.
    • Troubleshooting
      • The default export is not a React Component in page
      • Warning: You're using a string directly inside Link .
      • If the child is a custom component that wraps an tag
    • getStaticPaths & getStaticProps
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档