서버 컴포넌트에서 server action 사용 질문이 있습니다.
서버 컴포넌트에서 server actions 사용 중 네트워크 탭에서 응답이 안담기는 문제가 있어서 질문 드립니다.
강의에 나와있는대로 코드를 따라했는데 뭐가 문제인지 잘 모르겠습니다.
아래는 signupModal의 코드입니다.
import style from "./signup.module.css";
import onSubmit from "../_lib/signup";
import BackButton from "@/app/(beforelogin)/_component/BackButton";
import { useFormState, useFormStatus } from "react-dom";
import { redirect } from "next/navigation";
function showMessage(messasge: string | null | undefined) {
if (messasge === "no_id") {
return "아이디를 입력하세요.";
}
if (messasge === "no_name") {
return "닉네임을 입력하세요.";
}
if (messasge === "no_password") {
return "비밀번호를 입력하세요.";
}
if (messasge === "no_image") {
return "이미지를 업로드하세요.";
}
if (messasge === "user_exists") {
return "이미 사용 중인 아이디입니다.";
}
return "";
}
export default function SignupModal() {
//const [state, formAction] = useFormState(onSubmit, { message: null });
//const { pending } = useFormStatus();
const formAction = async (formData: any) => {
"use server";
let shouldRedirect = false;
try {
const response = await fetch(
`${process.env.NEXT_PUBLIC_BASE_URL}/api/users`,
{
method: "post",
body: formData,
credentials: "include",
}
);
console.log(response.status);
console.log(await response.json(), "abc");
if (response.status === 403) {
console.log("???");
return { message: "user_exists" };
}
shouldRedirect = true;
} catch (err) {
console.log(err);
}
if (shouldRedirect) {
redirect("/home"); // try/catch문 안에서 X
}
};
return (
<>
<div className={style.modalBackground}>
<div className={style.modal}>
<div className={style.modalHeader}>
<BackButton />
<div>계정을 생성하세요.</div>
</div>
<form action={formAction}>
<div className={style.modalBody}>
<div className={style.inputDiv}>
<label className={style.inputLabel} htmlFor="id">
아이디
</label>
<input
id="id"
name="id"
className={style.input}
type="text"
placeholder=""
required
/>
</div>
<div className={style.inputDiv}>
<label className={style.inputLabel} htmlFor="name">
닉네임
</label>
<input
id="name"
name="name"
className={style.input}
type="text"
placeholder=""
required
/>
</div>
<div className={style.inputDiv}>
<label className={style.inputLabel} htmlFor="password">
비밀번호
</label>
<input
id="password"
name="password"
className={style.input}
type="password"
placeholder=""
required
/>
</div>
<div className={style.inputDiv}>
<label className={style.inputLabel} htmlFor="image">
프로필
</label>
<input
id="image"
name="image"
required
className={style.input}
type="file"
accept="image/*"
/>
</div>
</div>
<div className={style.modalFooter}>
<button type="submit" className={style.actionButton}>
가입하기
</button>
</div>
</form>
</div>
</div>
</>
);
}
아래는 핸들러 세팅입니다.
http.post("/api/users", async ({ request }) => {
console.log("회원가입");
return HttpResponse.text(JSON.stringify("user_exists"), {
status: 403,
});
// return HttpResponse.text(JSON.stringify("ok"), {
// headers: {
// "Set-Cookie": "connect.sid=msw-cookie;HttpOnly;Path=/;Max-Age=0",
// },
// });
}),여기서 회원가입을 누를시에 
console.log() 처리한 부분은 잘 찍히고
서버쪽에서 찍은 회원가입 콘솔도 잘 찍히는 모습입니다.
네트워크탭에서 페이로드는 잘 담겼는데, 응답이 없습니다.
회원가입 누를 때 콘솔이 찍히는 것으로 보아서 포트도 9090으로 제대로 열려있고, 403응답이 오는 것으로 보아 핸들러 쪽은 제대로 작동하는 것 같습니다.
그리고 서버액션 쪽 콘솔 "???"가 찍히는 것으로 보아서 status도 403으로 잘 오는 것 같은데 리턴 메시지가 제대로 안되는 것인지 응답이 왜 없는 것인지 궁금합니다.
답변 1
0
서버액션은 원래 응답이 안 뜹니다. msw 서버에 대한 응답은 넥스트 서버가 대신 받았습니다.
0
제로초님 답변 감사합니다.
또 궁금한게 있습니다.
강의에서는 응답 messge가 있는데, 이것은 무슨 차이인가요 ??
그리고, 저 또한 응답에 message가 담겼다가 안담겼다가 하는데 담겼을 때 안담겼을 때 차이를 모르겠습니다.
응답에 message가 담겼다가, 바로 똑같은 요청을 보내니 응답이 없게 날라옵니다.
(동작은 동일하게 되어 문제는 없습니다. 그냥 차이가 무엇인지 궁금해서 여쭈어봅니다.)
넥스트에서 응답이 캐싱이 되는 것(?)
0
응답이 담겨있고 안 담겨있고는 전부 Next가 자체적으로 처리하기 위해서 편의상 넣거나 안 넣는 것입니다. 서버액션은 넥스트가 전적으로 컨트롤하는 것이라서요. 예를 들어 어디로 리다이렉트하라, 에러메시지가 이것이다 등등의 정보가 담겨있을 수 있는데 저희가 접근할 수는 없습니다.
캡처링부분 질문있습니다.
0
74
2
깃에 소스코드를 찾을 수 없습니다.
0
113
2
useInfiniteQuery promise와 react use 사용시 페이지 무한 로딩
0
98
1
import 파일 경로를 찾지 못 해서 에러가 발생합니다.
0
109
2
css 라이브러리 추천 부탁드립니다
0
140
2
팔로우 추천 목록이 빈 배열로 들어옵니다.
0
130
1
게시물 업로드 시 userId가 undefined로 들어가는 오류
0
119
1
useSuspenseQuery 사용 시 SSR 401 이슈 관련
0
172
1
리액트 쿼리 useinfinitequery 무한스크롤 구현 시 페이지가 이동할 경우 데이터가 보존되게 할 수 있나요??
0
184
3
폴링이 필요없는 이유
0
93
2
next Request Memoization과 react cache
0
108
2
seo 최적화 기준은 데이터 fetching인가요 아님 데이터 렌더링인가요?
0
84
2
next.js 서버fetch 에러 fallback ui 구현 방법
0
173
2
프레임워크 여론 파악법
0
125
2
필터옵션이 많은 페이지에서 서버 fetch를 하는게 맞는걸까요??
0
103
2
서버 fetch suspense 로 감싸고 새로고침 시 잠시 빈 화면이 노출된 후 fallback ui가 노출됩니다.
0
102
2
template.tsx 내 서버fetch 응답값과 클라이언트 컴포넌트 상태값 싱크가 맞지 않는 이슈
0
66
2
Auth.js 사용 시 authorize 함수가 호출되지 않습니다
0
132
2
next.js 에서 로그인에 관하여
0
138
1
Next의 route handler에 대한 질문이 있습니다.
0
101
2
게시판 리스트 만들때 use client를 어디서부터 집어넣어야할지 모르겠습니다
0
98
2
프라이빗 폴더를 해야 하는 이유가 모호한 것 같아요.
0
85
2
vanilla-extract 못찾는 문제
0
230
2
fetch 캐싱과 revalidate 관련
0
84
2





