Skip to content

Commit

Permalink
feat(gatsby-plugin-twitter): load widget for all Twitter embeds (#12607)
Browse files Browse the repository at this point in the history
Besides `.twitter-tweet`, there are other elements that Twitter widget renders: timelines, follow and share buttons - as mentioned in [Twitter developer docs](https://developer.twitter.com/en/docs/twitter-for-websites/overview.html).

This small change loads the widget script if _any_ of these elements is present on the page, not only `.twitter-tweet` elements.
  • Loading branch information
tmadeira authored and pieh committed Mar 16, 2019
1 parent 3834b2b commit bb5d62d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/gatsby-plugin-twitter/README.md
@@ -1,7 +1,7 @@
# gatsby-plugin-twitter

Loads the Twitter JavaScript for embedding tweets. Lets you add tweets to
markdown and in other places.
Loads the Twitter JavaScript for embedding tweets, timelines, share and follow
buttons. Lets you add tweets to markdown and in other places.

Note: when copying the embed code, just copy the blockquote section and not the
script.
Expand Down
11 changes: 9 additions & 2 deletions packages/gatsby-plugin-twitter/src/gatsby-browser.js
Expand Up @@ -27,10 +27,17 @@ const injectTwitterScript = () => {

let injectedTwitterScript = false

const embedClasses = [
`.twitter-tweet`,
`.twitter-timeline`,
`.twitter-follow-button`,
`.twitter-share-button`,
].join(`,`)

exports.onRouteUpdate = () => {
// If there's an embedded tweet, lazy-load the twitter script (if it hasn't
// If there's an embedded element, lazy-load the twitter script (if it hasn't
// already been loaded), and then run the twitter load function.
if (document.querySelector(`.twitter-tweet`) !== null) {
if (document.querySelector(embedClasses) !== null) {
if (!injectedTwitterScript) {
injectTwitterScript()
injectedTwitterScript = true
Expand Down

0 comments on commit bb5d62d

Please sign in to comment.