Skip to content

Commit ff006b9

Browse files
committed
Twitter, IG Lazy load, rename service worker
1 parent 36b2b9e commit ff006b9

File tree

6 files changed

+86
-30
lines changed

6 files changed

+86
-30
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,92 @@
11
import "./src/styles/global.css";
22
/* global window, document */
3+
const instaEmbedClasses = [".instagram-media"].join(",");
4+
const twitterEmbedClasses = [
5+
".twitter-tweet",
6+
".twitter-timeline",
7+
".twitter-follow-button",
8+
".twitter-share-button",
9+
].join(",");
310
const scrollTo = (id) => () => {
411
const el = document.querySelector(id);
512
if (el) return window.scrollTo(0, el.offsetTop - 20);
613
return false;
714
};
815

16+
const injectScript = function injectScript() {
17+
var js = document.createElement("script");
18+
var firstScript = document.getElementsByTagName("script")[0];
19+
js.id = "gatsby-plugin-instagram";
20+
js.src = "https://instagram.com/embed.js";
21+
firstScript.parentNode.insertBefore(js, firstScript);
22+
injected = true;
23+
if (
24+
typeof instgrm !== "undefined" &&
25+
window.instgrm.Embeds &&
26+
typeof window.instgrm.Embeds.process === "function"
27+
) {
28+
// manual process
29+
window.instgrm.Embeds.process();
30+
}
31+
return true;
32+
};
33+
34+
var injectTwitterScript = function injectTwitterScript() {
35+
function addJS(jsCode) {
36+
var s = document.createElement("script");
37+
s.type = "text/javascript";
38+
s.innerText = jsCode;
39+
document.getElementsByTagName("head")[0].appendChild(s);
40+
injectedTwitterScript = true;
41+
42+
if (
43+
typeof twttr !== "undefined" &&
44+
window.twttr.widgets &&
45+
typeof window.twttr.widgets.load === "function"
46+
) {
47+
window.twttr.widgets.load();
48+
}
49+
}
50+
51+
addJS(
52+
'\n window.twttr = (function(d, s, id) {\n var js,\n fjs = d.getElementsByTagName(s)[0],\n t = window.twttr || {};\n if (d.getElementById(id)) return t;\n js = d.createElement(s);\n js.id = id;\n js.src = "https://platform.twitter.com/widgets.js";\n fjs.parentNode.insertBefore(js, fjs);\n t._e = [];\n t.ready = function(f) {\n t._e.push(f);\n };\n return t;\n })(document, "script", "twitter-wjs");\n '
53+
);
54+
};
55+
let injected = false;
56+
let injectedTwitterScript = false;
57+
958
export const onRouteUpdate = ({ location: { hash } }) => {
1059
if (hash) {
1160
window.setTimeout(scrollTo(hash), 10);
1261
}
62+
63+
if (document.querySelector(instaEmbedClasses) !== null) {
64+
setTimeout(() => {
65+
if (!injected) {
66+
window.addEventListener(
67+
"scroll",
68+
function () {
69+
injectScript();
70+
},
71+
{ once: true }
72+
);
73+
}
74+
}, 2000);
75+
}
76+
77+
if (document.querySelector(twitterEmbedClasses) !== null) {
78+
setTimeout(() => {
79+
if (!injectedTwitterScript) {
80+
window.addEventListener(
81+
"scroll",
82+
function () {
83+
injectTwitterScript();
84+
},
85+
{ once: true }
86+
);
87+
}
88+
}, 2000);
89+
}
1390
};
91+
92+
export const registerServiceWorker = () => true;

