【问题标题】:NextJS / vercel - 504 Error 'FUNCTION_INVOCATION_TIMEOUT'NextJS / vercel - 504 错误'FUNCTION_INVOCATION_TIMEOUT'
【发布时间】:2021-10-16 15:42:36
【问题描述】:

部署到 vercel 后在我的一个页面上出现此错误,在开发模式下一切正常。

我认为问题可能是我的 fetch/API 之一,因为它使用来自第一个 fetch 请求的数据作为第二个 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,
};

【问题讨论】:

    标签: javascript reactjs next.js vercel


    【解决方案1】:

    此错误表明 API 响应时间响应时间过长

    当使用带有Hobby 计划的 Vercel 时,您的无服务器 API 路由可以only be processed for 5 seconds。这意味着 5 秒后,路由会以 504 GATEWAY TIMEOUT 错误响应。

    使用next dev 在本地运行时,这些限制不适用。

    要解决此问题,您需要减少 API 路由响应所需的时间,或升级您的 Vercel 计划。

    【讨论】:

    • 谢谢!我试着四处搜索,这个问题的许多“答案”是只使用 heroku,但我已经用 heroku + basic JS 构建了这个网站,并决定尝试使用 nextJS 重新创建它会很有用。我会看看我是否可以减少 api res 时间。
    猜你喜欢
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 2020-11-27
    • 2022-01-17
    • 2021-04-12
    • 2022-01-02
    • 2021-04-19
    • 1970-01-01
    相关资源
    最近更新 更多