[FE] api 호출 개선 : 이벤트 페이지 진입 시 같은 api 7개씩 호출되는 문제 #924
Merged
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.
issue
문제 상황
이벤트 페이지 접속 시 같은 api 요청이 7~8번씩 발생하는 문제가 있었습니다.
event, steps, reports, all members 요청이 반복적으로 실행되어 초기에 30번 이상 요청을 보내는 모습을 보고 경악을;;;;;;
다행이 원인을 발견하였고, SuspenseQueries의 initialData를 설정해주지 않아서였습니다....
해결 방법
suspenseQueries 옵션으로 initialData를 설정하고 initialDataUpdatedAt: 0을 설정했습니다.
initialDataUpdatedAt: 0을 설정한 이유는 이를 설정하지 않으면 초기 데이터를 화면에 보여주고 api 요청을 보내지 않게 되기 때문입니다.
이렇게 하고 끝났을 때 문제가 하나 생겼습니다. 바로 Suspense의 Fallback 컴포넌트가 보이지 않게 된 것이었습니다.
SuspenseQueries를 적용한 이유가 로딩 화면을 선언적으로 보여주기 위함인데 이렇게 되어버리면 Suspense를 적용하는 의미가 없어진다고 생각했습니다.
2025-01-11.19-43-11.mp4
그래서 생각해낸 대안으로 queries 반환 값인 isFetching을 사용하는 것입니다.
isFetching이 모두 false가 될 때까지 isFetching은 true가 되고 이 값이 true일 때 return null을 주어 데이터 fetching 중에는 하위 컴포넌트가 렌더링 되지 않도록 막았습니다. 이로써 fallback 컴포넌트가 보이면서 initial data가 보이는 현상을 막을 수 있었습니다.
2025-01-11.19-49-21.mp4
아쉬운 점
initial data를 넣어주지 않았을 때 왜 같은 요청이 7번씩 발생하는지 원인을 찾지 못 했어요. 어쩌다 보니 해결이 되었지만 원인을 명확하게 파악 후 해결한 것이 아니라 찜찜합니다..
🫡 참고사항