Question

Next JS returning 500 error after adding new product

What are the details of your problem ?

I am encountering an issue in my nextjs application, The project i am working on is an e-commerce application where backend framework used is laravel and the frontend framework is nextjs.The application is currently hosted on Digital Ocean App Platform and has two separate environments, i.e production and staging.When i add a new product in admin panel, the new product is created and hence it is shown in homepage, but the problem is when i navigate to products detail page,and also when i hover on the product on homepage which uses nextjs Link component and has prefetch enabled it gives me 500 internal server error.

Things to note:

  1. I tested locally using both staging and production environments to try to replicate the issue , but on local machine it is working.
  2. I tested on staging environment that is deployed on Digital Ocean App Platform by adding a new product, and it is also working fine.
  3. This issue didn’t exist (or i didn’t know it existed) until 3 weeks ago.
  4. The application has currently around 1450 pages (ISR) on production environment.

Application specification and server specification

Nextjs and react version used:

{
 "next": "13.0.6",
 "react": "18.2.0",
 "react-dom": "18.2.0",
}

Production:

  • RAM:4GB
  • CPU:4vCPU’s
  • Bandwidth: 250GB
  • Instance:2

Staging:

  • RAM:2GB
  • CPU:2vCPU’s
  • Bandwidth: 100GB
  • Instance:1

Local Machine

  • RAM:32GB
  • CPU: Intel i5 13th gen

Below is the code for both homepage and product detail page :

Get static props for home page:

export async function getStaticProps() {
    const queryClient = new QueryClient();

    await queryClient.prefetchQuery({
        queryKey: [KEYS.bannerList],
        queryFn: getBannerList,
    });
    await queryClient.prefetchQuery({
        queryKey: [KEYS.featuredCategory],
        queryFn: fetchFeaturedCategory,
    });
    await queryClient.prefetchQuery({
        queryKey: [KEYS.getAllHomeData],
        queryFn: fetchAllHomeData,
    });

    return {
        props: {
            dehydratedState: dehydrate(queryClient),
        },
        revalidate: 10
    };
}

Get static paths and getstatic props for product detail page:

export async function getStaticPaths() {
    const data = await fetch(`${baseURL}/api/all-product`, {
        cache: 'no-cache',
    })
        .then((response) => response.json())
        .then((data) => data?.data);

    const paths = await data?.map((product) => ({
        params: { product_slug: product.slug },
    }));

    return {
        paths: paths || [],
        fallback: 'blocking',
    };
}

export async function getStaticProps(context) {
    const { product_slug } = context.params;
    const queryClient = new QueryClient();

    await queryClient.prefetchQuery({
        queryKey: [KEYS.productsDetails, { product_slug }],
        queryFn: () => getProductDetails(product_slug),
    })
    return {
        props: {
            dehydratedState: dehydrate(queryClient),
        },
        revalidate: 10
    };
}

These are the general info’s that i get back from network tabs: GENERAL

  • Request URL:{{hostname}}/_next/data/7CjVi7i3xpZZ4UAXQAoPQ/product/asus-vivobook-s-14-flip-2024-ryzen5-price.json
  • Request Method: HEAD
  • Status Code:500 Internal Server Error
  • Referrer Policy:no-referrer

RESPONSE HEADERS

  • alt-svc:h3=“:443”; ma=86400
  • cache-control:private
  • cf-cache-status:MISS
  • cf-ray:8e8b0f2acc1d7f25-MAA
  • content-type:text/html; charset=utf-8
  • date:Tue, 26 Nov 2024 15:54:50 GMT
  • priority:u=1,i
  • server:cloudflare
  • server-timing:cfExtPri
  • vary:Accept-Encoding
  • x-do-app-origin:ceec05bb-dd58-4262-bbe3-222ad9cedc15
  • x-do-orig-status:500
  • x-nextjs-matched-path:/product/[product_slug]
  • x-powered-by:Next.js

What did you try and what were you expecting?

  • I tried to replicate the issue in local environment in which i had no luck , so my best option is to go to digital ocean’s dashboard and force rebuild and redeploy which only works until the new product is added.

  • I also tried by updating the version to nextjs to latest stable version.

  • I tried using nextjs standalone output mode in local environment using docker, which is also working fine.

I am trying to figure out whether the error is from app platform side or nextjs framework side , since the problem is only arising on production environment.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
November 27, 2024

Heya,

The best way to move forward is by checking logs. I’ll recommend you to check this page here:

https://docs.digitalocean.com/products/app-platform/how-to/view-logs/

Types of Logs

App Platform records several different types of information about your app to during each stage of its deployment. You can access and review the following logs:

  • Activity: Maintains a timeline of your app’s deployments, including the commit hash that triggered the build, how long the build took, and the build’s start and end times.
  • Deployment details: Contains overview information about a specific deployment, including build duration and the amount of time that was billed during deployment. This page also contains the deployment’s build and deploy logs.
    • Build log: Records the commands and subsequent build processes used to build the app, including any errors or warnings emitted by the software building the app. These can be helpful for identifying issues with the application’s code or dependencies that may need to be addressed before the application can be deployed.
    • Deploy log: Records the deployment of the app to its server, including any errors or warnings emitted from the software being used to host the app.
  • Runtime log: Records messages and errors emitted by the app and its components while running. These logs can be useful for troubleshooting issues with the application’s logic or functionality.
  • Crash log: Records the log output from the instance before it crashed and a potential reason for the crash.

What you need is either the Runtime log or the Crash log.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.