微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?