Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
166
rated 0 times [  167] [ 1]  / answers: 1 / hits: 10083  / 3 Years ago, fri, august 13, 2021, 12:00:00

Getting this error on one of my pages after deploying to vercel, it all works fine in dev mode.


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...


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(/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,
};

More From » reactjs

 Answers
9

This error suggests that the API response is taking too long to respond.


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.


These same limits do not apply when running locally with next dev.


To resolve this, you would need to reduce the amount of time your API route takes to respond, or upgrade your Vercel plan.


[#997] Tuesday, August 3, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bryantc

Total Points: 455
Total Questions: 96
Total Answers: 110

Location: San Marino
Member since Thu, Jun 30, 2022
2 Years ago
bryantc questions
Tue, Mar 30, 21, 00:00, 3 Years ago
Fri, Jun 5, 20, 00:00, 4 Years ago
Wed, May 27, 20, 00:00, 4 Years ago
Wed, May 13, 20, 00:00, 4 Years ago
;