Skip to content

Commit 9c2d58d

Browse files
committed
feat: 미들웨어에서 메타 태그 동적 수정 기능 추가
- '/meme/:path*' 경로에 대한 처리 로직을 추가하여 기본 HTML을 가져오고, 특정 조건에 따라 OG 및 Twitter 메타 태그를 동적으로 수정 - '/meme/{id}' 형식이 아닌 경우 기본 OG 태그 설정을 통해 사용자 경험 향상
1 parent 5cae30a commit 9c2d58d

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

apps/web/middleware.ts

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,60 @@
11
export const config = {
2-
matcher: ['/meme/:memeId((?!quiz)[^/]+)'],
2+
matcher: ['/meme/:path*'],
33
};
44

55
export default async function middleware(request: Request) {
66
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];
89

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} 경로에 대한 처리
958
try {
1059
const response = await fetch(
1160
`https://api.meme-wiki.net/api/memes/${memeId}`,

0 commit comments

Comments
 (0)