Replies: 1 comment
-
I have a feeling that So you'd have: const profile = content.frequentlyChanging ? 'minutes' : 'weeks'
cacheLife(profile) I kind of got it working on a quick demo: import { unstable_cacheLife as cacheLife } from "next/cache";
import { notFound } from "next/navigation";
// you'd get this from the CMS as metadata
const revalidateTimes = {
landingPage: 10, // 10 seconds
blogPost: 5, // 5 seconds
};
export default async function BlogPostPage({
params,
}: {
params: Promise<{ slug: string }>;
}) {
"use cache";
const { slug } = await params;
const origin = "http://localhost:3001";
const url = `${origin}/blog/${encodeURIComponent(slug)}`;
const res = await fetch(url);
if (!res.ok) notFound();
const post = await res.json();
const revalidate =
slug === "landing-page"
? revalidateTimes.landingPage
: revalidateTimes.blogPost;
cacheLife({ revalidate });
return ( /* JSX */ );
} Screen.Recording.2025-08-22.at.23.57.21.movSomething like that... I need to study and refine the mental model around |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
Have a way of setting ISR TTL based on the content, rather than statically for each route.
Non-Goals
No response
Background
The only alternative I've found is to literally copy-and-paste your route on the filesystem and provide export different cache settings, e.g.:
Proposal
When your data comes from a CMS, you usually have one catch-all route and you render pages based on what was visited. Something like
app/[slug]/page.tsx
with the following:However, it makes no sense to cache all pages equally. For example, the home page receives tons of traffic and rarely changes, so it makes sense to cache it with a higher TTL, but the rest — not so much. Also, for logged-in users, you don't want to cache anything, since they must see the changes they make to the content in the CMS.
Currently, to do the above, you need to essentially develop your own router that runs in
middleware.ts
and usesNextResponse.rewrite
to point to different copy-pasted routes that only differ in cache settings, e.g.:https://example.com/
→/normal-cache/home
, handled byapp/normal-cache/[slug]/page.tsx
https://example.com/foo
→/weaker-cache/foo
, handled byapp/weaker-cache/[slug]/page.tsx
https://example.com/foo?preview=true
→/no-cache/foo
, handled byapp/no-cache/[slug]/page.tsx
…and as you may guess, this is terrible in terms of DX and maintainability.
An ideal solution would be a single
app/[slug]/page.tsx
that looks like this:The filesystem router is designed to make routing easy and hassle-free, but in this situation, it does the exact opposite and is nothing but an obstacle that you have to overcome in an effort to build your own router..
Beta Was this translation helpful? Give feedback.
All reactions