-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-browser.js
54 lines (48 loc) · 1.36 KB
/
gatsby-browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Implement Gatsby's Browser APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/browser-apis/
*/
// You can delete this file if you're not using it
const injectTwitterScript = () => {
function addJS(jsCode) {
var s = document.createElement(`script`)
s.type = `text/javascript`
s.innerText = jsCode
document.getElementsByTagName(`head`)[0].appendChild(s)
}
addJS(`
window.twttr = (function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0],
t = window.twttr || {};
if (d.getElementById(id)) return t;
js = d.createElement(s);
js.id = id;
js.src = "https://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
t._e = [];
t.ready = function(f) {
t._e.push(f);
};
return t;
}(document, "script", "twitter-wjs"));
`)
}
let injectedTwitterScript = false
exports.onRouteUpdate = function({ location }) {
// If there's an embedded tweet, 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 (!injectedTwitterScript) {
injectTwitterScript()
injectedTwitterScript = true
}
if (
typeof twttr !== `undefined` &&
window.twttr.widgets &&
typeof window.twttr.widgets.load === `function`
) {
window.twttr.widgets.load()
}
}
}