Caching time

This commit is contained in:
journey-ad 2024-06-14 09:54:45 +08:00
parent 3a88b39e3b
commit 3b2007de8d
6 changed files with 22 additions and 9 deletions

View File

@ -88,5 +88,11 @@
"next": ["const", "let", "var"]
}
]
},
"prettier/prettier": [
"warn",
{
"endOfLine": "auto"
}
]
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 250 KiB

After

Width:  |  Height:  |  Size: 158 KiB

View File

@ -26,7 +26,7 @@ const query = gql`
`;
// Function to handle GET requests
export async function GET(request: Request) {
const handler = async(request: Request) => {
const { searchParams } = new URL(request.url);
const hash = searchParams.get("hash");
@ -34,7 +34,7 @@ export async function GET(request: Request) {
if (!hash) {
return NextResponse.json(
{
message: "hash is required",
message: "`hash` is required",
status: 400,
},
{
@ -76,3 +76,5 @@ export async function GET(request: Request) {
);
}
}
export { handler as GET, handler as POST };

View File

@ -57,7 +57,7 @@ const schema = z.object({
withTotalCount: z.coerce.boolean().default(false),
});
export async function GET(request: Request) {
const handler = async (request: Request) => {
// Extract search parameters from the request URL
const { searchParams } = new URL(request.url);
const params = Object.fromEntries(searchParams.entries());
@ -92,12 +92,13 @@ export async function GET(request: Request) {
variables: {
queryInput: safeParams,
},
fetchPolicy: "no-cache",
});
return NextResponse.json(
{
data: data.search,
message: "Success",
message: "success",
status: 200,
},
{
@ -119,3 +120,5 @@ export async function GET(request: Request) {
);
}
}
export { handler as GET, handler as POST };

View File

@ -14,7 +14,9 @@ async function fetchData(hash64: string) {
notFound();
}
const data = await apiFetch(`/api/detail?hash=${hash}`); // Fetch data from API
const data = await apiFetch(`/api/detail?hash=${hash}`, {
next: { revalidate: 60 * 60 * 24 * 7 }, // cache for 7 days
});
return data;
}

View File

@ -1,5 +1,4 @@
import { Metadata } from "next";
import { redirect } from "next/navigation";
import { getTranslations } from "next-intl/server";
import { Link } from "@nextui-org/react";
@ -8,7 +7,6 @@ import SearchResultsList from "@/components/SearchResultsList";
import apiFetch from "@/utils/api";
import { MagnetIcon } from "@/components/icons";
import { siteConfig } from "@/config/site";
import { Toast } from "@/utils";
import {
DEFAULT_SORT_TYPE,
SEARCH_PAGE_SIZE,
@ -70,7 +68,9 @@ async function fetchData({
}
try {
const resp = await apiFetch(`/api/search?${params.toString()}`);
const resp = await apiFetch(`/api/search?${params.toString()}`, {
next: { revalidate: 60 * 60 * 6 }, // cache for 6 hours
});
if (isNewSearch) {
totalCount = resp.data.total_count;