Skip to content

Commit

Permalink
refactor: 무한스크롤 개선, 태그 검색 결과 페이지 prefetch 제거 (#73)
Browse files Browse the repository at this point in the history
* refactor: infinite scroll
- remove Masonry component and useIntersect hook (It had a lot of bug)
- add @egjs-infinitegrid
- remove the feature of polling meme data from the cache

* refactor: 사용하지 않는 컬러 제거

* refactor: 변수명 변경

* fix: 태그 검색 결과 페이지 서버에서 데이터 prefetch 제거
- waterfall 로 인한 지연 시간을 제거 하기 위함
- TagBookmarkButton 컴포넌트 Suspense 로 wrapping

* refactor: 변수명 변경

* feat: error boundary fallback 에 홈으로 돌아가기 버튼 추가

* feat: PRD에서만 ga, gtag 활성화

* chore: 주석 추가
  • Loading branch information
ojj1123 authored Jan 19, 2024
1 parent 3bc57ac commit 14bd998
Show file tree
Hide file tree
Showing 43 changed files with 484 additions and 1,031 deletions.
751 changes: 264 additions & 487 deletions .pnp.cjs

Large diffs are not rendered by default.

101 changes: 47 additions & 54 deletions .pnp.loader.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 0 additions & 2 deletions mocks/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import * as account from "./account";
import * as auth from "./auth";
import * as collection from "./collection";
import * as meme from "./meme";
import * as post from "./post";
import * as search from "./search";
import * as tags from "./tags";

export const handlers = [
...Object.values(auth),
...Object.values(post),
...Object.values(tags),
...Object.values(meme),
...Object.values(search),
Expand Down
27 changes: 0 additions & 27 deletions mocks/handlers/post/index.ts

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"postinstall": "husky install"
},
"dependencies": {
"@egjs/react-infinitegrid": "^4.11.0",
"@emotion/css": "^11.10.5",
"@emotion/react": "^11.10.5",
"@emotion/serialize": "^1.1.1",
Expand Down
1 change: 0 additions & 1 deletion src/api/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from "./axios";
export * from "./query-client";
export * from "./queryKey";
export * from "./useCoreInfiniteQuery";
export * from "./useSuspensedQuery";
1 change: 1 addition & 0 deletions src/api/core/query-client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const QueryClientProvider = ({ hydrateState, children }: Props) => {
suspense: true,
retry: 0,
refetchOnWindowFocus: false,
// FIXME this must be set to true
useErrorBoundary: false,
staleTime: 1000 * 20, // 20초
},
Expand Down
3 changes: 0 additions & 3 deletions src/api/core/queryKey.ts

This file was deleted.

6 changes: 4 additions & 2 deletions src/api/core/useCoreInfiniteQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function useCoreInfiniteQuery<
isFetching,
isFetchingNextPage,
isError,
fetchNextPage: oldFetchNextPage,
fetchNextPage: originalFetchNextPage,
...rest
} = useInfiniteQuery(queryKey, queryFn, {
getNextPageParam: (lastPage, allPages) => {
Expand All @@ -56,7 +56,9 @@ export function useCoreInfiniteQuery<
},
...options,
});
const fetchNextPage = isError ? ((() => {}) as typeof oldFetchNextPage) : oldFetchNextPage;
const fetchNextPage = isError
? ((() => {}) as typeof originalFetchNextPage)
: originalFetchNextPage;

const flatData = data ? data.pages.flatMap(({ data }) => data) : [];
const isEmpty = data?.pages[0].data.length === 0;
Expand Down
4 changes: 2 additions & 2 deletions src/api/meme/useGetMemesByCollectionId.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { QueryFunctionContext } from "@tanstack/react-query";

import { api, CORE_QUERY_KEY, useCoreInfiniteQuery } from "../core";
import { api, useCoreInfiniteQuery } from "../core";

const PAGE_SIZE = 10;

Expand Down Expand Up @@ -35,4 +35,4 @@ export const useGetMemesByCollectionId = (collectionId: number) => {
};

useGetMemesByCollectionId.queryKey = (collectionId: number | null) =>
[CORE_QUERY_KEY.infiniteMemeList, "getMemesByCollectionId", collectionId] as const;
["getMemesByCollectionId", collectionId] as const;
5 changes: 2 additions & 3 deletions src/api/meme/useGetMemesBySort.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { QueryFunctionContext } from "@tanstack/react-query";

import { api, CORE_QUERY_KEY, useCoreInfiniteQuery } from "../core";
import { api, useCoreInfiniteQuery } from "../core";

const PAGE_SIZE = 10;

Expand Down Expand Up @@ -28,5 +28,4 @@ export const useGetMemesBySort = (sort: keyof typeof types) => {

return { data, isEmpty, isFetchingNextPage, fetchNextPage };
};
useGetMemesBySort.queryKey = (sort: string) =>
[CORE_QUERY_KEY.infiniteMemeList, "getMemesBySort", sort] as const;
useGetMemesBySort.queryKey = (sort: string) => ["getMemesBySort", sort] as const;
5 changes: 2 additions & 3 deletions src/api/search/useGetMemesByKeyword.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { QueryFunctionContext } from "@tanstack/react-query";

import { api, CORE_QUERY_KEY, useCoreInfiniteQuery } from "../core";
import { api, useCoreInfiniteQuery } from "../core";

const PAGE_SIZE = 20;

Expand Down Expand Up @@ -31,5 +31,4 @@ export const useGetMemesByKeyword = (keyword: string) => {
return { data, isEmpty, isFetchingNextPage, fetchNextPage };
};

useGetMemesByKeyword.queryKey = (keyword: string) =>
[CORE_QUERY_KEY.infiniteMemeList, "getMemesByKeyword", keyword] as const;
useGetMemesByKeyword.queryKey = (keyword: string) => ["getMemesByKeyword", keyword] as const;
15 changes: 11 additions & 4 deletions src/api/search/useGetMemesByTag.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { QueryClient, QueryFunctionContext } from "@tanstack/react-query";
import { useInfiniteQuery } from "@tanstack/react-query";

import { api, CORE_QUERY_KEY } from "../core";
import { api } from "../core";

const PAGE_SIZE = 20;

Expand All @@ -13,7 +13,12 @@ const PAGE_SIZE = 20;
* isEmpty - 밈 검색 결과가 없는 경우 true
*/
export const useGetMemesByTag = (tag: string) => {
const { data, isFetchingNextPage, fetchNextPage } = useInfiniteQuery(
const {
data,
isFetchingNextPage,
isError,
fetchNextPage: originalFetchNextPage,
} = useInfiniteQuery(
useGetMemesByTag.queryKey(tag),
({ pageParam = 0 }: QueryFunctionContext) =>
api.search.getMemesByTag({ keyword: tag, offset: pageParam, limit: PAGE_SIZE }),
Expand All @@ -28,15 +33,17 @@ export const useGetMemesByTag = (tag: string) => {
},
);

const fetchNextPage = isError
? ((() => {}) as typeof originalFetchNextPage)
: originalFetchNextPage;
const memes = data?.pages.flatMap(({ memes }) => memes) || [];
const totalCount = data?.pages[0].totalCount;
const isEmpty = !memes.length;

return { data: memes, totalCount, isEmpty, isFetchingNextPage, fetchNextPage };
};

useGetMemesByTag.queryKey = (tag: string) =>
[CORE_QUERY_KEY.infiniteMemeList, "getMemesByTag", tag] as const;
useGetMemesByTag.queryKey = (tag: string) => ["getMemesByTag", tag] as const;

useGetMemesByTag.fetchInfiniteQuery = (tag: string, queryClient: QueryClient) =>
queryClient.fetchInfiniteQuery(useGetMemesByTag.queryKey(tag), ({ pageParam = 0 }) =>
Expand Down
9 changes: 2 additions & 7 deletions src/api/search/useGetMemesFromCollectionByKeyword.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { QueryFunctionContext } from "@tanstack/react-query";

import { api, CORE_QUERY_KEY, useCoreInfiniteQuery } from "../core";
import { api, useCoreInfiniteQuery } from "../core";

const PAGE_SIZE = 20;

Expand Down Expand Up @@ -45,9 +45,4 @@ useGetMemesFromCollectionByKeyword.queryKey = ({
}: {
keyword: string;
collectionId: number;
}) =>
[
CORE_QUERY_KEY.infiniteMemeList,
"getMemesFromCollectionByKeyword",
{ keyword, collectionId },
] as const;
}) => ["getMemesFromCollectionByKeyword", { keyword, collectionId }] as const;
5 changes: 2 additions & 3 deletions src/api/tag/useGetTagInfo.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type { QueryClient, UseQueryOptions } from "@tanstack/react-query";
import { useQuery } from "@tanstack/react-query";

import { api } from "../core";
import { api, useSuspendedQuery } from "../core";

export const useGetTagInfo = (
tagId: number,
options: Pick<UseQueryOptions, "enabled"> = { enabled: true },
) => {
return useQuery({
return useSuspendedQuery({
queryKey: useGetTagInfo.queryKey(tagId),
queryFn: () => useGetTagInfo.queryFn(tagId),
staleTime: 0,
Expand Down
5 changes: 5 additions & 0 deletions src/common/components/Error/GlobalError.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Link from "next/link";

import { Navigation } from "../Navigation";

export const GlobalError = () => {
Expand All @@ -9,6 +11,9 @@ export const GlobalError = () => {
<span className="mt-16 text-16-semibold-140 text-gray-600">
이 페이지를 새로고침 해보세요.
</span>
<Link className="mt-16 text-16-semibold-140 text-primary-800" href="/">
홈으로 돌아가기
</Link>
</div>
</>
);
Expand Down
Loading

0 comments on commit 14bd998

Please sign in to comment.