個人サイトに動的ogを作ってつまづいたところ

このサイトを作ろうとしたときのはなしその 2。

経緯

なわしろ「ブログ書いたら Twitter とかで共有したいぞ」

識者「動的 og が必要だね。Vercel を利用して動的 og を生成する小さいサービスを作ってみるといいよ」

vercel/og チュートリアル

なわしろ「今度は古い情報に踊らされないぞ。検索すると og-image の使用例が最初に出てくるけど、GitHub に但し書きが書いてある」

Warning This repo is outdated and only works with Node.js 14. Please use @vercel/og for new projects.

If you have a problem that reproduces using the playground, please create an issue in the satori repo.

For all other issues with @vercel/og, please reach out to Vercel Support.

なわしろ「公式の案内 に載ってる を見て頑張ってみよう」

なわしろ「背景デザインには haikei を使おう」

つまづき encodeURI

なわしろ「うーん」

識者「どうしたんだい?」

なわしろ「動的 og を設定したんだけど」

GitHub Pages & Next.js で個人サイト作ってつまづいたところ

なわしろ「って表示してほしいのに」

GitHub Pages

なわしろ「になっちゃうんだよね」

識者「& が含まれているけど、URI エンコードはかけてるんだよね?」

なわしろ「こんな感じにかけてるよ」

layout.jsx
<meta
  property="og:image"
  content={`https://example.com/api/og?title=${encodeURI(
    title ? title : siteTitle
  )}`}
/>

識者「ああ、それは encodeURI を使用しているからだよ」

encodeURIComponent

識者「正しくは encodeURIComponent を使うんだよ」

layout.jsx
 <meta
   property="og:image"
-  content={`https://example.com/api/og?title=${encodeURI(
+  content={`https://example.com/api/og?title=${encodeComponentURI(
     title ? title : siteTitle
   )}`}
 />

識者「encodeURI& を含めた機能を持つ文字をエンコードしないんだ。機能があるのにエンコードされたら困るからね」

なわしろ「なるほどなあ」

まとめ

  • encodeURI& などの文字をパーセントエンコードしない。
  • かわりに encodeURIComponent を使う。

バックリンク

ネットワーク

site_graph 20241209-develop-index 🔧技術 - インデックス 20241209-socialmedia-index 📱ソーシャルメディア - インデックス 20241209-develop-index->20241209-socialmedia-index 20241210-digital-gerden デジタルガーデンの整備を進めた話 20241209-develop-index->20241210-digital-gerden 20241218-nos-hagaki-great-changing-plan NosHagaki大改造計画 20241209-develop-index->20241218-nos-hagaki-great-changing-plan 20241223-wcag20-visual-audio-contrast-without-color WCAG 2.0 達成基準 1.4.1 を理解する 20241209-develop-index->20241223-wcag20-visual-audio-contrast-without-color 20241227-social-media-comment-button ウェブサイトにコメントボタンを追加する 20241209-develop-index->20241227-social-media-comment-button 20250105-note-taking-techniques-index 📝メモの技法 - インデックス 20241209-develop-index->20250105-note-taking-techniques-index 20250212-philosophy-dialog-event-by-usable-udon-gimmick-made-story-conclusion 哲学対話イベントで使える Udon ギミックを作った話 まとめ 20241209-develop-index->20250212-philosophy-dialog-event-by-usable-udon-gimmick-made-story-conclusion 20250213-start-shader そろそろシェーダーをはじめる 20241209-develop-index->20250213-start-shader 20250413-ecdsa-sign-learn-in-tsunndere ツンデレで学ぶECDSA署名 20241209-develop-index->20250413-ecdsa-sign-learn-in-tsunndere 20250418-dijkstras-algorithm-learn-in-tsunndere ツンデレで学ぶダイクストラ法 20241209-develop-index->20250418-dijkstras-algorithm-learn-in-tsunndere 20250507-bluesky-and-the-atprotocol Blueskyってなんだ?ATProtocolのアーキテクチャ入門 20241209-develop-index->20250507-bluesky-and-the-atprotocol 20250709-share-your-website-with-brid-gy Brid.gyでウェブサイトを共有してみる 20241209-develop-index->20250709-share-your-website-with-brid-gy 20251212-i-want-to-turn-thino-into-an-indie-web-microblog Thinoをインディーウェブなマイクロブログにしたい 20241209-develop-index->20251212-i-want-to-turn-thino-into-an-indie-web-microblog 20251221-notes-on-managing-adhd ADHD自己管理術:戦略と戦術 20241209-develop-index->20251221-notes-on-managing-adhd 230928-create-ssg-1 GitHub Pages & Next.jsで個人サイト作ってつまづいたところ 20241209-develop-index->230928-create-ssg-1 230929-create-ssg-2 個人サイトに動的ogを作ってつまづいたところ 20241209-develop-index->230929-create-ssg-2 240311-nos-hagaki-making ハッカソンでバーチャルな文通ができるNostrクライアント「NosHagaki」を作ってみた 20241209-develop-index->240311-nos-hagaki-making 240824-a-conversation-with-zettelkasten-by-llm NotebookLMを通じたツェッテルカステンとの対話 20241209-develop-index->240824-a-conversation-with-zettelkasten-by-llm 240905-nostr-music-client-memo Nostrの音楽アプリをぼんやり構想してみる 20241209-develop-index->240905-nostr-music-client-memo 240911-udon-zettelkasten-system ハッカソン成果物発表 - Udon Zettelkasten System の紹介 20241209-develop-index->240911-udon-zettelkasten-system 241020-chatgpt-develop コード書くときChatGPTはどう使える?【WhiteCUL/春日部つむぎ】 20241209-develop-index->241020-chatgpt-develop 241031-sofa-asset-concept ソファのアセット構想 20241209-develop-index->241031-sofa-asset-concept