[英]How to add lang attribute to html tag in Next.js?
在我的 Next.js 投資組合站點上運行了一些性能檢查后,我注意到主index.html
缺少一個lang
屬性 - 它作為可訪問性分數的扣除返回。
我可以通過將 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
還有其他方法可以添加lang
屬性嗎?
您可以將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
作為@juliomalves 答案的替代方案,next.config.js 文件也可用於定義語言。
使用以下內容在項目的根目錄中創建一個名為“next.config.js”的文件,
module.exports = {
i18n: {
locales: ["en"],
defaultLocale: "en",
},
};
參考: https://nextjs.org/docs/api-reference/next.config.js/introduction
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.