Nextjs 动态路由与 next-i18next 构建错误

问题描述

我有一个将使用 id 参数呈现的编辑页面,它在应用程序运行时工作正常,但在构建 nextjs 应用程序时出现此错误

[错误:ENOENT:没有这样的文件或目录,重命名'C:\Users\Ahsan Nisar\Documents\GitHub\customer-portal\frontend.next\export\en\companies\edit[id].html' - > 'C:\Users\Ahsan Nisar\Documents\GitHub\customer-portal\frontend.next\server\pages\en\companies\edit[id].html']

Here

完整错误

我不确定这个错误与什么有关,或者我在我的代码中犯了什么错误,这个错误是在构建期间发生的。

这是我的页面代码

import { WithAuthorization } from 'common/roq-hocs';
import { MainLayout } from 'layouts';
import { useTranslation } from 'next-i18next';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
import React,{ FunctionComponent } from 'react';
import { CompaniesEditView } from 'views/companies-edit';

const CompanyCreatePage: FunctionComponent = () => {
  const { t } = useTranslation('companiesEdit');
  return (
    <MainLayout title={t('title')}>
      <WithAuthorization
        permissionKey="companies.update"
        failComponent={
          <div className="mt-16 text-2xl text-center text-gray-600">
            <span>{t('noView')}</span>
          </div>
        }
      >
        <CompaniesEditView />
      </WithAuthorization>
    </MainLayout>
  );
};

export const getStaticProps = async ({ locale }) => ({
  props: {
    ...(await serverSideTranslations(locale,['common','companiesEdit'])),},});

export const getStaticPaths = () => ({
  paths: ['/companies/edit/[id]'],fallback: true,});

export default CompanyCreatePage;

解决方法

我认为问题可能在于您没有在 paths 函数中返回预期的 getStaticPaths 模型。

此页面的最小示例:

import { GetStaticPaths,GetStaticProps } from 'next';
import { useRouter } from 'next/router';

const CompanyCreatePage = () => {
    const router = useRouter();
    const { id } = router.query;

    return (
        <div>
            <h1>Company Create Page Content for id: {id}</h1>
        </div>
    );
};

export const getStaticPaths: GetStaticPaths = async () => {
    // Get all possible 'id' values via API,file,etc.
    const ids = ['1','2','3','4','5']; // Example
    const paths = ids.map(id => ({
        params: { id },}));
    return { paths,fallback: false };
};

export const getStaticProps: GetStaticProps = async context => {
    return { props: {} };
};

export default CompanyCreatePage;

然后,导航到页面 /users/edit/3/ 返回以下内容

enter image description here

考虑到 fallback 中的 getStaticPaths 参数会改变 getStaticProps 函数的行为。如需参考,请参阅 documentation

相关问答

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