Skip to main content

Command Palette

Search for a command to run...

Supercharge Your Next.js App with Built-In Caching Techniques

Published
2 min read
Supercharge Your Next.js App with Built-In Caching Techniques
D

👨‍💻 Frontend Advocate | Tech Blogger | Dedicated to creating intuitive user experiences and optimizing web performance with ReactJS and Next.js. Let’s innovate and build exceptional applications together! 🚀

This intro dives into how Next.js, a popular React framework, empowers developers with built-in caching features. We'll explore Static Site Generation (SSG), Incremental Static Regeneration (ISR), and server-side caching – all designed to keep your web apps running smooth and speedy. Stay tuned for deeper dives into each technique in upcoming articles!

Understanding Caching in Next.js

Next.js provides several built-in caching techniques designed to optimize the delivery of content to users. These include:

  • Static Site Generation (SSG)

  • Incremental Static Regeneration (ISR)

  • Server-Side Rendering (SSR) with revalidation

Each of these techniques plays a critical role in ensuring that your application is fast and efficient.

Static Site Generation (SSG)

SSG generates static HTML at build time for fast load times. Perfect for content that doesn't change frequently.

export async function getStaticProps() {
  const res = await fetch('https://api.example.com/data');
  const data = await res.json();

  return {
    props: {
      data,
    },
  };
}

const HomePage = ({ data }) => (
  <div>
    <h1>{data.title}</h1>
    <p>{data.content}</p>
  </div>
);

export default HomePage;

Incremental Static Regeneration (ISR)

ISR updates static content after the build, keeping pages fresh without full rebuilds. Specify revalidation time.

export async function getStaticProps({ params }) {
  const res = await fetch(`https://api.example.com/posts/${params.id}`);
  const post = await res.json();

  return {
    props: {
      post,
    },
    revalidate: 3600, // Revalidate every hour
  };
}

const BlogPost = ({ post }) => (
  <div>
    <h1>{post.title}</h1>
    <p>{post.content}</p>
  </div>
);

export default BlogPost;

Server-Side Rendering (SSR) with Caching

SSR generates pages on request and can cache responses, reducing server load for subsequent requests.

export async function getServerSideProps({ params, res }) {
  const productRes = await fetch(`https://api.example.com/products/${params.id}`);
  const product = await productRes.json();

  res.setHeader('Cache-Control', 's-maxage=10, stale-while-revalidate');

  return {
    props: {
      product,
    },
  };
}

const ProductPage = ({ product }) => (
  <div>
    <h1>{product.name}</h1>
    <p>{product.description}</p>
  </div>
);

export default ProductPage;

Conclusion

Next.js's built-in caching techniques—SSG, ISR, and SSR with caching—boost your app's performance. Stay tuned for detailed articles on each technique!

More from this blog

Untitled Publication

14 posts