1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-04-26 07:05:21 -04:00

properly use vercel data cache for fetch results

This commit is contained in:
Jake Jarvis 2025-04-04 11:32:20 -04:00
parent f0d4937f0f
commit 68b09ebc36
Signed by: jake
SSH Key Fingerprint: SHA256:nCkvAjYA6XaSPUqc4TfbBQTpzr8Xj7ritg/sGInCdkc
4 changed files with 42 additions and 33 deletions

View File

@ -1,9 +1,7 @@
import { NextResponse } from "next/server";
import { unstable_cache as cache } from "next/cache";
import redis from "../../../lib/helpers/redis";
export const dynamic = "force-static";
export const revalidate = 1800; // 30 mins
export const GET = async (): Promise<
NextResponse<{
total: {
@ -15,6 +13,8 @@ export const GET = async (): Promise<
}>;
}>
> => {
const { total, pages } = await cache(
async () => {
// get all keys (aka slugs)
const slugs = await redis.scan(0, {
match: "hits:*",
@ -42,5 +42,14 @@ export const GET = async (): Promise<
total.hits += page.hits;
});
return { total, pages };
},
undefined,
{
revalidate: 1800, // 30 minutes
tags: ["hits"],
}
)();
return NextResponse.json({ total, pages });
};

View File

@ -86,8 +86,8 @@ const getRepos = async (): Promise<Project[] | null> => {
...options,
cache: "force-cache",
next: {
// 10 minutes
revalidate: 600,
revalidate: 600, // 10 minutes
tags: ["github-api"],
},
});
},

View File

@ -12,8 +12,8 @@ const Gist = async ({ id, file }: GistProps) => {
const scriptResponse = await fetch(scriptUrl, {
cache: "force-cache",
next: {
// cache indefinitely in data store
revalidate: false,
revalidate: false, // cache indefinitely in data store
tags: ["gist"],
},
});

View File

@ -1,4 +1,4 @@
import { unstable_cache } from "next/cache";
import { unstable_cache as cache } from "next/cache";
import Image from "next/image";
import { EmbeddedTweet, TweetNotFound } from "react-tweet";
import { fetchTweet as _fetchTweet } from "react-tweet/api";
@ -12,9 +12,9 @@ export type TweetProps = Omit<ComponentPropsWithoutRef<typeof EmbeddedTweet>, "t
className?: string;
};
const fetchTweet = unstable_cache(async (id: string) => _fetchTweet(id), [], {
// cache indefinitely
revalidate: false,
const fetchTweet = cache(async (id: string) => _fetchTweet(id), undefined, {
revalidate: false, // cache indefinitely
tags: ["tweet"],
});
const Tweet = async ({ id, className, ...rest }: TweetProps) => {