Next.js Discord

Discord Forum

Fetch in layout only once per build not once per page

Unanswered
Pacific anchoveta posted this in #help-forum
Open in Discord
Pacific anchovetaOP
How do i fetch something only once per build and not once per page
Fetching in layout tsx fetches once per page

12 Replies

Dwarf Crocodile
export const dynamic = 'force-static'
@Dwarf Crocodile export const dynamic = 'force-static'
Pacific anchovetaOP
My site is static, but still stuff gets called per page in layout tsx not just once
@Pacific anchoveta My site is static, but still stuff gets called per page in layout tsx not just once
Dwarf Crocodile
yeah that's because layout has to render to finalize the html for every page
Because it wraps them
Why don't you try caching or putting the data into a json file
And importing from your json file into your layout
@Dwarf Crocodile Why don't you try caching or putting the data into a json file
Pacific anchovetaOP
It's an axion call, i've cached it using unstable cache, but i think the api is still being called a lot of times before it gets cached
And there's a built in cache if I know right
Pacific anchovetaOP
damn, i rewrote the component because it was client at first so i had to use axion, the whole point was to use fetch and i just forgot half way though, thanks for the help that should fix it.
Dwarf Crocodile
layouts are server components by default so
Yeah switching to native fetch and using inbuilt caching should work better or you could just stick to axion and make a global flag so other components wait for the data and don't override the initial one fetching again and again