-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
35 lines (30 loc) · 1.06 KB
/
route.ts
File metadata and controls
35 lines (30 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { NextResponse } from 'next/server';
import dbConnect from '@/app/lib/dbConnect';
import Post from '@/app/models/Post';
export const dynamic = 'force-dynamic'; // 캐싱 방지
export async function GET() {
try {
await dbConnect();
const pageUrl =
process.env.NEXT_PUBLIC_DEPLOYMENT_URL ||
process.env.NEXTAUTH_URL ||
'https://shipfriend.dev';
// 최신 글 1개 가져오기
const latestPost = await Post.findOne({}).sort({ date: -1 }).select('slug');
if (!latestPost) {
// 블로그 홈페이지로 리다이렉션 (글이 없는 경우)
return NextResponse.redirect(new URL(`${pageUrl}/posts`));
}
// 최신 글로 리다이렉션
return NextResponse.redirect(
new URL(`${pageUrl}/posts/${latestPost.slug}`)
);
} catch (error) {
console.error('Error redirecting to latest post:', error);
const pageUrl =
process.env.NEXT_PUBLIC_DEPLOYMENT_URL ||
process.env.NEXTAUTH_URL ||
'https://shipfriend.dev';
return NextResponse.redirect(new URL(`${pageUrl}/posts`));
}
}