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

Become a contributor for community

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

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.