By JuanG4
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!
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:
headers
field to the fetch options object.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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
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
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.