#Workaround on caching a request with Headers

15 messages · Page 1 of 1 (latest)

ionic sable
#

i have a fetch request that i need to protect, i am currently using 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-side

lucid sequoiaBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

rancid nimbus
#

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

ionic sable
#

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;
}
ionic sable
#

.

rancid nimbus
#

it doesn't cache in production?

#

show the full code for where that fetch function is used

ionic sable
rancid nimbus
#

and it doesn't cache in production builds?

ionic sable
#

no it doesnt

ionic sable
ionic sable
#

still have found no fix

clever sorrel
#

Hi @ionic sable! 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.

ionic sable