Next.js Discord

Discord Forum

ISR not working.

Answered
Spectacled bear posted this in #help-forum
Open in Discord
Avatar
Spectacled bearOP
Hello, I wanted to use ISR for my nextjs 15 app. Basically I want my blog post page to be cached and revalidated each 60 seconds, so the views stay the same. But every time I refresh the page, it is fetching the views over and over instead of using the cached version. Is it because the nextjs 15 caching changes?

//app/blog/[slug]/page.tsx

export const revalidate = 60;

type Params = {
  slug: string;
};

export default async function Post(props: { params: Promise<Params> }) {
  const params = await props.params;
  const post = getBlogPostBySlug(params.slug);

  if (!post) {
    return notFound();
  }

  const content = await markdownToHtml(post.content || "");

  return (
    <div className="flex flex-col gap-2">
      <Link href="/blogs" className="text-gray-600 hover:text-gray-900">
        Back
      </Link>
      <PostHeader post={post} />
      <div
        dangerouslySetInnerHTML={{ __html: content }}
        className={markdownStyles["markdown"]}
      />
    </div>
  );
}

export async function generateMetadata(props: {
  params: Promise<Params>;
}): Promise<Metadata> {
  const params = await props.params;
  const post = getBlogPostBySlug(params.slug);

  if (!post) {
    return notFound();
  }

  const title = post.title;

  return {
    title,
  };
}

export async function generateStaticParams() {
  const posts = getAllBlogPosts();

  return posts.map((post) => ({
    slug: post.slug,
  }));
}


In post header I have this component
import { getViewCount } from "@/lib/actions/viewCount";

export async function ShowViews({ slug }: { slug: string }) {
  const { views } = await getViewCount(slug);

  return <div>{views} views</div>;
}
Answered by Spectacled bear
fixed it by adding the export const fetchCache = "force-cache" in the page
View full answer

6 Replies

Avatar
Spectacled bearOP
This is my build log
Route (app)                              Size     First Load JS
┌ ○ /                                    2.54 kB         158 kB
├ ○ /_not-found                          899 B           100 kB
├ ○ /blogs                               171 B           108 kB
├ ● /blogs/[slug]                        234 B           108 kB
├   └ /blogs/coding-journey
├ ○ /contact                             140 B          99.3 kB
├ ○ /journal                             1.77 kB         117 kB
├ ● /journal/[slug]                      234 B           108 kB
├   ├ /journal/october-2024
├   └ /journal/september-2024
├ ○ /opengraph-image.png                 0 B                0 B
└ ○ /uses                                140 B          99.3 kB
+ First Load JS shared by all            99.1 kB
  ├ chunks/215-a44b0ade0df77d3a.js       44.6 kB
  ├ chunks/4bd1b696-2d507bf053f7c2cf.js  52.6 kB
  └ other shared chunks (total)          1.91 kB


○  (Static)  prerendered as static content
●  (SSG)     prerendered as static HTML (uses generateStaticParams)
And you can see the problem when going to my page https://www.jakmaz.com/blogs. The journals are loaded blazingly fast but blogs with views not
Avatar
West African Lion
v15 switched fetch from default force-cache to no-store, if you want it to only be fetched every 60 seconds you need to set the fetch.options.cache back to 'force-cache' and fetch.options.next.revalidate to 60
Avatar
Spectacled bearOP
Thank you @West African Lion. If i set this, will it mean that the whole page will be cached and served as static html or only the fetch request to my database for getting the views will be reused and page will still render on server?
Avatar
Spectacled bearOP
fixed it by adding the export const fetchCache = "force-cache" in the page
Answer
Avatar
Spectacled bearOP
turning on logging of cache really helps out
import type { NextConfig } from "next";

const nextConfig: NextConfig = { logging: { fetches: { fullUrl: true } } };

export default nextConfig;