问题描述
部署到 vercel 后在我的一个页面上出现此错误,在开发模式下一切正常.
Getting this error on one of my pages after deploying to vercel, it all works fine in dev mode.
我认为问题可能是我的 fetch/API 之一,因为它使用来自第一个 fetch 请求的数据作为第二个 fetch 请求的 URL...
I think the problem might be one of my fetch/APIs since it uses the data from the first fetch request as the URL for the second fetch request...
我所有其他具有不同 API/获取请求的页面都可以正常工作...
All of my other pages with different APIs / fetch requests work fine...
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(/
/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(/
/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(/
/g, "");
const brand = $2("#product-details dd:nth-child(4)")
.text()
.replace(/
/g, "");
newProducts.push({
name: name,
name2: name2,
brand: brand,
pageSrc: pageSrc,
price: price,
imageSrc: imageSrc,
});
}
return newProducts;
} catch (err) {}
};
module.exports = {
fetchData,
};
推荐答案
此错误表明 API 响应时间响应时间过长.
This error suggests that the API response is taking too long to respond.
当使用带有 Hobby
计划的 Vercel 时,您的无服务器 API 路由只能处理 5 秒.这意味着 5 秒后,路由会以 504 GATEWAY TIMEOUT
错误响应.
When using Vercel with a Hobby
plan, your serverless API routes can only be processed for 5 seconds. This means that after 5 seconds, the route responds with a 504 GATEWAY TIMEOUT
error.
使用 next dev
在本地运行时,这些限制不适用.
These same limits do not apply when running locally with next dev
.
要解决此问题,您需要减少 API 路由响应所需的时间,或升级您的 Vercel 计划.
To resolve this, you would need to reduce the amount of time your API route takes to respond, or upgrade your Vercel plan.
这篇关于NextJS/vercel - 504 错误'FUNCTION_INVOCATION_TIMEOUT'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!