Cypress 中的下一个生产包没有定位静态文件

问题描述

我使用 next-i18next 进行翻译。 <rootDir>/public 目录内是我的翻译 *.json 文件

"next": "^10.0.2","next-i18next": "^8.1.1",

我的 Cypress 测试在运行我的生产包时失败,因为找不到完整的文本字符串。它只显示翻译键的值:t('someKey')

next-i18next.config.js 文件

module.exports = {
  i18n: {
    defaultLocale: 'en',locales: ['en'],},localePath: path.resolve('./public/static/locales'),};

next.config.js 文件

const defaultConfig = {
  i18n,redirects: require('./next-redirects'),}

_app.js:

function MyApp({ Component,pageProps }) {
   /// snipped
}

export default appWithTranslation(MyApp);

错误页面(不仅限于此单页)

export const SearchPage = () => {
  const router = useRouter();
  const { t } = useTranslation('search'); 

  // snipped //

  return (
    <>
      <Head>
        <title>{`${t('searchTitle')}`}</title>
        <Meta
          property="og:title"
          content={`${t('searchTitle')}`}
          key="title"
        />
        <Meta
          name="description"
          content={t('MetaDescription')}
        />
      </Head>
      <ItemsContext.Provider
        value={{snipped}}>
         <Search />
      </ItemsContext.Provider>
    </>
  );
};

SearchPage.namespace = 'SearchPage';

export const getServerSideProps = async ({ locale }) => ({
  // exposes `_nextI18Next` as props which includes our namespaced files
  props: {
    ...await serverSideTranslations(locale,['common','search']),}
});

export default SearchPage;

使用的命令:

"dev": "next dev","build": "next build","start": "next start","cypress.test": "npx cypress run",

运行 yarn dev 工作正常,如图所示:

enter image description here

运行 yarn build && yarn start && yarn cypress.test输出

enter image description here

如果我尝试在本地机器上使用 yarn build 复制失败,则翻译没问题。

来自关于静态文件服务的 nextJS docs

注意:Next.js 只会提供构建时位于公共目录中的资产。在运行时添加文件将不可用。我们建议使用第三方服务(如 AWS S3)进行持久文件存储。

似乎 serverSideTranslations 函数可能被视为运行时 - 因为我的页面在与动态路径 URL 相关时不是预先构建的。

请就 serverSideTranslation 考虑运行时与构建时提供建议?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...