By A G
I have some static pages in my Next.js application (app router) that gets data from a database using Prisma. The data on those static pages change very rarely so the best option would be to manually revalidate it on demand. The solution I want to use works as expected locally: I make a build, open a page in a browser, update the DB data, run the route to revalidate, refresh the page and see the updated data. But it doesn’t work for the hosted app (App Platform), using the same process.
app -> (pages) -> my-page -> page.tsx
export default async function MyPage() {
const data = await getMyData();
return (
...show the data
);
}
app -> (pages) -> actions.ts
export async function getMyData() {
return await prisma.data.findMany({ orderBy: { order: 'asc' } });
}
app -> api -> revalidate -> route.ts
import { NextRequest, NextResponse } from 'next/server'
import { revalidatePath, revalidateTag } from 'next/cache'
export const dynamic = "force-dynamic";
export async function GET(request: NextRequest) {
const path = request.nextUrl.searchParams.get('path') ?? "/";
revalidatePath(path);
return NextResponse.json({ revalidated: path, now: Date.now() });
}
Example use of the revalidate route:
https://mysite.com/api/revalidate
https://mysite.com/api/revalidate?path=/(pages)/my-page
How to make on-demand revalidation not using the timing option? (export const revalidate). Why does it work locally but not on DO?
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!
Digital Ocean’s APP platform applies an additional level of cahcing through Cloudlfare.
In these cases, requests made to https://mysite.com/* will actually be cached by cloudflare, and they will never reach your nextJs server, so they will never get the new content (until the age of the cache reaches the max-age defined in the Cache-Control header, which I believe is 1 year.
DO says disabling this cache can be done by setting a Cache-control to no-cache in the responses of your server, but at the same time nextJS say this is not allowed
Not sure of a solution for this, other than don’t use NextJS + DO
Have you found any solution to that? I have pretty much same problem as you do. Wanted to use sanity webhooks to revalidate my content in next app but it works only localy.
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.