とんちゃんといっしょ

Cloudに関する技術とか日常とかについて書いたり書かなかったり

ちょっと不思議なバグを見かけたので調べてみた

某所のニュースリリースページに有るTwitterへ投稿するボタンを押すとおかしなURLがTweet本文に埋め込まれるので修正してはどうかと言ったところ「修正は難しい」って言われたので、「そんなわけあるか!」となり勢いで調べた。

<script type="text/javascript">document.write("<a href=\"https://twitter.com/share?count=none&dlang=ja&text="+getEncodedTitle+"\" target=\"_blank\"><img src=\"*****\"></a>");</script>

というJavaScriptでコードを生成しているようなのだが、報告した先では再現する環境としない環境があるとのこと。

とりあえずTwitterの "https://twitter.com/share" の仕様を見てみる。

Guides — Twitter Developers

url
The url parameter contains an absolute HTTP or HTTPS URL to be shared on Twitter. The shared URL will be automatically shortened in a published Tweet. A Card may appear for a shared URL.
The url parameter may be auto-populated from a canonical link element (<link rel="canonical">) or the location.href of the page when not explicitly set.

The url parameter may be auto-populated from a canonical link element () or the location.href of the page when not explicitly set.

環境依存なのこれじゃね?

ニュースリリースのページを確認

<link rel=“canonical” href=“記事のURL“>

なるほどあっているらしい。

というわけで、TwitterにShareするためのボタンのURLパラメータに url を指定しないと <link rel=“canonical”>location.href が使われるが、後者の location.href が使われた際に意図しない("https://twitter.com/share?...") URLがTweet本文に入ることがあるので、明示的に url のパラメータを指定したほうが間違いないということがわかった。

ね、直し方は簡単だからお願い早く直してあげて・・・