260206_01_ENH : api 통신 모듈에 캐싱 기능 추가
This commit is contained in:
@@ -2,6 +2,7 @@ import type { NextConfig } from "next";
|
|||||||
|
|
||||||
const nextConfig: NextConfig = {
|
const nextConfig: NextConfig = {
|
||||||
/* config options here */
|
/* config options here */
|
||||||
|
output: "standalone",
|
||||||
reactCompiler: true,
|
reactCompiler: true,
|
||||||
|
|
||||||
async rewrites() {
|
async rewrites() {
|
||||||
|
|||||||
@@ -33,6 +33,11 @@ export type QueryValue = string | string[] | undefined;
|
|||||||
export type Query = Record<string, QueryValue>;
|
export type Query = Record<string, QueryValue>;
|
||||||
export type PathParams = Record<string, string | number | boolean>;
|
export type PathParams = Record<string, string | number | boolean>;
|
||||||
|
|
||||||
|
export type NextFetchOptions = {
|
||||||
|
revalidate?: number;
|
||||||
|
tags?: string[];
|
||||||
|
};
|
||||||
|
|
||||||
export type RequestOptions = {
|
export type RequestOptions = {
|
||||||
method?: HttpMethod;
|
method?: HttpMethod;
|
||||||
pathParams?: PathParams;
|
pathParams?: PathParams;
|
||||||
@@ -47,6 +52,8 @@ export type RequestOptions = {
|
|||||||
autoLogoutOnAuthError?: boolean;
|
autoLogoutOnAuthError?: boolean;
|
||||||
expectText?: boolean;
|
expectText?: boolean;
|
||||||
expectBinary?: boolean;
|
expectBinary?: boolean;
|
||||||
|
cache?: RequestCache; // "force-cache" | "no-store" | ...
|
||||||
|
next?: NextFetchOptions; // Next 서버 fetch 확장 옵션
|
||||||
};
|
};
|
||||||
|
|
||||||
export type HttpClientOptions = {
|
export type HttpClientOptions = {
|
||||||
@@ -140,12 +147,18 @@ export const createHttpClient = (baseURL: string, opts: HttpClientOptions = {})
|
|||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
const timer = setTimeout(() => controller.abort(), options.timeoutMs ?? defaultTimeoutMs);
|
const timer = setTimeout(() => controller.abort(), options.timeoutMs ?? defaultTimeoutMs);
|
||||||
|
|
||||||
|
const isServer = typeof window === "undefined";
|
||||||
|
const cacheMode: RequestCache = options.cache ?? (isServer && options.next ? "force-cache" : "no-store");
|
||||||
|
const nextOpt = isServer ? options.next : undefined;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch(url.toString(), {
|
const res = await fetch(url.toString(), {
|
||||||
method: options.method ?? (body ? "POST" : "GET"),
|
method: options.method ?? (body ? "POST" : "GET"),
|
||||||
headers,
|
headers,
|
||||||
body,
|
body,
|
||||||
cache: "no-store",
|
cache: cacheMode,
|
||||||
|
// TS에서 RequestInit에 next가 없어서 캐스팅 필요
|
||||||
|
...(nextOpt ? ({ next: nextOpt } as any) : {}),
|
||||||
signal: controller.signal,
|
signal: controller.signal,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user