|
1 | 1 | export const config = { |
2 | | - matcher: ['/meme/:path*'], |
| 2 | + matcher: ['/meme/:path*', '/meme/quiz'], |
3 | 3 | }; |
4 | 4 |
|
5 | 5 | export default async function middleware(request: Request) { |
6 | 6 | const url = new URL(request.url); |
7 | 7 | const pathSegments = url.pathname.split('/'); |
8 | | - const memeId = pathSegments[pathSegments.length - 1]; |
| 8 | + const path = pathSegments[2]; |
9 | 9 |
|
10 | 10 | // 기본 HTML을 가져옵니다 |
11 | 11 | const res = await fetch(new URL('/', request.url)); |
12 | 12 | const html = await res.text(); |
13 | 13 |
|
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') { |
19 | 59 | const modifiedHtml = html |
20 | 60 | .replace( |
21 | 61 | /<meta\s+property="og:title"\s+content="[^"]*"[^>]*>/, |
@@ -60,7 +100,7 @@ export default async function middleware(request: Request) { |
60 | 100 | // /meme/{id} 경로에 대한 처리 |
61 | 101 | try { |
62 | 102 | const response = await fetch( |
63 | | - `https://api.meme-wiki.net/api/memes/${memeId}`, |
| 103 | + `https://api.meme-wiki.net/api/memes/${path}`, |
64 | 104 | { |
65 | 105 | headers: { |
66 | 106 | Accept: 'application/json', |
@@ -99,7 +139,7 @@ export default async function middleware(request: Request) { |
99 | 139 | ) |
100 | 140 | .replace( |
101 | 141 | /<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}" />`, |
103 | 143 | ) |
104 | 144 | .replace( |
105 | 145 | /<meta\s+property="twitter:title"\s+content="[^"]*"[^>]*>/, |
|
0 commit comments