Question

Why does revalidateTag work on local build but not in App Platform?

Code: Link

Output is as expected but the page only actually updates after two refreshes on the local build on my PC, and never on the hosted site.

src > app > api > revalidate > route.tsx

import { NextRequest, NextResponse } from 'next/server'
import { revalidateTag } from 'next/cache'

export async function GET(request: NextRequest) {
    const tag = request.nextUrl.searchParams.get('tag')
    revalidateTag(tag ?? "revalidate")
    return NextResponse.json({ revalidated: true, now: Date.now() })
}

src > app > lib > cms.tsx

import type { PortfolioItem, SocialMediaLink } from '@/lib/types.d'
import 'server-only'

const baseUrl = process.env.CMS_API_URL

export const getAllPortfolioEntries = async () => {
    try {
        const response = await fetch(baseUrl + 'portfolio-entries?populate=*', { next: { tags: ['revalidate'] } });

        if (!response.ok) {
            throw new Error(response.statusText)
        }

        const entries = await response.json()

        if (entries === null) {
            throw new Error('No entries found')
        }

        return entries.data as PortfolioItem[]
    } catch (error) {
        console.error(error)
    }
}

Console:

$  curl https://domain/api/revalidate?tag=revalidate
>  {"revalidated":true,"now":16867717638}
$  curl https://localhost:30000/api/revalidate?tag=revalidate
>  {"revalidated":true,"now":16867717638}

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
July 2, 2023
Accepted Answer

Hi there,

It sounds like that this might be a caching problem. What you could try is, in your fetch request, consider adding the appropriate cache control headers, for example, ‘no-store’ to prevent the browser or a CDN from caching the response.

You can add cache control headers to your fetch request like so:

const response = await fetch(baseUrl + 'portfolio-entries?populate=*', { 
    next: { tags: ['revalidate'] },
    headers: { 'Cache-Control': 'no-store' }
});

Quick overview:

  • You’re adding a headers field to the fetch options object.
  • Inside the headers field, you’re setting Cache-Control to no-store.

This tells any caches (like a browser’s local cache or a CDN) not to store the response. Each time you make the same request, it should go all the way to the server rather than being served from a cache, ensuring you get the most recent data.

Let me know how it goes!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel