Skip to content

Commit

Permalink
답변 서버렌더링시에 Limiter제외
Browse files Browse the repository at this point in the history
  • Loading branch information
yunochi committed Jan 13, 2025
1 parent 6921e4b commit b229c27
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
18 changes: 13 additions & 5 deletions src/app/api/_service/answer/answer-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,17 @@ export class AnswerService {

@RateLimit({ bucket_time: 300, req_limit: 600 }, 'ip')
public async GetSingleAnswerApi(_req: NextRequest, answerId: string, userHandle: string) {
const dto = await this.GetSingleAnswerDto(answerId, userHandle);
if (!dto) {
return sendApiError(404, 'Not found', 'NOT_FOUND');
}
return NextResponse.json(dto, {
status: 200,
headers: { 'Content-type': 'application/json', 'Cache-Control': 'public, max-age=60' },
});
}

public async GetSingleAnswerDto(answerId: string, userHandle: string): Promise<AnswerWithProfileDto | undefined> {
const answer = await this.prisma.answer.findUnique({
include: { answeredPerson: { include: { user: { include: { server: { select: { instanceType: true } } } } } } },
where: {
Expand All @@ -363,7 +374,7 @@ export class AnswerService {
},
});
if (!answer) {
return sendApiError(404, 'Not found', 'NOT_FOUND');
return;
}
const profileDto = profileToDto(
answer.answeredPerson,
Expand All @@ -375,10 +386,7 @@ export class AnswerService {
...answerDto,
answeredPerson: profileDto,
};
return NextResponse.json(dto, {
status: 200,
headers: { 'Content-type': 'application/json', 'Cache-Control': 'public, max-age=60' },
});
return dto;
}

public async filterBlock(answers: AnswerWithProfileDto[], myHandle: string) {
Expand Down
13 changes: 5 additions & 8 deletions src/app/main/user/[handle]/[answer]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Metadata } from 'next';
import SingleAnswer from './answer';
import { AnswerWithProfileDto } from '@/app/_dto/answers/Answers.dto';
import { notFound } from 'next/navigation';
import { AnswerService } from '@/app/api/_service/answer/answer-service';

export const dynamic = 'force-dynamic';

Expand Down Expand Up @@ -29,16 +30,12 @@ export async function generateMetadata({
}

async function fetchAnswer(userHandle: string, id: string): Promise<AnswerWithProfileDto | undefined> {
const url = process.env.WEB_URL;
const res = await fetch(`${url}/api/db/answers/${userHandle}/${id}`, {
method: 'GET',
});
if (res.status === 404) {
const answerService = AnswerService.getInstance();
const answer = await answerService.GetSingleAnswerDto(id, userHandle);
if (!answer) {
return undefined;
} else if (!res.ok) {
throw new Error(`Fail to fetch answer! ${await res.text()}`);
}
return await res.json();
return answer;
}

export default async function singleAnswerWrapper({ params }: { params: Promise<{ handle: string; answer: string }> }) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/main/user/[handle]/_answers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async function fetchProfile(handle: string) {
onApiError(profile.status, profile);
return undefined;
}
} catch (err) {
} catch {
return undefined;
}
}
Expand Down

0 comments on commit b229c27

Please sign in to comment.