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

Viewed 2400

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.

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.

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?

2 Answers

You can add the lang attribute to the <Html> tag in your custom _document.

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
Related