在部署到vercel之后,在我的一个页面上得到这个错误,它在dev模式下都工作得很好。
我认为问题可能是我的fetch /API之一,因为它使用来自第一个fetch请求的数据作为第二个提取请求的URL .
所有其他带有不同API/获取请求的页面都能正常工作.
export const fetchData = async (page) => {
try {
const req = await fetch(
"https://www.productpage.com/new/" +
page
);
const html = await req.text();
const $ = cheerio.load(html);
let newProducts = [];
for (let i = 1; i < 25; i++) {
let name = $(`#product_listing > tbody > #_${i} > td:nth-child(2) > a`)
.text()
.replace(/\n/g, "");
let pageSrc = $(
`#product_listing > tbody > #_${i} > td:nth-child(2) > a`
).attr("href");
const price = $(`#product_listing > tbody >#_${i} > td.price.notranslate`)
.text()
.replace(/\n/g, "");
pageSrc = "https://www.productpage.com" + pageSrc;
const req2 = await fetch(pageSrc); // here it is using data from first fetch for a 2nd request..
const html2 = await req2.text();
const $2 = cheerio.load(html2);
const imageSrc = $2(
"#product-main-image .main-image-inner:first-child img"
).attr("src");
const name2 = $2("#product-details dd:nth-child(2)")
.text()
.replace(/\n/g, "");
const brand = $2("#product-details dd:nth-child(4)")
.text()
.replace(/\n/g, "");
newProducts.push({
name: name,
name2: name2,
brand: brand,
pageSrc: pageSrc,
price: price,
imageSrc: imageSrc,
});
}
return newProducts;
} catch (err) {}
};
module.exports = {
fetchData,
};
发布于 2021-08-13 14:48:26
此错误表明API响应花费的时间太长,无法响应。
当使用带有Hobby
计划的Vercel时,您的无服务器API路由可以只需处理5秒。这意味着5秒后,路由将以504 GATEWAY TIMEOUT
错误进行响应。
当使用next dev
在本地运行时,这些相同的限制不适用。
要解决这个问题,您需要减少API路由响应或升级Vercel计划所需的时间。
发布于 2022-09-20 05:39:35
就我而言,任何职能都没有出现重大延误。您可以在函数中使用此方法进行检查。
console.time()
在我的例子中,我需要安装mongodb。
"mongodb": "^3.6.3"
在mongodb安装之后,很慢的网站上的页面只是瞬间加载。
发布于 2022-11-24 12:22:43
在我的例子中,我的getServerSideProps
在某些情况下没有解决,所以我修复了它,并确保getServerSideProps
在每一种情况下都得到解决,现在它可以正常工作了。
https://stackoverflow.com/questions/68771480
复制相似问题