Nextjs dynamic route redirect to 404 after certain days

28 views Asked by At

I'm encountering an issue with Next.js 12.3.1 where some dynamic routes unexpectedly redirect to the 404 page after a period of time (randomly sometimes it works for 1 week, sometimes for 2 weeks), despite initially functioning correctly. This behaviour occurs sporadically, affecting only certain routes.

APIs are working perfectly and no any problem with the APIs.

My Code:

export const getServerSideProps: GetServerSideProps = async ({ params }) => {
  const paramSlug = Array.isArray(params?.slug)
    ? params?.slug && params?.slug.length > 0
      ? params?.slug?.join('/')
      : ''
    : params?.slug;

  try {
    if (!paramSlug) {
      throw new Error('Not found!');
    }

    const api = new Api();
    const { body }: { body: any } = await api.get(paramSlug);
    const payload = body?.payload as IResourcePage;
    // if body is null then throw error
    if (!payload) {
      throw new Error(body.message);
    }
    const seo = (body?.seo_data || null) as string | null;

    return {
      props: {
        seo,
        pageData: payload || null,
      },
    };
  } catch (error: any) {
    return {
      notFound: true,
    };
  }
};

Can anyone provide insights or solutions to diagnose and address this problem?"

0

There are 0 answers