question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enormous number of API requests using Incremental Static Generation with revalidation

See original GitHub issue

Bug report

Describe the bug

On two websites, I have revalidation set ranging from 5 minutes to 1 hour. However, enabling any revalidation time leads to an astonishingly high number of requests to my API, with very low traffic.

To Reproduce

A typical getStaticProps in my apps might look like this:

export async function getStaticProps(context) {
  const navigationResponse = await fetchAPI(fetchNavigation);
  const response = await fetchAPI(fetchHomepage);
  const eventsResponse = await fetchAPI(fetchEvents, {
    limit: 12,
    orderBy: 'startDateTime ASC',
  });

  return {
    props: {
      page: response.page,
      navigation: navigationResponse,
      events: eventsResponse.events,
    },
    revalidate: 300,
  };
}

fetchAPI is a common API function that looks like this:

const fetchAPI = async (query, variables = {}, token = null) => {
  const url = token
    ? `${process.env.NEXT_PUBLIC_API_URL}?token=${token}`
    : `${process.env.NEXT_PUBLIC_API_URL}`;

  try {
    const res = await fetch(url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Authorization: `Bearer ${process.env.NEXT_PUBLIC_GRAPHQL_TOKEN}`,
      },
      body: JSON.stringify({
        query,
        variables,
      }),
    });

    const json = await res.json();

    if (json.errors) {
      console.error(json.errors);
      throw new Error(json.errors);
    }

    return json.data;
  } catch (error) {
    console.error(error);
    throw new Error(error);
  }
};

With revalidation enabled, and with hardly 10 visitors per hour, I’m seeing the API be hit almost 20,000 times per hour (!).

With revaliation disabled, my API is hit 0 times per hour.

Expected behavior

I’d expect the number of times my API hit with revalidation set to 300 seconds, be close to zero.

System information

  • Version of Next.js: 9.5.4

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:10
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
howellscommented, Jul 9, 2021

@hereisfahad not really but today I discovered an issue with a large number of <Links> on a page, where each was being prefetched when they came into view: with ~100 links, each to a page that had revalidation enabled, hundreds of requests were being made to the server. I simply set prefetch={false} (prefetch on hover still works)

2reactions
leerobcommented, Sep 15, 2021

@howells’s comment is accurate. If you have next/link on the page, you could be prefetching pages that have revalidation set up, increasing the number of requests. In that case, prefetch={false} is a great solution, thank you for suggesting that.

@dipankarmaikap the Vercel logs will show a request for the HTML page (from the Edge) as well as the Serverless Function (that’s doing the revalidation). This is expected! For further details into the logs, you can use log integrations too: https://vercel.com/integrations#logging

Read more comments on GitHub >

github_iconTop Results From Across the Web

Enormous number of API requests using Incremental Static ...
Bug report. Describe the bug. On two websites, I have revalidation set ranging from 5 minutes to 1 hour. However, enabling any revalidation...
Read more >
Incremental Static Regeneration with Next.js - LogRocket Blog
Next.js v9.5 introduces Incremental Static Regeneration, a hybrid version of SSG and SSR, which can regenerate static pages during runtime.
Read more >
A Complete Guide To Incremental Static Regeneration (ISR ...
Incremental Static Regeneration (ISR) enables developers and content editors to use static-generation on a per-page basis, without needing ...
Read more >
Incremental Static Regeneration - Data Fetching - Next.js
Incremental Static Regeneration (ISR) enables you to use ... This secret will be used to prevent unauthorized access to the revalidation API Route....
Read more >
Considerations for Incremental Static Regeneration in Next.js
On demand revalidation. Next also offers another feature (currently in beta) that allows you to request a page is updated by sending an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found