Next.js Discord

Discord Forum

Disable fetch requests made at build-time

Answered
Eulophid wasp posted this in #help-forum
Open in Discord
Eulophid waspOP
I'm using NextJS 13 with the App Router.

Given this example:
async function getData() {
  const res = await fetch('https://api.example.com/abc')
 
  return res.json()
}
 
export default async function Page() {
  const data = await getData()
 
  return <main>{ data.title }</main>
}

Current Behaviour
When I run next build the page is optimized to become static. It expects the API to both; be available at build-time, and never change.

I can disable cache on the fetch call in order to fetch the API at runtime on the server as well, but the API will still be called at build-time.

The Issue
Assuming access to all the APIs that server components might call seems a bit weird to me for a build script. Currently my API server is not running when the build script is called. Is there a way to ensure that these fetch requests are not called at build-time, whether that's on a project-level or on a per-component basis.

Investigated Solutions
I don't want to disable static optimization completely either as I may still have static pages that do not rely on API calls.

Or should I be rethinking the way I'm deploying my site? (currently the API and NextJS are updated in parallel, maybe I need to take down the NextJS server, then stop and update the API server, start the API server, update and build the NextJS server, then restart the NextJS server.
Answered by Eulophid wasp
export const revalidate = 0;

async function getData() {
  const res = await fetch('https://api.example.com/abc')
 
  return res.json()
}
 
export default async function Page() {
  const data = await getData()
 
  return <main>{ data.title }</main>
}
View full answer

23 Replies

Eulophid waspOP
For now I've moved back to the pages router as getServerSideProps gives me the control I'm looking for.

If anyone has a solution for the App router then do let me know 🙂
Golden-winged Warbler
'use client' is probably what you are after. I suspect your page is being statically generated
Eulophid waspOP
Not quite, I do not wish to have my data fetched from the client either. I'd just like it to be fetched on the server at request time like with getServerSideProps, and not at build time.
Spectacled bear
export const revalidate=0
worked for me to get it to stop hitting my database during build
@joulev `export const dynamic = 'force-dynamic'` or `cache: 'no-cache'`
Eulophid waspOP
No-cache weirdly enough still causes it to run during build time even though there's no reason to!
@Spectacled bear export const revalidate=0
Eulophid waspOP
It looks like that did the trick!
Eulophid waspOP
export const revalidate = 0;

async function getData() {
  const res = await fetch('https://api.example.com/abc')
 
  return res.json()
}
 
export default async function Page() {
  const data = await getData()
 
  return <main>{ data.title }</main>
}
Answer
Ojos Azules
@Eulophid wasp What is your current behavior with this settings?
Are requests sent during the build phase? Are requests cached in runtime?
@Ojos Azules <@238641619010715649> What is your current behavior with this settings? Are requests sent during the build phase? Are requests cached in runtime?
Eulophid waspOP
So with no-cache the current behaviour is:
- Request is fired at build time (?)
- Request is fired at request time (and not cached as expected)

With export const revalidate = 0;:
- Request is fired at request time and not cached.
Ojos Azules
I have a similar issue. I want the requests not to be sent at the build stage (my API server is building in parallel and at the time of the Next build - it's not ready yet), but I want the requests to be cached at runtime
Eulophid waspOP
Same reason here, try out this suggestion by DogLovingCoder:
export const revalidate = 0;
From a quick test I did it seemed to solve the issue
Ojos Azules
but I want the requests to be cached at runtime
This is the problem for me 🙃
Eulophid waspOP
Aah sorry yep, yeah that's a problem
I tried joelev's syggestion for:
export const dynamic = 'force-dynamic'
But it still appeared to fetch at build-time
So I'm looking around online some more with the suggestions in this thread and there're a ton of Issues on Github all marked as closed regarding these issues
Eulophid waspOP
This issue seems to best describe the problem:
https://github.com/vercel/next.js/issues/50828
It looks like this is an ongoing bug that has been there for quite a while now so I think I'm just gonna stick to the pages router instead, is that an option for you @Ojos Azules ?
Ojos Azules
I'm not sure. Maybe I'll just give up with static pages and caching