problem new posts not fetched until build
Answered
Devon Rex posted this in #help-forum
Devon RexOP
I have a page following this structure, getArticles is an api call to my backend
However, when a new post is posted onto the dashboard, the new post is not taken until the next build.
I suspect that generateStaticParams like this
what do you think?
export default async function Page() {
const articles = await getArticles();
return (
<LearnPage
articles={articles}
/>
);
}However, when a new post is posted onto the dashboard, the new post is not taken until the next build.
I suspect that generateStaticParams like this
export async function generateStaticParams() {
const articles = await getArticles();
const data = articles.data
.map((article) => ({ lng: fallbackLng, slug: article.attributes.Slug }));
return data;
}what do you think?
Answered by joulev
export function POST(request: NextRequest) {
const searchParams = request.nextUrl.searchParams;
if (searchParams.get("secret") !== process.env.REVALIDATION_SECRET) return new Response(null, { status: 403 });
const pathname = searchParams.get("pathname");
revalidatePath(pathname);
return new Response(null);
}14 Replies
Devon RexOP
No one? 😦
@Devon Rex I have a page following this structure, getArticles is an api call to my backend
export default async function Page() {
const articles = await getArticles();
return (
<LearnPage
articles={articles}
/>
);
}
However, when a new post is posted onto the dashboard, the new post is not taken until the next build.
I suspect that generateStaticParams like this
export async function generateStaticParams() {
const articles = await getArticles();
const data = articles.data
.map((article) => ({ lng: fallbackLng, slug: article.attributes.Slug }));
return data;
}
what do you think?
a new post is posted onto the dashboardafter this, you need a revalidatePath
say, if
/blogs/hello-world is added, you should run revalidatePath("/blogs/hello-world") after adding the new post into the data source@joulev say, if `/blogs/hello-world` is added, you should run `revalidatePath("/blogs/hello-world")` after adding the new post into the data source
Devon RexOP
thank you for the message, so I need my api to call a revalidate endpoint? Will that also update the static list of article?
@Devon Rex thank you for the message, so I need my api to call a revalidate endpoint? Will that also update the static list of article?
to update any article, you can revalidatePath(<the url to the page of that article>)
@joulev to update any article, you can revalidatePath(<the url to the page of that article>)
Devon RexOP
yes but I mean, the list of articles into
/blogs/category1 for an example, calling revalidatePath will only refresh the article isn't it?@Devon Rex yes but I mean, the list of articles into `/blogs/category1` for an example, calling revalidatePath will only refresh the article isn't it?
what do you mean? you have a page containing the list of all articles? then yes can revalidatePath() that page too
Devon RexOP
hum, not sure I understand how that's working but thanks!
@joulev what do you mean? you have a page containing the list of all articles? then yes can revalidatePath() that page too
Devon RexOP
is it possible to do it through the middleware?
I tried to do something like this
But I am getting the error
I tried to do something like this
export function middleware(request: NextRequest) {
const { pathname, search, searchParams } = request.nextUrl;
if (searchParams.has('revalidate')) {
console.log('REVALIDATE', searchParams.get('revalidate'))
revalidatePath(pathname)
return NextResponse.redirect(pathname)
}
return NextResponse.next();
}But I am getting the error
static generation store missing in revalidatePath@Devon Rex is it possible to do it through the middleware?
I tried to do something like this
export function middleware(request: NextRequest) {
const { pathname, search, searchParams } = request.nextUrl;
if (searchParams.has('revalidate')) {
console.log('REVALIDATE', searchParams.get('revalidate'))
revalidatePath(pathname)
return NextResponse.redirect(pathname)
}
return NextResponse.next();
}
But I am getting the error `static generation store missing in revalidatePath`
no, not possible to do in middleware. it only works in server actions and app router route handlers
Devon RexOP
huh 😦 can I do a generic one to be able to refresh any route?
@Devon Rex huh 😦 can I do a generic one to be able to refresh any route?
umm
/api/revalidate?pathname=/foo/bar?Devon RexOP
for an example, but what will be the handler? same as my middleware but as server actions?
@Devon Rex for an example, but what will be the handler? same as my middleware but as server actions?
export function POST(request: NextRequest) {
const searchParams = request.nextUrl.searchParams;
if (searchParams.get("secret") !== process.env.REVALIDATION_SECRET) return new Response(null, { status: 403 });
const pathname = searchParams.get("pathname");
revalidatePath(pathname);
return new Response(null);
}Answer