Fix loading embedded content from Twitter and Instagram when navigating
This commit is contained in:
1 parent
bfbaf74fde
commit
fc48f00a34
2 files changed
+52
No files matched your search
@@ -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
|
||||
@@ -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)
|
||||
}
|
||||
Reference in new issue
Block a user