ํฐ์คํ ๋ฆฌ ๋ทฐ
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
- next.config.mjs ํ์ผ ์ค์
- ํ๋กํ ์ฝ, ํธ์คํธ๋ค์, ํจ์ค๋ค์
- (modules.export -> const / export default) ์ฐธ๊ณ ํ ๋ธ๋ก๊ทธ
<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);
}

'Forntend > Trouble Shooting' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [์ฌ๋๋ณ_Day8] Tanstack Query ์ด๊ธฐ ๋ก๋ฉ ์ฒ๋ฆฌ - initialData (0) | 2025.04.06 |
|---|---|
| [LoL info App] 0314 ํธ๋ฌ๋ธ์ํ (0) | 2025.03.17 |
| [/\/] fetch ์ต์ ์ ํ์์ฑ (0) | 2025.03.13 |
| vercel ๋ฐฐํฌ ์ค๋ฅ - ์๋ก๊ณ ์นจ 404: NON_FOUND (0) | 2025.02.09 |
| [Pokemon PJ_Day 3]Dynamic Route์ QueryString (0) | 2025.02.06 |