ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

25.03.13 Today I Learned๐Ÿ’ก

1. ‘์ผ์น˜ํ•˜๋Š” ์˜ค๋ฒ„๋กœ๋“œ๊ฐ€ ์—†๋‹ค’๋Š” ์—๋Ÿฌ (serverApi.ts)

2. ๊ฐ์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜

3. Next์˜ Image ํƒœ๊ทธ์˜ src ์†์„ฑ์— ๋™์ ์ธ url

4. ์ฑ”ํ”ผ์–ธ ๋ชฉ๋ก ํŽ˜์ด์ง€ ํƒ€์ž…์—๋Ÿฌ


 

'์ผ์น˜ํ•˜๋Š” ์˜ค๋ฒ„๋กœ๋“œ๊ฐ€ ์—†๋‹ค' ๋Š” ์—๋Ÿฌ(serverApi.ts)

 

  • fetch()์˜ ์ฒซ๋ฒˆ์งธ ์ธ์ž๋Š” ๋ฌธ์ž์—ด(url) ๐Ÿ‘‰๐Ÿป await๋กœ promise ๋ฒ—๊ธฐ๊ธฐ
// roitDataURL.ts

// ์ฑ”ํ”ผ์–ธ ๋ชฉ๋ก API
export const CHAMPION_LIST_URL = async (): Promise<string> => {
  const version = await getLatestVersion();
  return `${RIOT_BASE_URL}/cdn/${version}/data/ko_KR/champion.json`;
};
// serverApi.ts

// ์ฑ”ํ”ผ์–ธ ๋ชฉ๋ก ํŒจ์นญ
export async function fetchChampionList(): Promise<Champions[]> {
  const chamUrl = await CHAMPION_LIST_URL(); ๐Ÿ‘ˆ๐Ÿป chamUrl์€ Promise
  const res = await fetch(chamUrl, { next: { revalidate: 86400 } });
  const { data } = await res.json();

  return Object.values(data);

 

 

๊ฐ์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜

 

* API ์‘๋‹ต ๋ถ„์„ํ•˜๊ธฐ


* ๊ฐ์ฒด ์ค‘์— ๋™์ ์œผ๋กœ ๋ณ€ํ•˜๋Š” key(1001,1004 ๋“ฑ)๋ฅผ ๋™์ ์ธ ๋‹ค๋ฅธ ํƒ€์ž…์œผ๋กœ ์ง€์ •(์ด๋ฆ„์ด ๊ฐ™์€ ์•„์ดํ…œ์ด ์žˆ์œผ๋ฏ€๋กœ name ์†์„ฑ์œผ๋กœ key ์ง€์ • ๋ถˆ๊ฐ€)
* Object.values ๋กœ ๊ฐ์ฒด -> ๋ฐฐ์—ด ๋ณ€ํ™˜ ํ•˜์ง€๋งŒ, value๋งŒ ์ฃผ์–ด map์—์„œ key ์‚ฌ์šฉ ๋ชปํ•จ => Object.entries()
* ํƒ€์ž…์„ [string, Item][] ์œผ๋กœ ์ง€์ •

// serverApi.ts

// ์•„์ดํ…œ ๋ฐ์ดํ„ฐ ํŒจ์นญ
export async function fetchItemList(): Promise<[string, Item][]> {
  const itemUrl = await LOL_ITEM_URL();
  const res = await fetch(itemUrl, { cache: "force-cache" });
  const { data } = await res.json();

  return Object.entries(data);
}

 

Next์˜ Image ํƒœ๊ทธ์˜ src ์†์„ฑ์— ๋™์ ์ธ url

 

 <Image
     src={`${img_Url}${champion.image.full}`}
     alt="Picture of the Champion"
     width={250}
     height={250}
     className="mx-auto flex rounded-lg shadow-2xl"
  />
// next.config.mjs
/** @type {import('next').NextConfig} */
const NextConfig = {
  images: {
    formats: ["image/avif", "image/webp"],
    remotePatterns: [
      {
        protocol: "https",
        hostname: "ddragon.leagueoflegends.com",
        pathname: "/**",
      },
    ],
  },
};

export default NextConfig;

 

 

์ฑ”ํ”ผ์–ธ ๋ชฉ๋ก ํŽ˜์ด์ง€ ํƒ€์ž…์—๋Ÿฌ

 

  • ๋น„๋™๊ธฐ ํ•จ์ˆ˜ return ๊ฐ’ ์ œ๋„ค๋ฆญ (fetchChampionList)
// ์ฑ”ํ”ผ์–ธ ๋ชฉ๋ก ํŒจ์นญ
export async function fetchChampionList(): Promise<Champions[]> {
  const chamUrl = await CHAMPION_LIST_URL();
  const res = await fetch(chamUrl, { next: { revalidate: 86400 } });
  const { data } = await res.json();

  return Object.values(data);
}

 

 

 

 



 

๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2026/03   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