Next.js Discord

Discord Forum

nextjs cache until revalidation

Answered
Brown bear posted this in #help-forum
Open in Discord
Brown bearOP
Hello - i have a pretty big endpoint returning 1000+ rows of data and call revalidatePath(/api/bigdata) upon data change.

I also have 'export revalidate = 60' on the endpoint.

Currently the route doesnt use any dynamic params like cookies or headers, so i would expect nextJS to cache it at build time, SWR (via incremental static generation) at least every 60 seconds upon fetch, or refresh if revalidatePath was called at some time (even before 60 second hits).

How should i configure my endpoints so it behaves this way? For some reason, i am getting cache miss every time i hit this endpoint
Answered by Ray
try this
export const dynamic = "force-static";
export const revalidate = 60;
View full answer

28 Replies

Brown bearOP
Dynamic
It does make a POST call to Redis to fetch (via upstash sdk) but i don't think that matters?
Brown bearOP
import redis from "@/lib/redis";
import { NextResponse } from "next/server";

import { RedisStoreV2 } from "@/utils/env/kv";

export const revalidate = 60;

export const GET = async () => {
const res = await RedisStoreV2(redis).Players().getAll();

return NextResponse.json({
players: res
});
};
And Redis store just calls the upstash Redis sdk to fetch the data
Locally i see cache miss no-cache with a call log of POST to redis endpoint..
Even if i hit the endpoint directly
Brown bearOP
Yes
post request alwasy skip the cache
its fine as long as your GET endpoint is static
Brown bearOP
The redis call is POST (not with my domain, but to redis cluster)
But GET should be static per the code above right?
@Brown bear But GET should be static per the code above right?
check the build report
or post a screenshot here
Brown bearOP
@Brown bear Click to see attachment
try this
export const dynamic = "force-static";
export const revalidate = 60;
Answer
Brown bearOP
does force-static not honor query parameters
@Brown bear does force-static not honor query parameters
yes, if you are using query parameters, the route cannot be static
Brown bearOP
got it
ive changed the route so its not query param anoymore
thanks!
could you take a look at this one too if you get a chance? https://nextjs-forum.com/post/1227018401063374878
Brown bearOP
actually @Ray
if an endpoint is returning an error like 404, will the error also be cached?
with force-static
@Brown bear with force-static
then you shouldn't be able to build it, it should stop the build process with the error fetch failed