next.jsi18nextnext-i18next

Nextjs dynamic routes with next-i18next build error


I have an edit page that will be rendered with an id parameter and it works fine when application is running but while building the nextjs app I get this error

[Error: ENOENT: no such file or directory, rename '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 the full error

I am not sure what this error is related to or what mistake am I making in my code that this error is occuring during build time.

Here is the code of my page

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;

Solution

  • I think that the problem might be that you are not returning the expected paths model in getStaticPaths function.

    Minimal example of this page:

    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;
    
    

    Then, navigating to the page /users/edit/3/ returns the following content

    enter image description here

    Take into account that the fallback param in getStaticPaths changes the behavior of getStaticProps function. For reference, see the documentation