|
1 | 1 | export const config = { |
2 | | - matcher: ['/meme/:memeId((?!quiz)[^/]+)'], |
| 2 | + matcher: ['/meme/:path*'], |
3 | 3 | }; |
4 | 4 |
|
5 | 5 | export default async function middleware(request: Request) { |
6 | 6 | const url = new URL(request.url); |
7 | | - const memeId = url.pathname.split('/').pop(); |
| 7 | + const pathSegments = url.pathname.split('/'); |
| 8 | + const memeId = pathSegments[pathSegments.length - 1]; |
8 | 9 |
|
| 10 | + // 기본 HTML을 가져옵니다 |
| 11 | + const res = await fetch(new URL('/', request.url)); |
| 12 | + const html = await res.text(); |
| 13 | + |
| 14 | + // /meme/{id} 형식이 아닌 경우 기본 OG 태그 설정 |
| 15 | + if ( |
| 16 | + pathSegments[1] === 'meme' && |
| 17 | + (pathSegments[2] === 'quiz' || !pathSegments[2]) |
| 18 | + ) { |
| 19 | + const modifiedHtml = html |
| 20 | + .replace( |
| 21 | + /<meta\s+property="og:title"\s+content="[^"]*"[^>]*>/, |
| 22 | + `<meta property="og:title" content="Meme Wiki - 밈 문화의 모든 것" />`, |
| 23 | + ) |
| 24 | + .replace( |
| 25 | + /<meta\s+property="og:description"\s+content="[^"]*"[^>]*>/, |
| 26 | + `<meta property="og:description" content="나만의 밈을 만들고 공유하세요." />`, |
| 27 | + ) |
| 28 | + .replace( |
| 29 | + /<meta\s+property="og:image"\s+content="[^"]*"[^>]*>/, |
| 30 | + `<meta property="og:image" content="https://meme-wiki.net/thumbnail.svg" />`, |
| 31 | + ) |
| 32 | + .replace( |
| 33 | + /<meta\s+property="og:url"\s+content="[^"]*"[^>]*>/, |
| 34 | + `<meta property="og:url" content="${url.href}" />`, |
| 35 | + ) |
| 36 | + .replace( |
| 37 | + /<meta\s+property="twitter:title"\s+content="[^"]*"[^>]*>/, |
| 38 | + `<meta property="twitter:title" content="Meme Wiki - 밈 문화의 모든 것" />`, |
| 39 | + ) |
| 40 | + .replace( |
| 41 | + /<meta\s+property="twitter:description"\s+content="[^"]*"[^>]*>/, |
| 42 | + `<meta property="twitter:description" content="나만의 밈을 만들고 공유하세요." />`, |
| 43 | + ) |
| 44 | + .replace( |
| 45 | + /<meta\s+property="twitter:image"\s+content="[^"]*"[^>]*>/, |
| 46 | + `<meta property="twitter:image" content="https://meme-wiki.net/thumbnail.svg" />`, |
| 47 | + ); |
| 48 | + |
| 49 | + return new Response(modifiedHtml, { |
| 50 | + status: 200, |
| 51 | + headers: { |
| 52 | + 'content-type': 'text/html;charset=UTF-8', |
| 53 | + }, |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + // /meme/{id} 경로에 대한 처리 |
9 | 58 | try { |
10 | 59 | const response = await fetch( |
11 | 60 | `https://api.meme-wiki.net/api/memes/${memeId}`, |
|
0 commit comments