简体   繁体   English

如何在 Next.js 中的 html 标签中添加 lang 属性?

[英]How to add lang attribute to html tag in Next.js?

After running some performance check on my Next.js portfolio site I noticed that the main index.html is missing a lang attribute - which gets returned as a deduction from the accessibility score.在我的 Next.js 投资组合站点上运行了一些性能检查后,我注意到主index.html缺少一个lang属性 - 它作为可访问性分数的扣除返回。

I can add the locale by using the i18n setup to next.config.js , but those features are incompatible with next export - the site is statically generated.我可以通过将 i18n 设置添加到next.config.js来添加语言环境,但这些功能与next export不兼容 - 该站点是静态生成的。

Error: i18n support is not compatible with next export. See here for more info on deploying: https://nextjs.org/docs/deployment

Are there any other ways to add the lang attribute?还有其他方法可以添加lang属性吗?

You can add the lang attribute to the <Html> tag in your custom _document .您可以将lang属性添加到自定义_document中的<Html>标记。

import Document, { Html, Head, Main, NextScript } from 'next/document'

class MyDocument extends Document {
  render() {
    return (
      <Html lang="en">
        <Head />
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}

export default MyDocument

As alternative to @juliomalves answer, next.config.js file can also be used to define lang.作为@juliomalves 答案的替代方案,next.config.js 文件也可用于定义语言。

create a file called "next.config.js" in the root of your project with the below,使用以下内容在项目的根目录中创建一个名为“next.config.js”的文件,

module.exports = {
    i18n: {
        locales: ["en"],
        defaultLocale: "en",
    },
};

ref: https://nextjs.org/docs/api-reference/next.config.js/introduction参考: https://nextjs.org/docs/api-reference/next.config.js/introduction

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM