๐Ÿžํ”„๋กœ๊ทธ๋ž˜๋ฐ/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