Skip to content

How to Configure the Cache-Control Response Header in Vercel Projects

Sometimes, you may need to change the default value of the Cache-Control response header returned when a page of your deployment is requested. An example of that is when you want to set up Incremental Static Regeneration and you are not using Next.js. If you are using Next.js, it is recommended to use getStaticProps as explained here.

Overview

Before you get started, you should have:

Let's say you have a page in your deployment called about. When this page is requested at yoursite.com/about, you would like to return the Cache-Control header with value s-maxage=1, stale-while-revalidate=59 so that Incremental Static Regeneration happens on this page for all subsequent requests based on the specified stale-while-revalidate value.

This guide shows three different methods of setting this Cache-Control header value on the about page.

Using the Vercel.json file

This applies if your project is not based on Next.js.

  1. Add the following code to your vercel.json file.
{
  "headers": [
        {
      "source": "/about.js",
      "headers": [
        {
          "key": "Cache-Control",
          "value": "s-maxage=1, stale-while-revalidate=59"
        }
      ]
    }
  ]
}

Setting a response header in Vercel.json

  1. Deploy your project and go to yoursite.com/about in your web browser.
  2. Inspect the Network tab in the developer tools to view the response headers for this page and look for the Cache-Control value.
Note:

You can also use the curl command to get the response headers from the command line by running the following command:

curl -v https://yoursite.com/about

Using the Next.config.js file

This applies if your project uses Next.js

  1. Add the following code to your next.config.js file.
/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  async headers() {
    return [
      {
        source: "/about",
        headers: [
          {
            key: "Cache-Control",
            value: "s-maxage=1, stale-while-revalidate=59",
          },
        ],
      },
    ];
  },
};

module.exports = nextConfig;

Setting a response header in Next.js.json

  1. Deploy your project and go to yoursite.com/about in your web browser.
  2. Inspect the Network tab in the developer tools to view the response headers for this page and look for the Cache-Control value.

You can also use the curl command as explained above.

Using Edge Functions

This applies if your project uses the latest version of Next.js.

  1. Create a folder called about inside pages/.
  2. Move your about.js file to the about folder and rename it to index.js.
  3. Add a new file called handler.js in the /pages/api folder.
  4. Add the following code to your handler.js file:
import { NextResponse } from 'next/server';

export const config = {
  runtime: 'edge',
};

export default async function handler(req) {
  return NextResponse.json(
    { name: 'Mary Lamb' },
    {
      status: 200,
      headers: {
        'Cache-Control': 's-maxage=1, stale-while-revalidate=59',
      },
    },
  );
}

Setting a response header in an Edge Function

  1. Deploy your project and go to yoursite.com/about in your web browser.
  2. Inspect the Network tab in the developer tools to view the response headers for this page and look for the Cache-Control value.

You can also use the curl command as explained above.

Couldn't find the guide you need?