Next.js Discord

Discord Forum

Cache request over 2mb

Answered
Devon Rex posted this in #help-forum
Open in Discord
Devon RexOP
Hello everyone, I am fetching from an endpoint that I don’t control and the response is 2.6mb, which is over the 2mb cache limit. My work around to this was to create my own route, fetch the data, and return only what I need. This works in development and my response gets cached, but at build time my route isn’t active and it fails to fetch and my build fails. What can I do to fix this?
Answered by Devon Rex
Solved by using unstable_cache

const getCachedData = unstable_cache(async () => {
        const res = await fetch(
            `URL TO FETCH`,
        );
                const data = await res.json();
        return data ;
}, ["cached-data"]);

Now call your function and now you will have your response cached
const data = await getCachedData();
View full answer

1 Reply

Devon RexOP
Solved by using unstable_cache

const getCachedData = unstable_cache(async () => {
        const res = await fetch(
            `URL TO FETCH`,
        );
                const data = await res.json();
        return data ;
}, ["cached-data"]);

Now call your function and now you will have your response cached
const data = await getCachedData();
Answer