gatsby-wordpress-theme-libre/gatsby-config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ module.exports = (themeOptions) => {
110110
icon: "static/favicon.png",
111111
},
112112
},
113-
'gatsby-plugin-remove-serviceworker',
114113
{
115114
resolve: `gatsby-plugin-feed`,
116115
options: {

gatsby-wordpress-theme-libre/gatsby-ssr.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

gatsby-wordpress-theme-libre/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@
5858
"gatsby-plugin-manifest": "^2.2.48",
5959
"gatsby-plugin-react-helmet": "^3.1.23",
6060
"gatsby-plugin-remove-generator": "^1.0.5",
61-
"gatsby-plugin-remove-serviceworker": "^1.0.0",
6261
"gatsby-plugin-sharp": "^2.4.11",
6362
"gatsby-plugin-typescript": "^2.2.3",
6463
"gatsby-source-filesystem": "^2.1.53",
6564
"gatsby-source-wordpress": "^3.1.69",
6665
"gatsby-transformer-sharp": "^2.3.18",
6766
"html-to-text": "^5.1.1",
67+
"lodash": "^4.17.19",
6868
"node-fetch": "^2.6.0",
6969
"react-helmet": "^5.2.1",
7070
"react-intersection-observer": "^8.26.2",

yarn.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6294,11 +6294,6 @@ gatsby-plugin-remove-generator@^1.0.5:
62946294
resolved "https://registry.yarnpkg.com/gatsby-plugin-remove-generator/-/gatsby-plugin-remove-generator-1.0.5.tgz#ca1da320b7a860fb6e7a1974e9b433acc45bdc39"
62956295
integrity sha512-9Iq8JBHcc9BOp/uDCF0ctI9ntr6LjGUTgHkmwEiYiuHG6DQxutwCFQ9OVT9bomYKF/nDFQW0MfbC4eP9il4YCA==
62966296

6297-
gatsby-plugin-remove-serviceworker@^1.0.0:
6298-
version "1.0.0"
6299-
resolved "https://registry.yarnpkg.com/gatsby-plugin-remove-serviceworker/-/gatsby-plugin-remove-serviceworker-1.0.0.tgz#9fb433bc8bd766e14e1d3711c4ac6f051e1dff7c"
6300-
integrity sha1-n7QzvIvXZuFOHTcRxKxvBR4d/3w=
6301-
63026297
gatsby-plugin-sharp@^2.4.11:
63036298
version "2.4.13"
63046299
resolved "https://registry.yarnpkg.com/gatsby-plugin-sharp/-/gatsby-plugin-sharp-2.4.13.tgz#da47cfd8a0d80fc35b7bb072d8a84f6f4f29116d"
@@ -8855,6 +8850,11 @@ lodash@4.17.15, lodash@^4.11.1, lodash@^4.13.1, lodash@^4.17.11, lodash@^4.17.12
88558850
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"
88568851
integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==
88578852

8853+
lodash@^4.17.19:
8854+
version "4.17.19"
8855+
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.19.tgz#e48ddedbe30b3321783c5b4301fbd353bc1e4a4b"
8856+
integrity sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==
8857+
88588858
log-update@^3.0.0:
88598859
version "3.4.0"
88608860
resolved "https://registry.yarnpkg.com/log-update/-/log-update-3.4.0.tgz#3b9a71e00ac5b1185cc193a36d654581c48f97b9"
@@ -12216,7 +12216,7 @@ shallowequal@^1.0.1, shallowequal@^1.1.0:
1221612216
resolved "https://registry.yarnpkg.com/shallowequal/-/shallowequal-1.1.0.tgz#188d521de95b9087404fd4dcb68b13df0ae4e7f8"
1221712217
integrity sha512-y0m1JoUZSlPAjXVtPPW70aZWfIL/dSP7AFkRnniLCrK/8MDKog3TySTBmckD+RObVxH0v4Tox67+F14PdED2oQ==
1221812218

12219-
sharp@0.23.4, sharp@^0.23.4:
12219+
sharp@^0.23.4:
1222012220
version "0.23.4"
1222112221
resolved "https://registry.yarnpkg.com/sharp/-/sharp-0.23.4.tgz#ca36067cb6ff7067fa6c77b01651cb9a890f8eb3"
1222212222
integrity sha512-fJMagt6cT0UDy9XCsgyLi0eiwWWhQRxbwGmqQT6sY8Av4s0SVsT/deg8fobBQCTDU5iXRgz0rAeXoE2LBZ8g+Q==

0 commit comments

Comments
 (0)