revalidatePath not working with Route Handler and MongoDB (SSG) NEXT.JS 13.5.1
Unanswered
Nile Crocodile posted this in #help-forum
Nile CrocodileOP
I am developing an application utilizing SSG.
Retrieve data from mongoDB at build time and create static pages.
Generate pages at build time using generateStaticParams as follows.
getSalesCarsData retrieves data from mongoDB.
Retrieve data from mongoDB at build time and create static pages.
Generate pages at build time using generateStaticParams as follows.
getSalesCarsData retrieves data from mongoDB.
"/trucks/[page]/page.tsx"
export async function generateStaticParams() {
return await getSalesCarPages()
}
const Page = async ({ params: { page } }) => {
const { page_count, data } = await getSalesCarsData(page)
...other code
}
export default Page"function.ts"
export async function getSalesCarPages() {
const totalDataCount = await withDb(async (db) => {
const collection = await getCollection(db, 'collection')
const totalDataCount = await collection.countDocuments()
return totalDataCount
})
const maxPageSize = calculateMaxPageSize(totalDataCount)
const result = [...new Array(maxPageSize)].map((_, index) => ({ id: index + 1 }))
return result
}
export async function getSalesCarsData(pageNumber: string, searchParams?: SalesCarsSearchParamsType) {
const result: TrucksIndex = await _getAllCars({ pageNumber, searchParams })
return result
}
async function _getAllCars({ pageNumber, searchParams }): Promise<TrucksIndex> {
const cursorSize = pageNumber ? calculatePageCursorSize(pageNumber) : -1
const result = await withDb(async (db) => {
const collection = await getCollection<TrucksShow>(db, 'collection')
const data = await collection.find(searchParams || {}).sort({ slug: 1 }).toArray()
const page_count = calculateMaxPageSize(await collection.countDocuments(searchParams))
return { data, page_count }
})
return result
}1 Reply
Nile CrocodileOP
The build is a perfect success. As shown below
Define the API for revalidate as follows
After deployment, change mongoDB data manually.
In addition I go to the following URL and do the revalidate
Responses come back correctly like this
But the data is not updated.
The log shows getSalesCarsData is not called.
Do you know what could be causing this? I'm having a very hard time.
├ λ /api/revalidate/sales_cars 0 B 0 B
├ ◠/trucks/[page] 872 B 106 kBDefine the API for revalidate as follows
/api/revalidate/sales_cars/route.ts
export function GET() {
revalidatePath('(no-auth)/trucks/[page]')
revalidatePath('/trucks/[page]')
revalidatePath('/trucks/1')
revalidatePath('/')
return NextResponse.json({ revalidated: true, now: Date.now() })
}After deployment, change mongoDB data manually.
In addition I go to the following URL and do the revalidate
/api/revalidate/sales_carsResponses come back correctly like this
{"revalidated":true,"now":1699627968226}But the data is not updated.
The log shows getSalesCarsData is not called.
Do you know what could be causing this? I'm having a very hard time.