mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-09-16 21:05:33 -04:00
fix /api/hits edge caching
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import redis from "../../../lib/helpers/redis";
|
import redis from "../../../lib/helpers/redis";
|
||||||
|
|
||||||
export const revalidate = 900; // 15 mins
|
export const dynamic = "force-static";
|
||||||
|
export const revalidate = 1800; // 30 mins
|
||||||
|
|
||||||
export const GET = async (): Promise<
|
export const GET = async (): Promise<
|
||||||
NextResponse<{
|
NextResponse<{
|
||||||
@@ -14,12 +15,15 @@ export const GET = async (): Promise<
|
|||||||
}>;
|
}>;
|
||||||
}>
|
}>
|
||||||
> => {
|
> => {
|
||||||
|
// get all keys (aka slugs)
|
||||||
const slugs = await redis.scan(0, {
|
const slugs = await redis.scan(0, {
|
||||||
count: 50,
|
type: "string",
|
||||||
|
// set an arbitrary yet generous upper limit, just in case...
|
||||||
|
count: 99,
|
||||||
});
|
});
|
||||||
|
|
||||||
// fetch all rows from db sorted by most hits
|
// get the value (number of hits) for each key (the slug of the page) and pair them together
|
||||||
const data = await Promise.all(
|
const pages = await Promise.all(
|
||||||
slugs[1].map(async (slug) => {
|
slugs[1].map(async (slug) => {
|
||||||
const hits = (await redis.get(slug)) as number;
|
const hits = (await redis.get(slug)) as number;
|
||||||
return {
|
return {
|
||||||
@@ -30,14 +34,14 @@ export const GET = async (): Promise<
|
|||||||
);
|
);
|
||||||
|
|
||||||
// sort by hits
|
// sort by hits
|
||||||
data.sort((a, b) => b.hits - a.hits);
|
pages.sort((a, b) => b.hits - a.hits);
|
||||||
|
|
||||||
// calculate total hits
|
// calculate total hits
|
||||||
const total = { hits: 0 };
|
const total = { hits: 0 };
|
||||||
data.forEach((page) => {
|
pages.forEach((page) => {
|
||||||
// add these hits to running tally
|
// add these hits to running tally
|
||||||
total.hits += page.hits;
|
total.hits += page.hits;
|
||||||
});
|
});
|
||||||
|
|
||||||
return NextResponse.json({ total, pages: data });
|
return NextResponse.json({ total, pages });
|
||||||
};
|
};
|
||||||
|
@@ -7,6 +7,11 @@ const HitCounter = async ({ slug }: { slug: string }) => {
|
|||||||
await connection();
|
await connection();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// if this is a new slug, redis will automatically create a new key and set its value to 0 (and then 1, obviously)
|
||||||
|
// https://upstash.com/docs/redis/sdks/ts/commands/string/incr
|
||||||
|
// TODO: maybe don't allow this? or maybe it's fine? kinda unclear how secure this is:
|
||||||
|
// https://nextjs.org/blog/security-nextjs-server-components-actions
|
||||||
|
// https://nextjs.org/docs/app/building-your-application/rendering/server-components
|
||||||
const hits = await redis.incr(slug);
|
const hits = await redis.incr(slug);
|
||||||
|
|
||||||
// we have data!
|
// we have data!
|
||||||
|
Reference in New Issue
Block a user