Skip to content
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

feat: msw 기반으로 next.js에서 mock service worker 추가 #33

Merged
merged 6 commits into from
Feb 8, 2023

Conversation

alstn2468
Copy link
Member

@alstn2468 alstn2468 commented Feb 8, 2023

진행한 것

  • msw 이용해서 백엔드 API 모킹 진행
  • 공통으로 사용할 수 있도록 packages 폴더로 이동 시도

문제점

첫 렌더링시 서비스 워커 등록이 되지 않아 네트워크 오류가 발생하는 문제가 있음
그것을 useEffectshouldRender 상태를 이용해 해결함
하지만 렌더링 자체를 막음으로써 msw기반의 상태에서 Suspense를 이용하기 어려워보임 (초기 렌더링의 경우에 해당할 듯)

  • Before
if (process.env.NEXT_PUBLIC_API_MOCKING === 'enabled') {
  require('@weekly/msw');
}

function App() {
// ...
}
  • After
function App(props: AppProps) {
  const { Component, pageProps } = props;
  const [shouldRender, setShouldRender] = useState(
    !process.env.NEXT_PUBLIC_API_MOCKING,
  );
  useEffect(() => {
    async function init() {
      const { initMocks } = await import('@weekly/msw');
      await initMocks();
      setShouldRender(true);
    }
    if (process.env.NEXT_PUBLIC_API_MOCKING === 'enabled') {
      init();
    }
  }, []);
  if (!shouldRender) {
    return null;
  }
  // ...

연결 이슈

top-level-await관련 babel 설정 넣어서 initMocks를 기다리게 하는 방법이 있는 것 같았지만 axios 관련 버그가 또 터지네.
서버 개발 전에 잠깐 쓰는거니 그냥 그대로 쓰는 것도 좋을듯~!

@alstn2468 alstn2468 added the enhancement New feature or request label Feb 8, 2023
@alstn2468 alstn2468 self-assigned this Feb 8, 2023
Copy link
Member

@zi-gae zi-gae left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

목서버 보다 api 가 먼저 나올 것 같은건 기분탓일까..

@zi-gae
Copy link
Member

zi-gae commented Feb 8, 2023

top-level-await관련 babel 설정 넣어서 initMocks를 기다리게 하는 방법이 있는 것 같았지만 axios 관련 버그가 또 터지네.
서버 개발 전에 잠깐 쓰는거니 그냥 그대로 쓰는 것도 좋을듯~!

axios 걷어내~ fetch 면 충분하지

@alstn2468
Copy link
Member Author

@zi-gae 인증관련 인터셉터 쓸일 있을 것 같아서 axios 걷는건 반려!
API가 더 먼저 나올거 같기도해 ㅋㅋㅋㅋㅋ

@alstn2468 alstn2468 merged commit e535819 into main Feb 8, 2023
@alstn2468 alstn2468 deleted the feat/setup-mock-service-worker branch February 8, 2023 14:33
@9yujin
Copy link
Member

9yujin commented Feb 8, 2023

처음 써보는데 열씨미 공부해보겠습니다 헤헤

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants