๐ํ๋ก๊ทธ๋๋ฐ/Next.js
[Nextjs] api return ๊ฐ ๊ฐฑ์ ์๋๋ ์ด์
TwoIceFish
2023. 12. 4. 21:49
์์ธ
fetch๋ก api๋ฅผ ํธ์ถ ์ cache๋ก ์ฒ๋ฆฌํ๊ธฐ ๋๋ฌธ์ด๋ค.
dev ํ๊ฒฝ์์๋ cache ์์ด ๊ฐฑ์ ๋์ ์๋์์ง๋ง production ํ๊ฒฝ์์๋ cache๋ก ๋ณ๊ฒฝ์ด ๋์ง ์์ ๊ฒ์ด๋ค.
prisma์ findMany findDelete๊ฐ ๊ณ ์ฅ๋์ค ์์๋ค
ํด๊ฒฐ๋ฐฉ๋ฒ
api tsํ์ผ์ dynamic ์ง์
// /api/log/route.ts
import prismadb from '@/app/libs/prismadb';
import { NextResponse } from 'next/server';
// ํธ์ถ์ cache ์์ด ์ํ
export const dynamic = 'force-dynamic';
export async function GET(request: Request) {
try {
const logs = await prismadb.log.findMany({});
return NextResponse.json(logs);
} catch (e) {
throw new Error('์์ด๋ ๋ฐ ๋น๋ฐ๋ฒํธ๋ฅผ ์ ๋๋ก ํ์ธํด ์ฃผ์ธ์');
}
}
์ฐธ๊ณ ๋ฌธํ
https://nextjs.org/docs/app/building-your-application/caching#on-demand-revalidation
Building Your Application: Caching | Next.js
An overview of caching mechanisms in Next.js.
nextjs.org