Skip to content

Commit ab6ef17

Browse files
committed
feat: 미들웨어에서 퀴즈 경로에 대한 메타 태그 설정 추가 및 썸네일 이미지 파일 삭제
- '/meme/quiz' 경로에 대한 OG 및 Twitter 메타 태그를 동적으로 수정하는 로직 추가 - 기존의 썸네일 SVG 파일 삭제
1 parent 0083a5a commit ab6ef17

File tree

2 files changed

+49
-55
lines changed

2 files changed

+49
-55
lines changed

apps/web/middleware.ts

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,61 @@
11
export const config = {
2-
matcher: ['/meme/:path*'],
2+
matcher: ['/meme/:path*', '/meme/quiz'],
33
};
44

55
export default async function middleware(request: Request) {
66
const url = new URL(request.url);
77
const pathSegments = url.pathname.split('/');
8-
const memeId = pathSegments[pathSegments.length - 1];
8+
const path = pathSegments[2];
99

1010
// 기본 HTML을 가져옵니다
1111
const res = await fetch(new URL('/', request.url));
1212
const html = await res.text();
1313

14-
// /meme/{id} 형식이 아닌 경우 기본 OG 태그 설정
15-
if (
16-
pathSegments[1] === 'meme' &&
17-
(pathSegments[2] === 'quiz' || !pathSegments[2])
18-
) {
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="https://meme-wiki.net/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="https://meme-wiki.net/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+
}
56+
57+
// /meme/{id} 경로인 경우 API 호출하여 OG 태그 설정
58+
if (path && path !== 'quiz') {
1959
const modifiedHtml = html
2060
.replace(
2161
/<meta\s+property="og:title"\s+content="[^"]*"[^>]*>/,
@@ -60,7 +100,7 @@ export default async function middleware(request: Request) {
60100
// /meme/{id} 경로에 대한 처리
61101
try {
62102
const response = await fetch(
63-
`https://api.meme-wiki.net/api/memes/${memeId}`,
103+
`https://api.meme-wiki.net/api/memes/${path}`,
64104
{
65105
headers: {
66106
Accept: 'application/json',
@@ -99,7 +139,7 @@ export default async function middleware(request: Request) {
99139
)
100140
.replace(
101141
/<meta\s+property="og:url"\s+content="[^"]*"[^>]*>/,
102-
`<meta property="og:url" content="https://meme-wiki.net/meme/${memeId}" />`,
142+
`<meta property="og:url" content="https://meme-wiki.net/meme/${path}" />`,
103143
)
104144
.replace(
105145
/<meta\s+property="twitter:title"\s+content="[^"]*"[^>]*>/,

apps/web/public/thumbnail.svg

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)