I am fetching (using Nextjs 13's fetch
with {cache:'force-cache'}
) from an api that returns a random joke. I noticed that fetch
is being called twice during build.
Here's my code:
// page.js
import {RefreshButton} from './RefreshButton'
async function getRandomJoke(){
const res = await fetch("https://v2.jokeapi.dev/joke/Programming?type=single", {cache:'force-cache'})
const data = await res.json()
console.log("fetch called. Joke is: ", data['joke'])
return data['joke']
}
export default async function Home() {
const joke = await getRandomJoke()
return (
<div>
{joke}
<RefreshButton/>
</div>
)
}
and here's the build log:
[= ] info - Generating static pages (2/3)fetch called. Joke is: A programmer puts two glasses on his bedside table before going to sleep. A full one, in case he gets thirsty, and an empty one, in case he doesn't.
[== ] info - Generating static pages (2/3)fetch called. Joke is: The generation of random numbers is too important to be left to chance.
When the page is first rendered, it shows the first joke. Upon clicking on <RefreshButton />
, which is a client component that calls router.refresh()
on click, the second joke is shown.
My questions are:
- why is
fetch
called twice during build? - why would its data change upon refresh if it is statically generated?