Skip to content

Commit

Permalink
Og: クエリパラメータにタイトルを渡すときはエスケープする (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
2RiniaR authored Sep 28, 2024
1 parent 426284d commit 25a31df
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
7 changes: 6 additions & 1 deletion app/api/og/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const font = await fs.readFile(path.join(process.cwd(), "assets", "mplus-2c-medium.ttf"));
return new ImageResponse(
<OgImage title={searchParams.get("title") ?? ""} thumbnail={searchParams.get("thumbnail") ?? undefined} />,
(
<OgImage
title={decodeURIComponent(searchParams.get("title") ?? "")}
thumbnail={searchParams.get("thumbnail") ?? undefined}
/>
),
{
fonts: [
{
Expand Down
2 changes: 1 addition & 1 deletion app/pages/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const generateMetadata = async ({ params }: Props) => {
siteName: siteName,
type: "article",
images: {
url: `${siteUrl}/api/og?title=${data.title}${data.thumbnail?.url !== undefined ? `&thumbnail=${data.thumbnail.url}` : ""}`,
url: `${siteUrl}/api/og?title=${encodeURIComponent(data.title)}${data.thumbnail?.url !== undefined ? `&thumbnail=${data.thumbnail.url}` : ""}`,
width: 1200,
height: 630
}
Expand Down
2 changes: 1 addition & 1 deletion app/preview/pages/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const generateMetadata = async ({ searchParams }: Props) => {
siteName: siteName,
type: "article",
images: {
url: `${siteUrl}/api/og?title=${data.title}${data.thumbnail?.url !== undefined ? `&thumbnail=${data.thumbnail.url}` : ""}`,
url: `${siteUrl}/api/og?title=${encodeURIComponent(data.title)}${data.thumbnail?.url !== undefined ? `&thumbnail=${data.thumbnail.url}` : ""}`,
width: 1200,
height: 630
}
Expand Down
4 changes: 2 additions & 2 deletions components/ArticleShare.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const ArticleShare = ({ article, displayMessage }: Props) => {
<div className={styles.elementRoot}>
<ExternalLink
aria-label="X(旧Twitter)にポスト"
href={`https://twitter.com/intent/tweet?url=${url}&text=${title}`}
href={`https://twitter.com/intent/tweet?url=${url}&text=${encodeURIComponent(title)}`}
className={styles.element}
>
<Image src={twitterLogo} alt="X(旧Twitter)" width={24} className={styles.icon} />
Expand All @@ -38,7 +38,7 @@ export const ArticleShare = ({ article, displayMessage }: Props) => {
</ExternalLink>
<ExternalLink
aria-label="はてなブックマークに登録"
href={`https://b.hatena.ne.jp/add?mode=confirm&url=${url}&title=${title}`}
href={`https://b.hatena.ne.jp/add?mode=confirm&url=${url}&title=${encodeURIComponent(title)}`}
className={styles.element}
>
<Image src={hatenabookmarkLogo} alt="はてなブックマーク" width={24} className={styles.icon} />
Expand Down

0 comments on commit 25a31df

Please sign in to comment.