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.
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:
Quick overview:
headers
field to the fetch options object.headers
field, you’re settingCache-Control
tono-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