Skip to content

Commit a77e7ba

Browse files
committed
revaldate cache after fetch
1 parent af35f64 commit a77e7ba

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

app/api/revalidate/route.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { revalidatePath } from 'next/cache';
2+
import type { NextRequest } from 'next/server';
3+
4+
export async function GET(request: NextRequest) {
5+
const path = request.nextUrl.searchParams.get('path');
6+
7+
if (path) {
8+
revalidatePath(path);
9+
return Response.json({ revalidated: true, now: Date.now() });
10+
}
11+
12+
return Response.json({
13+
revalidated: false,
14+
now: Date.now(),
15+
message: 'Missing path to revalidate',
16+
});
17+
}

app/profile/[login]/components/fetch-user-button.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export const FetchUserButton: FC<FetchUserButtonProps> = ({
8080
fetchAttempt.current += 1;
8181
setLoadingLabel(FETCH_MESSAGES[fetchAttempt.current % FETCH_MESSAGES.length]);
8282
} else {
83+
await fetch(`/api/revalidate?path=${encodeURIComponent(`/profile/${login}`)}`);
8384
window.location.reload();
8485
}
8586
}, [login]);

app/profile/[login]/layout.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { TopRanksDocument } from '@/types/generated/graphql';
77
type ProfileLayoutProps = Readonly<{ children: React.ReactNode; params: Promise<{ login: string }> }>;
88

99
// Next.js will invalidate the cache when a
10-
// request comes in, at most once every 10800 seconds.
11-
export const revalidate = 10800;
10+
// request comes in, at most once every 3 hours.
11+
export const revalidate = 10800; // 3 hours
1212

1313
// We'll prerender only the params from `generateStaticParams` at build time.
1414
// If a request comes in for a path that hasn't been generated,

0 commit comments

Comments
 (0)