Cache a dynamic route handler
Answered
Florian posted this in #help-forum
FlorianOP
Does Next.js have a mechanism to cache this dynamic
If this was a
route.ts file? Or do I need to build my own caching mechanism (e.g. Redis)?If this was a
page.tsx, I could export generateStaticParams and it would cache each dynamic route after the first access. Can I achieve the same in route.ts?export async function GET(
req: Request,
{ params: { url } }: { params: { url: string } },
) {
try {
const ogResult = await ogScrape({ url });
if (ogResult.error) {
console.error(ogResult.result.error);
throw new Error("Failed to scrape og data");
}
const responseBody: OgData = {
link: url,
title: ogResult.result.ogTitle,
description: ogResult.result.ogDescription,
image: ogResult.result.ogImage?.[0].url,
};
return Response.json(responseBody);
} catch (error) {
console.error(error);
return Response.json({ error: "Internal server error" }, { status: 500 });
}
}17 Replies
@Florian Does Next.js have a mechanism to cache this dynamic `route.ts` file? Or do I need to build my own caching mechanism (e.g. Redis)?
If this was a `page.tsx`, I could export `generateStaticParams` and it would cache each dynamic route after the first access. Can I achieve the same in `route.ts`?
export async function GET(
req: Request,
{ params: { url } }: { params: { url: string } },
) {
try {
const ogResult = await ogScrape({ url });
if (ogResult.error) {
console.error(ogResult.result.error);
throw new Error("Failed to scrape og data");
}
const responseBody: OgData = {
link: url,
title: ogResult.result.ogTitle,
description: ogResult.result.ogDescription,
image: ogResult.result.ogImage?.[0].url,
};
return Response.json(responseBody);
} catch (error) {
console.error(error);
return Response.json({ error: "Internal server error" }, { status: 500 });
}
}
Yeah unfortunately generateStaticParams doesn’t work in route handlers yet (afaik). But I think you can just unstable_cache the query
FlorianOP
That sounds like it could work 😎 Thank you I'll try it out.
@Florian That sounds like it could work 😎 Thank you I'll try it out.
Actually seems like generateStaticParams is supported in route handlers https://discord.com/channels/752553802359505017/752647196419031042/1239500285613965343
that's so cool
So what I do is, I just return a placeholder URL. The other ones will be cached at first acccess. Does this make sense?
export function generateStaticParams() {
return [{ url: "https://example.com" }];
}you can just return
[] lolAnswer
thats how straightforward the ISR feature is
FlorianOP
Ah, thank you
Do you know if
unstable_cache gets cleared then the project is rebuilt/redeployed?no
they dont
but they have a limit now, not that its pricey but if you go over the limit, the old cache would just get thrown away, and youd probably need to call
ogScrape againFlorianOP
Interesting, thank you. Then maybe unstable_cache is still a better solution to avoid clearing the whole cache on every rebuild.
i honestly havent tried it but theorhetically you'd get best of both worlds haha
FlorianOP
Yea that could make sense