Got fetch error timeout when using getServerSideProps in production

398 Views Asked by At

i'm trying to popualte my page to seo using getServerSideProps and it's working fine in Development but in production it gives fetch error timeout FetchError: request to API failed, reason: connect ETIMEDOUT

// lib/fetchData.js

export const GetData = async (id) => {
    try {
        const res = await fetch(`api?id=${id}`, {
            method: 'Get',
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json"
            },
            agent,
        })
        const data = await res.json()
        return data

    } catch (error) {
        console.log(error);
    }
}

//in /myPage 
export async function getServerSideProps({ query }) {
    const data = await GetData(query.DetailedCampaign)
    console.log('data', data);
    return { props: { metaDetails: await data?.Data[0] } }
}

edit

I tried this way also and got the same issue it works fine in development and time out in production


export async function getServerSideProps({ query }) {
    try {
        const response = await fetch(`https://internalApi?id=${query.id}`, {
            method: 'Get',
            agent: httpsAgent,
            headers: {
                "Content-Type": "application/json",
            },
        });

        const data = await response.json();

        return { props: { metaDetails: data?.Data[0] } };
    } catch (error) {
        console.error(error);
        return { props: { metaDetails: null } };
    }
}
0

There are 0 best solutions below