Skip to content

Commit ab626f2

Browse files
committed
feat: 미들웨어에서 퀴즈 경로 처리 로직 간소화 및 메타 태그 수정
- '/meme/quiz' 경로에 대한 OG 태그 설정을 기본으로 변경하여 처리 간소화 - '/meme/:memeId' 경로에 대한 API 호출 시 ID 추출 방식 수정
1 parent 917024b commit ab626f2

File tree

1 file changed

+4
-52
lines changed

1 file changed

+4
-52
lines changed

apps/web/middleware.ts

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,15 @@
11
export const config = {
2-
matcher: ['/meme/quiz', '/meme/:memeId'],
2+
matcher: ['/meme/:memeId'],
33
};
44

55
export default async function middleware(request: Request) {
66
const url = new URL(request.url);
7-
const pathSegments = url.pathname.split('/');
8-
const path = pathSegments[2];
9-
10-
// 기본 HTML을 가져옵니다
11-
const res = await fetch(new URL('/', request.url));
12-
const html = await res.text();
13-
14-
// /meme/quiz 경로인 경우 퀴즈 페이지용 OG 태그 설정
15-
if (path === 'quiz') {
16-
const modifiedHtml = html
17-
.replace(
18-
/<meta\s+property="og:title"\s+content="[^"]*"[^>]*>/,
19-
`<meta property="og:title" content="Meme Wiki - 밈 퀴즈" />`,
20-
)
21-
.replace(
22-
/<meta\s+property="og:description"\s+content="[^"]*"[^>]*>/,
23-
`<meta property="og:description" content="재미있는 밈 퀴즈를 풀어보세요!" />`,
24-
)
25-
.replace(
26-
/<meta\s+property="og:image"\s+content="[^"]*"[^>]*>/,
27-
`<meta property="og:image" content="${url.origin}/thumbnail.png" />`,
28-
)
29-
.replace(
30-
/<meta\s+property="og:url"\s+content="[^"]*"[^>]*>/,
31-
`<meta property="og:url" content="${url.href}" />`,
32-
)
33-
.replace(
34-
/<meta\s+property="twitter:title"\s+content="[^"]*"[^>]*>/,
35-
`<meta property="twitter:title" content="Meme Wiki - 밈 퀴즈" />`,
36-
)
37-
.replace(
38-
/<meta\s+property="twitter:description"\s+content="[^"]*"[^>]*>/,
39-
`<meta property="twitter:description" content="재미있는 밈 퀴즈를 풀어보세요!" />`,
40-
)
41-
.replace(
42-
/<meta\s+property="twitter:image"\s+content="[^"]*"[^>]*>/,
43-
`<meta property="twitter:image" content="${url.origin}/thumbnail.png" />`,
44-
);
45-
46-
return new Response(modifiedHtml, {
47-
status: 200,
48-
headers: {
49-
'content-type': 'text/html;charset=UTF-8',
50-
'cache-control': 'no-cache, no-store, must-revalidate',
51-
pragma: 'no-cache',
52-
expires: '0',
53-
},
54-
});
55-
}
7+
const memeId = url.pathname.split('/').pop();
568

579
// /meme/{id} 경로에 대한 처리
5810
try {
5911
const response = await fetch(
60-
`https://api.meme-wiki.net/api/memes/${path}`,
12+
`https://api.meme-wiki.net/api/memes/${memeId}`,
6113
{
6214
headers: {
6315
Accept: 'application/json',
@@ -96,7 +48,7 @@ export default async function middleware(request: Request) {
9648
)
9749
.replace(
9850
/<meta\s+property="og:url"\s+content="[^"]*"[^>]*>/,
99-
`<meta property="og:url" content="https://meme-wiki.net/meme/${path}" />`,
51+
`<meta property="og:url" content="${url.origin}/meme/${memeId}" />`,
10052
)
10153
.replace(
10254
/<meta\s+property="twitter:title"\s+content="[^"]*"[^>]*>/,

0 commit comments

Comments
 (0)