生产环境使用getServerSideProps时遇Fetch请求超时错误求助
getServerSideProps生产环境Fetch请求超时问题
我使用getServerSideProps为页面填充SEO相关数据,该功能在开发环境运行正常,但在生产环境中出现Fetch请求超时错误,报错信息如下:
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); } } // 在 /myPage 页面中 export async function getServerSideProps({ query }) { const data = await GetData(query.DetailedCampaign) console.log('data', data); return { props: { metaDetails: await data?.Data[0] } } }
编辑补充:我也尝试了以下实现方式,依然遇到相同问题——开发环境正常,生产环境超时:
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 } }; } }
内容的提问来源于stack exchange,提问作者eslam elameen
相关产品推荐
相关产品推荐

