Report this

What is the reason for this report?

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

Posted on June 14, 2023

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}


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!

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.
0

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

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.