如何避免Next.js中的“您尚未声明namespacesRequired数组”错误?

时间:2020-08-24 15:42:17

标签: next.js i18next react-i18next next-i18next

我在多语言应用程序中使用了next-i18next模块。我不明白为什么会收到以下错误消息,因为我以与文档中所示相同的方式声明了名称空间。

错误消息:

You have not declared a namespacesRequired array on your page-level component: Home.
This will cause all namespaces to be sent down to the client, possibly negatively impacting the performance of your app.
For more info, see: https://github.com/isaachinman/next-i18next#4-declaring-namespace-dependencies

index.ts

import Head from 'next/head'
import styles from '../styles/Home.module.css'
import { withTranslation } from '../i18n'
import { useTranslation } from 'react-i18next';
// import NextI18NextInstance from '../i18n';

function Home({ coursesData }) {
  const { t, i18n } = useTranslation(['common', 'cars']);
  return (
    <>
      <h1>{t('common:indexHeader')}</h1>
      <h2>{t('cars:title')}</h2>
    </>
  )
}

// All works with this code, but I plan to use getServerSideProps()
// Home.getInitialProps = async () => {
//   return {
//     namespacesRequired: ['common', 'courses']
//   }
// }

export default Home

我该如何解决问题?

1 个答案:

答案 0 :(得分:3)

next-i18next目前尚不支持getServerProps,因此您需要继续使用getInitialProps

有关更多信息:https://github.com/isaachinman/next-i18next/issues/652