-
Notifications
You must be signed in to change notification settings - Fork 1
Feat/#9 msw 초기 설정 및 HydrationBoundaryPage 추가 #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5633837
remove: public에서 기본 이미지들 삭제
leeleeleeleejun 21e5932
fix: tsconfig.json에서 경로 alias를 app/*로 수정
leeleeleeleejun eb2a6a7
fix: .gitignore에 .env 파일 추가
leeleeleeleejun 23ec850
feat: MSW 설치 및 초기 설정
leeleeleeleejun 647083d
feat: Axios 인스턴스 기본 URL 설정 추가
leeleeleeleejun 0592285
feat: query prefetching용 HydrationBoundaryPage 컴포넌트 추가
leeleeleeleejun 3f07b0b
feat: MSW 브라우저 초기화 시 개발 환경에서만 시작하도록 수정
leeleeleeleejun a1c9dff
feat: MSWProvider에서 개발 환경에서만 MSW 초기화하도록 수정
leeleeleeleejun e4d2df8
feat: BASE_URL에 기본값 추가하여 빈 문자열 처리
leeleeleeleejun d0c23ef
feat: initServerMSW 함수를 비동기로 변경하여 서버 초기화 시 대기하도록 수정
leeleeleeleejun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { | ||
| dehydrate, | ||
| HydrationBoundary, | ||
| QueryClient, | ||
| } from '@tanstack/react-query' | ||
| import { ReactNode } from 'react' | ||
|
|
||
| /** | ||
| * React Query 쿼리 구성 타입 | ||
| * | ||
| * @property queryKey - React Query에서 사용할 쿼리 키 | ||
| * @property queryFn - 데이터를 가져오는 비동기 함수 | ||
| */ | ||
| type QueryConfig = { | ||
| queryKey: string[] | ||
| queryFn: () => Promise<unknown> | ||
| } | ||
|
|
||
| /** | ||
| * 서버 컴포넌트에서 React Query 쿼리를 미리 요청(prefetch)한 뒤, | ||
| * dehydrate 상태를 클라이언트에 전달하기 위한 컴포넌트. | ||
| * | ||
| * @example | ||
| * ```tsx | ||
| * <HydrationBoundaryPage | ||
| * queries={[ | ||
| * { queryKey: ['user'], queryFn: fetchUser }, | ||
| * { queryKey: ['posts'], queryFn: fetchPosts }, | ||
| * ]} | ||
| * > | ||
| * <MyPage /> | ||
| * </HydrationBoundaryPage> | ||
| * ``` | ||
| * | ||
| * @param queries - 사전 요청할 쿼리들의 배열 | ||
| * @param children - HydrationBoundary로 감쌀 React 노드 | ||
| */ | ||
| export const HydrationBoundaryPage = async ({ | ||
| queries, | ||
| children, | ||
| }: { | ||
| queries: QueryConfig[] | ||
| children: ReactNode | ||
| }) => { | ||
| const queryClient = new QueryClient() | ||
|
|
||
| await Promise.all( | ||
| queries.map(({ queryKey, queryFn }) => | ||
| queryClient.prefetchQuery({ queryKey, queryFn }), | ||
| ), | ||
| ) | ||
|
|
||
| return ( | ||
| <HydrationBoundary state={dehydrate(queryClient)}> | ||
| {children} | ||
| </HydrationBoundary> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export const API_PATH = { | ||
| CATEGORY: '/categories', | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import axios from 'axios' | ||
|
|
||
| const axiosInstance = axios.create({ | ||
| baseURL: process.env.NEXT_PUBLIC_API_URL, | ||
| withCredentials: true, | ||
| }) | ||
|
|
||
| export default axiosInstance | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| 'use client' | ||
|
|
||
| import { useEffect, useState } from 'react' | ||
| import { initBrowserMSW } from '@/_mocks/initMSW' | ||
|
|
||
| export function MSWProvider({ children }: { children: React.ReactNode }) { | ||
| const isDev = process.env.NODE_ENV === 'development' | ||
| const [mswReady, setMSWReady] = useState(!isDev) | ||
|
|
||
| useEffect(() => { | ||
| if (!isDev || mswReady) return | ||
| const init = async () => { | ||
| await initBrowserMSW() | ||
| setMSWReady(true) | ||
| } | ||
| init() | ||
| }, [isDev, mswReady]) | ||
|
|
||
| if (!mswReady) return null | ||
|
|
||
| return <>{children}</> | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| export const category = [ | ||
| { id: 1, name: '치킨', iconKey: 'chicken' }, | ||
| { id: 2, name: '햄버거', iconKey: 'burger' }, | ||
| { id: 3, name: '한식', iconKey: 'korean' }, | ||
| { id: 4, name: '분식', iconKey: 'bunsik' }, | ||
| { id: 5, name: '중식', iconKey: 'chinese' }, | ||
| { id: 6, name: '양식', iconKey: 'western' }, | ||
| { id: 7, name: '샐러드', iconKey: 'salad' }, | ||
| { id: 8, name: '일식', iconKey: 'japanese' }, | ||
| { id: 9, name: '카페', iconKey: 'cafe' }, | ||
| { id: 10, name: '피자', iconKey: 'pizza' }, | ||
| { id: 11, name: '멕시칸', iconKey: 'mexican' }, | ||
| { id: 12, name: '아시안', iconKey: 'asian' }, | ||
| { id: 13, name: '찜·탕', iconKey: 'soup' }, | ||
| { id: 14, name: '고기·구이', iconKey: 'meat' }, | ||
| { id: 15, name: '도시락', iconKey: 'lunchbox' }, | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { http, HttpResponse } from 'msw' | ||
| import { category } from '../data/category' | ||
| import { API_PATH } from '@/_constants/path' | ||
|
|
||
| const BASE_URL = process.env.NEXT_PUBLIC_API_URL || '' | ||
|
|
||
| const addBaseUrl = (path: string) => { | ||
| return `${BASE_URL}${path}` | ||
| } | ||
|
|
||
| export const CategoryHandlers = [ | ||
| http.get(addBaseUrl(API_PATH.CATEGORY), () => { | ||
| return HttpResponse.json(category) | ||
| }), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import { CategoryHandlers } from '@/_mocks/handlers/categoryHandlers' | ||
|
|
||
| export const handlers = [...CategoryHandlers] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| export const initServerMSW = async () => { | ||
| // 서버에서만 실행되는 MSW 초기화 | ||
| if (typeof window === 'undefined' && process.env.NODE_ENV === 'development') { | ||
| import('./server').then(({ server }) => { | ||
| server.listen({ | ||
| onUnhandledRequest: 'warn', | ||
| }) | ||
| console.log('✅ MSW 서버 시작됨') | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| export const initBrowserMSW = async () => { | ||
| if (typeof window !== 'undefined' && process.env.NODE_ENV === 'development') { | ||
| const { worker } = await import('./worker') | ||
| await worker.start({ | ||
| onUnhandledRequest: 'bypass', | ||
| }) | ||
| console.log('✅ MSW 브라우저 시작됨') | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { setupServer } from 'msw/node' | ||
| import { handlers } from './handlers' | ||
|
|
||
| export const server = setupServer(...handlers) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { setupWorker } from 'msw/browser' | ||
| import { handlers } from './handlers' | ||
|
|
||
| export const worker = setupWorker(...handlers) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
개발 환경에서 MSW 핸들러 매칭 실패 가능성: baseURL 미설정/상대경로 요청 이슈
현재 baseURL이 환경변수에 전적으로 의존합니다. dev에서 NEXT_PUBLIC_API_URL이 비어 있으면:
해결 방향(택1):
아래 예시는 서버에서 env 미설정 시 에러를 던지고, 브라우저에서는 origin으로 폴백하며 trailing slash를 정규화합니다. 타임아웃도 기본 추가했습니다.
적용 제안 diff:
📝 Committable suggestion
🤖 Prompt for AI Agents