Fix loading embedded content from Twitter and Instagram when navigating

This commit is contained in:
ponas committed 2019-08-04 21:21:06 +02:00
1 parent bfbaf74fde
commit fc48f00a34
2 files changed
+52

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
/* global twttr instgrm */
const loadTwitter = () => {
if (
typeof twttr !== `undefined` &&
twttr.widgets &&
typeof twttr.widgets.load === `function`
) {
twttr.widgets.load(document.getElementById('___gatsby'))
}
}
const processInstagram = () => {
console.log('process')
if (
typeof instgrm !== `undefined` &&
instgrm.Embeds &&
typeof instgrm.Embeds.process === `function`
) {
console.log('yes')
instgrm.Embeds.process()
}
}
const initScripts = () => {
loadTwitter()
processInstagram()
}
exports.onInitialClientRender = initScripts
exports.onRouteUpdate = initScripts
+21
View File
@@ -0,0 +1,21 @@
const React = require('react')
const SCRIPTS = {
Twitter: 'https://platform.twitter.com/widgets.js',
Instagram: 'https://www.instagram.com/embed.js',
}
const createScriptTag = (key, scriptSrc) => {
return React.createElement(
'script',
{ src: scriptSrc, key: `gatsby-plugin-oembed-${key.toLowerCase()}` },
null
)
}
exports.onRenderBody = ({ setPostBodyComponents }, options) => {
const scriptKeys = ['Twitter', 'Instagram']
const scripts = scriptKeys.map(key => createScriptTag(key, SCRIPTS[key]))
setPostBodyComponents(scripts)
}