Skip to content

Commit e16d40c

Browse files
authored
The unstable cache (#9251)
* Remove fetch use unstable_cache * fix revalidate caching
1 parent df98140 commit e16d40c

File tree

5 files changed

+53
-87
lines changed

5 files changed

+53
-87
lines changed

app/changelog/[slug]/api/route.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

app/changelog/[slug]/page.tsx

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import {Fragment, Suspense} from 'react';
22
import {type Category, type Changelog} from '@prisma/client';
33
import * as Sentry from '@sentry/nextjs';
4+
import {GET as sessionHandler} from 'app/changelog/api/auth/[...nextauth]/route';
45
import type {Metadata, ResolvingMetadata} from 'next';
6+
import {unstable_cache} from 'next/cache';
57
import Link from 'next/link';
68
import {notFound} from 'next/navigation';
9+
import {getServerSession} from 'next-auth/next';
710
import {MDXRemote} from 'next-mdx-remote/rsc';
811

912
import Article from 'sentry-docs/components/changelog/article';
1013
import {mdxOptions} from 'sentry-docs/mdxOptions';
14+
import prisma from 'sentry-docs/prisma';
1115

1216
type ChangelogWithCategories = Changelog & {
1317
categories: Category[];
@@ -39,15 +43,30 @@ export async function generateMetadata(
3943
};
4044
}
4145

42-
const getChangelog = async slug => {
43-
const result = await fetch(
44-
`${process.env.BASE_URL || `https://${process.env.VERCEL_URL}` || 'https://localhost:3000'}/changelog/${slug}/api`
45-
);
46-
if (result.ok) {
47-
return result.json();
48-
}
49-
return null;
50-
};
46+
const getChangelog = unstable_cache(
47+
async slug => {
48+
const session = await getServerSession(sessionHandler);
49+
let published: boolean | undefined = undefined;
50+
if (!session) {
51+
published = true;
52+
}
53+
try {
54+
return await prisma.changelog.findUnique({
55+
where: {
56+
slug,
57+
published,
58+
},
59+
include: {
60+
categories: true,
61+
},
62+
});
63+
} catch (e) {
64+
return null;
65+
}
66+
},
67+
['changelog-detail'],
68+
{tags: ['changelog-detail']}
69+
);
5170

5271
export default async function ChangelogEntry({params}) {
5372
const changelog: ChangelogWithCategories | null = await getChangelog(params.slug);

app/changelog/api/route.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

app/changelog/page.tsx

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
import {Fragment} from 'react';
22
import * as Sentry from '@sentry/nextjs';
3-
import {GET as changelogsEndpoint} from 'app/changelog/api/route';
43
import type {Metadata} from 'next';
4+
import {unstable_cache} from 'next/cache';
55

66
import List from 'sentry-docs/components/changelog/list';
7+
import prisma from 'sentry-docs/prisma';
78

89
import Header from './header';
910

10-
const getChangelogs = async () => {
11-
if (process.env.CI) {
12-
// during CI, we invoke the API directly since the endpoint doesn't exist yet
13-
return (await changelogsEndpoint()).json();
14-
}
15-
const result = await fetch(
16-
`${process.env.BASE_URL || `https://${process.env.VERCEL_URL}` || 'https://localhost:3000'}/changelog/api`,
17-
{
18-
cache: 'force-cache',
19-
next: {tags: ['changelogs']},
20-
}
21-
);
22-
if (result.ok) {
23-
return result.json();
24-
}
25-
return [];
26-
};
11+
export const dynamic = 'force-dynamic';
12+
13+
const getChangelogs = unstable_cache(
14+
async () => {
15+
return await prisma.changelog.findMany({
16+
include: {
17+
categories: true,
18+
},
19+
where: {
20+
published: true,
21+
},
22+
orderBy: {
23+
publishedAt: 'desc',
24+
},
25+
});
26+
},
27+
['changelogs'],
28+
{tags: ['changelogs']}
29+
);
2730

2831
export default async function ChangelogList() {
2932
const changelogs = await getChangelogs();

src/actions/changelog.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ export async function unpublishChangelog(formData: FormData) {
2525
}
2626

2727
revalidateTag('changelogs');
28-
revalidatePath('/changelog', 'page');
29-
revalidatePath('/changelog/[slug]', 'page');
28+
revalidateTag('changelog-detail');
3029
return revalidatePath(`/changelog/_admin`);
3130
}
3231

@@ -48,8 +47,7 @@ export async function publishChangelog(formData: FormData) {
4847
}
4948

5049
revalidateTag('changelogs');
51-
revalidatePath('/changelog', 'page');
52-
revalidatePath('/changelog/[slug]', 'page');
50+
revalidateTag('changelog-detail');
5351
return revalidatePath(`/changelog/_admin`);
5452
}
5553

@@ -82,9 +80,6 @@ export async function createChangelog(formData: FormData) {
8280

8381
await prisma.changelog.create({data});
8482

85-
revalidateTag('changelogs');
86-
revalidatePath('/changelog', 'page');
87-
revalidatePath('/changelog/[slug]', 'page');
8883
return redirect(`/changelog/_admin`);
8984
}
9085

@@ -123,8 +118,7 @@ export async function editChangelog(formData: FormData) {
123118
}
124119

125120
revalidateTag('changelogs');
126-
revalidatePath('/changelog', 'page');
127-
revalidatePath('/changelog/[slug]', 'page');
121+
revalidateTag('changelog-detail');
128122
return redirect(`/changelog/_admin`);
129123
}
130124

@@ -144,7 +138,6 @@ export async function deleteChangelog(formData: FormData) {
144138
}
145139

146140
revalidateTag('changelogs');
147-
revalidatePath('/changelog', 'page');
148-
revalidatePath('/changelog/[slug]', 'page');
141+
revalidateTag('changelog-detail');
149142
return revalidatePath(`/changelog/_admin`);
150143
}

0 commit comments

Comments
 (0)