Workaround on caching a request with Headers
Unanswered
Indian mackerel posted this in #help-forum

Indian mackerelOP
i have a fetch request that i need to protect, i am currently using
basically i need to have a route in my express backend accessible only by the nextjs server, i also want to be able to cache the request
The reason i need to cache the request:
I need this request to display information in my page, this information doesnt need to change frequently, so i need to kinda SSG-like render the page (i cannot use SSG because my navbar has information based on
Authorization
header to do that but like this i cannot cache the request, how do i work around this? is there a way to "force-cache"
a request even if it uses headers? if not possible why this limitation?basically i need to have a route in my express backend accessible only by the nextjs server, i also want to be able to cache the request
The reason i need to cache the request:
I need this request to display information in my page, this information doesnt need to change frequently, so i need to kinda SSG-like render the page (i cannot use SSG because my navbar has information based on
getServerSession
which is a server function) and i dont want to use useSession
neither to keep as much as i can server-side14 Replies

European sprat
if it's a fetch and is using a static authorization header, why can't you cache it?

@European sprat if it's a fetch and is using a static authorization header, why can't you cache it?

Indian mackerelOP
i try but it doesnt get cached
this is the code
export async function getGroups() {
const groupsData = await (
await fetch(process.env.NEXT_PUBLIC_SERVER_URL + "/groups", {
method: "GET",
headers: {
Authorization: process.env.SUPERSECRET,
},
cache: "force-cache",
next: {
tags: ["groups"],
},
})
).json();
return groupsData;
}

Indian mackerelOP
.

European sprat
it doesn't cache in production?
show the full code for where that fetch function is used

@European sprat show the full code for where that fetch function is used

Indian mackerelOP
this is my file structure
inside
/[type]
page.js
/[group]
/[post]
page.js
inside
/[type]/page.js
and /[type]/[group]/[post]/page.js
...
export default async function Page({ params: { type } }) {
const groupsData = await getGroups();
...

European sprat
and it doesn't cache in production builds?

Indian mackerelOP
no it doesnt

@European sprat and it doesn't cache in production builds?

Indian mackerelOP
and i call it for each post so if i have 350 posts i'll have 350 exactly equal requesta

Indian mackerelOP
still have found no fix

Yellowfin tuna
Hi @Indian mackerel! did you find how to cache your fetch? I am in a similiar position. In local dev the "force-cache" seems to work, but in production it does not get cached.

@Yellowfin tuna Hi <@636111217244766208>! did you find how to cache your fetch? I am in a similiar position. In local dev the "force-cache" seems to work, but in production it does not get cached.

Indian mackerelOP
i did not, i put the issue in my todos and abandoned the project before trying to solve that issue again. i remember tho i struggled above and beyond with that so good luck with it, if you find a fix please share it so if i run into it again in the future i'll know how to solve it.
best of luck!
best of luck!

@Indian mackerel i did not, i put the issue in my todos and abandoned the project before trying to solve that issue again. i remember tho i struggled above and beyond with that so good luck with it, if you find a fix please share it so if i run into it again in the future i'll know how to solve it.
best of luck!

Yellowfin tuna
Thank you for your response! Will do!