Skip to content

Commit d79e450

Browse files
committed
use cache :)
1 parent 5645ff2 commit d79e450

File tree

22 files changed

+175
-67
lines changed

22 files changed

+175
-67
lines changed

app/api/revalidate/route.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
import { revalidatePath } from 'next/cache';
1+
import { revalidateTag } from 'next/cache';
22
import type { NextRequest } from 'next/server';
33

44
export async function GET(request: NextRequest) {
5-
const path = request.nextUrl.searchParams.get('path');
5+
const tagName = request.nextUrl.searchParams.get('tag');
66

7-
if (path) {
8-
revalidatePath(path);
9-
return Response.json({ revalidated: true, now: Date.now() });
7+
if (tagName) {
8+
revalidateTag(tagName);
9+
return Response.json({ revalidated: true });
1010
}
1111

12-
return Response.json({
13-
revalidated: false,
14-
now: Date.now(),
15-
message: 'Missing path to revalidate',
16-
});
12+
return Response.json({ revalidated: false, message: 'Missing tag to revalidate' });
1713
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import { useQueryStates } from 'nuqs';
2+
import { useLayoutEffect, useState } from 'react';
23

34
import { BadgeNuqsSchema } from '@/badge/badge.nuqs';
45

56
export const useBadgeUrl = (githubLogin: string | undefined, githubId: string | undefined) => {
67
const [badgeParams] = useQueryStates(BadgeNuqsSchema);
8+
const [origin, setOrigin] = useState<string>('');
9+
10+
useLayoutEffect(() => {
11+
setOrigin(window.location.origin);
12+
}, []);
713

814
if (!githubId) {
915
return '';
1016
}
1117

1218
const queryParams = new URLSearchParams(badgeParams).toString();
13-
const origin = typeof window !== 'undefined' ? window.location.origin : '';
1419
return `${origin}/api/badge/${githubLogin}` + (queryParams ? '?' : '') + queryParams;
1520
};

app/badge/[[...login]]/layout.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Metadata } from 'next';
22

33
import { Header } from '@/components/header/header';
4+
import { Page } from '@/components/page/page';
45

56
export const metadata: Metadata = {
67
title: 'GitRanks · GitHub Profile Analytics & Rankings',
@@ -12,7 +13,15 @@ export default function BadgeLayout({ children }: Readonly<{ children: React.Rea
1213
return (
1314
<>
1415
<Header />
15-
{children}
16+
17+
<Page className="gap-6">
18+
<div>
19+
<h1 className="text-2xl font-semibold">Profile badge</h1>
20+
<div>Create a custom profile badge to showcase on your GitHub portfolio or anywhere you like!</div>
21+
</div>
22+
23+
{children}
24+
</Page>
1625
</>
1726
);
1827
}

app/badge/[[...login]]/loading.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Skeleton } from '@/components/ui/skeleton';
2+
3+
export default function Loading() {
4+
return (
5+
<div className="flex gap-8">
6+
<Skeleton className="h-14 flex grow" />
7+
<Skeleton className="h-14 flex grow" />
8+
</div>
9+
);
10+
}

app/badge/[[...login]]/page.tsx

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { Page } from '@/components/page/page';
1+
'use cache';
2+
3+
import { unstable_cacheLife as cacheLife, unstable_cacheTag as cacheTag } from 'next/cache';
4+
25
import { Separator } from '@/components/ui/separator';
36
import { graphqlDirect } from '@/lib/graphql/graphql-direct';
47
import { IdByLoginDocument } from '@/types/generated/graphql';
@@ -9,32 +12,29 @@ import { LoginForm } from './components/login-form';
912
import { Preview } from './components/preview';
1013

1114
export default async function Badge({ params }: { params: Promise<{ login?: string[] }> }) {
15+
cacheLife('hours');
16+
1217
const { login } = await params;
1318
const githubLogin = login?.[0];
1419
let githubId: string | undefined;
1520

1621
if (githubLogin) {
22+
cacheTag(`profile:${githubLogin}`);
23+
1724
const data = await graphqlDirect(IdByLoginDocument, { login: githubLogin });
1825
githubId = data.globalRankByLogin?.githubId;
1926
}
2027

2128
return (
22-
<Page className="gap-6">
23-
<div>
24-
<h1 className="text-2xl font-semibold">Profile badge</h1>
25-
<div>Create a custom profile badge to showcase on your GitHub portfolio or anywhere you like!</div>
26-
</div>
27-
28-
<div className="flex flex-col lg:flex-row gap-8 items-center lg:items-start">
29-
<div className="flex flex-col gap-6 min-w-2xs max-w-md flex-grow">
30-
<LoginForm githubLogin={githubLogin} githubId={githubId} />
31-
<Separator />
32-
<BadgeForm />
33-
<Separator />
34-
<IntegrationCode githubLogin={githubLogin} githubId={githubId} />
35-
</div>
36-
<Preview githubLogin={githubLogin} githubId={githubId} />
29+
<div className="flex flex-col lg:flex-row gap-8 items-center lg:items-start">
30+
<div className="flex flex-col gap-6 min-w-2xs max-w-md flex-grow">
31+
<LoginForm githubLogin={githubLogin} githubId={githubId} />
32+
<Separator />
33+
<BadgeForm />
34+
<Separator />
35+
<IntegrationCode githubLogin={githubLogin} githubId={githubId} />
3736
</div>
38-
</Page>
37+
<Preview githubLogin={githubLogin} githubId={githubId} />
38+
</div>
3939
);
4040
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import LoadingDefault from '../loading';
2+
3+
export default function Loading() {
4+
return <LoadingDefault />;
5+
}

app/by/[rankingType]/[page]/page.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
'use cache';
2+
3+
import { unstable_cacheLife as cacheLife } from 'next/cache';
4+
15
import { Page } from '@/components/page/page';
26
import { RankDelta } from '@/components/rank-delta/rank-delta';
37
import {
@@ -54,6 +58,8 @@ function getConfigByRankingType(rankingType: string) {
5458
}
5559

5660
export default async function GlobalRanking({ params }: { params: Promise<{ rankingType: string; page: string }> }) {
61+
cacheLife('hours');
62+
5763
const { rankingType, page: pageParam } = await params;
5864
const page = parseInt(pageParam, 10);
5965
const [queryOrder, rankPropName, title, subtitle, rankingBaseEntity] = getConfigByRankingType(rankingType);
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ export const metadata: Metadata = {
88
'Explore ranks based on stars, followers, contributions, and more. Dive into dynamic leaderboards and find out how you measure up against developers worldwide.',
99
};
1010

11-
export const revalidate = 10800;
12-
export const dynamicParams = true;
13-
1411
export async function generateStaticParams() {
1512
const page = '1';
1613
return [
@@ -20,7 +17,7 @@ export async function generateStaticParams() {
2017
];
2118
}
2219

23-
export default function RootLayout({ children }: Readonly<{ children: React.ReactNode }>) {
20+
export default function RankingListLayout({ children }: Readonly<{ children: React.ReactNode }>) {
2421
return (
2522
<>
2623
<Header />

app/by/[rankingType]/loading.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Page } from '@/components/page/page';
2+
import { Skeleton } from '@/components/ui/skeleton';
3+
4+
export default function Loading() {
5+
return (
6+
<Page className="max-w-5xl gap-6">
7+
<div className="flex flex-col gap-4">
8+
<Skeleton className="h-8 w-[200]" />
9+
<Skeleton className="h-5 w-full" />
10+
</div>
11+
</Page>
12+
);
13+
}

app/layout.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import { NuqsAdapter } from 'nuqs/adapters/next/app';
66
import { Footer } from '@/components/footer/footer';
77
import { ThemeProvider } from '@/components/theme-provider/theme-provider';
88
import { Toaster } from '@/components/ui/sonner';
9-
10-
import { PostHogProvider } from '../lib/posthog/post-hog-provider';
9+
import { PostHogClientOnlyProvider } from '@/lib/posthog/post-hog-client-only';
1110

1211
import './globals.css';
1312

@@ -19,16 +18,12 @@ export const metadata: Metadata = {
1918
'Explore ranks based on stars, followers, contributions, and more. Dive into dynamic leaderboards and find out how you measure up against developers worldwide.',
2019
};
2120

22-
export default function RootLayout({
23-
children,
24-
}: Readonly<{
25-
children: React.ReactNode;
26-
}>) {
21+
export default function RootLayout({ children }: Readonly<{ children: React.ReactNode }>) {
2722
return (
2823
<html lang="en" suppressHydrationWarning>
2924
<body className={`${inter.className} antialiased`}>
3025
<SessionProvider>
31-
<PostHogProvider>
26+
<PostHogClientOnlyProvider>
3227
<NuqsAdapter>
3328
<ThemeProvider attribute="class" defaultTheme="system" enableSystem disableTransitionOnChange>
3429
<div className="flex flex-col min-h-screen">
@@ -38,7 +33,7 @@ export default function RootLayout({
3833
<Toaster richColors position="top-right" />
3934
</ThemeProvider>
4035
</NuqsAdapter>
41-
</PostHogProvider>
36+
</PostHogClientOnlyProvider>
4237
</SessionProvider>
4338
</body>
4439
</html>

0 commit comments

Comments
 (0)