Skip to content

Commit 2827add

Browse files
authored
[25.02.28 / TASK-116,TASK-103] Modify - 캐시 충돌 문제 해결 (#18)
* modify: 캐시 충돌 관련 이슈 해결 * modify: 로그아웃 시에도 캐시 잔존 오류 해결 * modify: 사소한 휴먼에러 해결 * refactor: 필요없는 주석 제거
1 parent 5d52b3b commit 2827add

File tree

6 files changed

+27
-18
lines changed

6 files changed

+27
-18
lines changed

src/apis/instance.request.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,9 @@ export const instance = async <I, R>(
7575
return (data.body as unknown as SuccessType<R>).data;
7676
} catch (err: unknown) {
7777
const context = err as Response;
78-
if (location && !context.ok && context.status === 403) {
78+
if (location && !context.ok && context.status === 401) {
7979
window.location.replace('/');
8080
}
81-
//context.status === 401 ||
8281
setContext('Request', {
8382
path: context.url,
8483
status: context.status,

src/app/(with-tracker)/(login)/Content.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { useRouter } from 'next/navigation';
44
import { useForm } from 'react-hook-form';
55
import Image from 'next/image';
6-
import { useMutation, useQueryClient } from '@tanstack/react-query';
6+
import { useMutation } from '@tanstack/react-query';
77
import { Input, Button } from '@/components';
88
import { LoginVo } from '@/types';
99
import { login, sampleLogin } from '@/apis';
@@ -14,7 +14,6 @@ const responsiveStyle =
1414

1515
export const Content = () => {
1616
const { replace } = useRouter();
17-
const client = useQueryClient();
1817

1918
const {
2019
register,
@@ -23,7 +22,6 @@ export const Content = () => {
2322
} = useForm<LoginVo>({ mode: 'all' });
2423

2524
const onSuccess = () => {
26-
client.clear();
2725
trackUserEvent(MessageEnum.LOGIN);
2826
replace('/main?asc=false&sort=');
2927
};

src/components/auth-required/header/index.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { NameType } from '@/components';
99
import { useResponsive } from '@/hooks';
1010
import { logout, me } from '@/apis';
1111
import { trackUserEvent, MessageEnum } from '@/utils/trackUtil';
12+
import { revalidate } from '@/utils/revalidateUtil';
13+
1214
import { defaultStyle, Section, textStyle } from './Section';
1315

1416
const PARAMS = {
@@ -37,13 +39,19 @@ export const Header = () => {
3739

3840
const { mutate: out } = useMutation({
3941
mutationFn: logout,
40-
onMutate: () => router.replace('/'),
41-
onSuccess: () => client.removeQueries(),
42+
onSuccess: async () => {
43+
await revalidate();
44+
client.clear();
45+
router.replace('/');
46+
},
4247
});
4348

4449
const { data: profiles } = useQuery({
4550
queryKey: [PATHS.ME],
4651
queryFn: me,
52+
enabled: !!client.getQueryData([PATHS.ME]),
53+
// 로그아웃 후 리렌더링되어 다시 fetch되는 경우 해결
54+
// 어차피 prefetch를 통해 데이터를 불러온 상태에서 렌더하기 때문에, 캐시 여부만 판단하면 됨
4755
});
4856

4957
useEffect(() => {

src/components/auth-required/main/Section/index.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
'use client';
22

3-
import { useQueryClient } from '@tanstack/react-query';
43
import { useState } from 'react';
5-
import { UserNameNotFoundError } from '@/errors';
64
import { trackUserEvent, MessageEnum } from '@/utils/trackUtil';
75
import { parseNumber } from '@/utils/numberUtil';
86
import { COLORS, env, PATHS } from '@/constants';
97
import { PostType, UserDto } from '@/types';
108
import { Icon } from '@/components';
9+
import { getQueryClient } from '@/utils/queryUtil';
1110
import { Graph } from './Graph';
1211

1312
export const Section = (p: PostType) => {
1413
const [open, setOpen] = useState(false);
15-
const client = useQueryClient();
1614

17-
const { username } = client.getQueryData([PATHS.ME]) as UserDto;
18-
const URL = env.VELOG_URL;
15+
const username = (
16+
getQueryClient().getQueryData([PATHS.ME]) as Partial<UserDto>
17+
)?.username;
1918

20-
if (!username) {
21-
throw new UserNameNotFoundError();
22-
}
23-
24-
const url = `${URL}/@${username}/${p.slug}`;
19+
const url = `${env.VELOG_URL}/@${username}/${p.slug}`;
2520

2621
return (
2722
<section className="flex flex-col w-full h-fit relative">

src/utils/queryUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { toast } from 'react-toastify';
33

44
let localQueryClient: QueryClient | undefined;
55
const STALE_TIME = 1000 * 60 * 3;
6-
const GC_TIME = 1000;
6+
const GC_TIME = 1000 * 60 * 20;
77

88
const createQueryClient = () =>
99
new QueryClient({

src/utils/revalidateUtil.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use server';
2+
3+
import { revalidatePath } from 'next/cache';
4+
import { redirect } from 'next/navigation';
5+
6+
export async function revalidate() {
7+
revalidatePath('/', 'layout');
8+
redirect('/');
9+
}

0 commit comments

Comments
 (0)