Next.js Discord

Discord Forum

I am facing this issue on production website since many months now, not able to resolve it...

Unanswered
Champagne D’Argent posted this in #help-forum
Open in Discord
Avatar
Champagne D’ArgentOP
I have a button, clicking on it is calling an api in next
/api/callpredict/


Now almost once in 4 times, it show me this error when clicked, and work fine after refreshing the page and hiting again.

There is no error on the api side, it is actually not reaching to the api
Image
Image

43 Replies

Avatar
whats your trouble? I only see 500 from the api?
Avatar
Australian Freshwater Crocodile
maybe it reading orther file but that file isn't loading data from lib. you can check infor more about error.
Avatar
Bombay
/api/callpredict or /api.callpredict ?
Avatar
Champagne D’ArgentOP
No, the api works fine....just doing page refresh, everything works


I am guessing this could be cache issue, but already added force-dynamic to api, still same issue
for me all apis are working with / only, havn't tried .
what is this TCP fetch failed error for?
Avatar
Bombay
No I mean you said on your post /api.callpredict/ but on the console its /api/callpredict/
so just double checking first if it's the correct route
Avatar
Champagne D’ArgentOP
ahh, that's typo
I have corrected it. It was typo here
Avatar
Champagne D’ArgentOP
Avatar
Bombay
Does the samething happen locally or just prod/deployed?

Have you used a try catch in the route and logged the error?

What version of Next are you running, if it's the cache then it's enabled by default on fetch on versions prev 15.
Avatar
Champagne D’ArgentOP
It happens both locally (attached local screenshot) and prod.

This happens when opening the website, refreshing the page works fine.

That route is different issue, yes there is try catch but not error, it's actually not reaching to the route itself. This one happens in prod only.


Next version, 13.4.19
Image
Avatar
Champagne D’ArgentOP
That api route issue happened again just now on production website on another route
Image
same logs
Image
Same....on prod with no logs
Image
refreshing solves it
Avatar
Bombay
I'm getting the issue on endpoint https://cloth2life.ai/webapp/product/create
It's expecting JSON, but it's returning a 500 page. Thats why you get this error in particular
That's probably as far as I can go for diagnosing this issue without seeing code.
Avatar
Champagne D’ArgentOP
can you try refreshing the page and try the api again
it will work
that's where it is becoming difficult for me to diagnose
Avatar
Bombay
Have you any middleware
The error could be thrown in there
Avatar
Champagne D’ArgentOP
have logs in aws and just added sentry too
nothing else apart from these
Avatar
Bombay
Try removing the middleware in local - keep refreshing etc to see if the bug occurs again.
I have a feeling it might be middleware
Avatar
Champagne D’ArgentOP
can you help?

I can share screen 🙏🏻
Avatar
Bombay
Unfortunately, I cannot 😦
I don't have much time availablee
Avatar
Champagne D’ArgentOP
okay
let me keep trying
Avatar
Bombay
if you find something of interest
or maybe post here snippets of the route and any middleware
Avatar
Champagne D’ArgentOP
yeah
Avatar
Champagne D’ArgentOP
Here's the code for one of the api route:

import { NextResponse } from "next/server";

async function generateA() {
    const letters = "ABC"

    return letters;
}

export async function POST(request) {

    try {
        const formData = await request.formData();
        // console.log("----149 formData", formData);

        const WordA= await generateA();

       
        let body = {
            id: formData.get('id'),
            WordA: WordA,
        };

        const requestBody = {
            "body": body
        }

        const resp = await fetch(process.env.NEXT_PUBLIC_API, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(requestBody),
        });

        const responseData = await resp.json();

        return NextResponse.json({
            resp: responseData,
            success: true,
        }, { status: 200 })


    } catch (error) {
        console.log("---error in api", error);
        return NextResponse.json({
            error: "Error t api " + error
        },
            { status: 500 })
    }
}


export const dynamic = "force-dynamic";
could it be because
export const dynamic = "force-dynamic";
should be on top?
Avatar
Bombay
Next dynamic doesn't have to be at the top it's fine.

First, you need to check if ID is correctly received (non-error related):
id: formData.get('id')

Without nit picking but you could refactor it quite a bit:

  
        let body = {
            id: formData.get('id'),
            WordA: WordA,
        };

        const requestBody = {
            "body": body
        }



But I just tried to do GET on your route, then got the 500.

Then after I got the 405 (which is your route is POST).

Which means, the error has to occur before the function is called.
You need to look at your middleware.

Exclude the api route from the middleware: /((?!api|login|_next/static|_next/image).*) and then see if you still get the issue.
Avatar
Champagne D’ArgentOP
We don't have any middleware.js file at all in our project
Avatar
Bombay
You need to see what is being called or what is breaking it, it's something either Global server side, or executing before the routes.