Skip to content

Commit

Permalink
fix: [HTML] Fix race conditions with Firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
aethys256 committed Oct 2, 2018
1 parent 4487c91 commit 7f3bef5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
8 changes: 4 additions & 4 deletions gatsby-browser.js
@@ -1,4 +1,4 @@
export const onInitialClientRender = () => {
export const onClientEntry = () => {
// Make sure the main script exists
if (!window.herodamage) window.herodamage = {}
const hd = window.herodamage
Expand All @@ -8,7 +8,7 @@ export const onInitialClientRender = () => {

if (process.env.NODE_ENV === 'production') {
// Google Publisher Tag
const googletag = window.googletag || {}
const googletag = window.googletag = window.googletag || {}
googletag.cmd = googletag.cmd || []
googletag.cmd.push(function () {
const topBotMapping = googletag.sizeMapping()
Expand Down Expand Up @@ -62,7 +62,7 @@ export const onInitialClientRender = () => {
googletag.enableServices()
})

// CookieConsent
// Cookie Consent
window.addEventListener('load', function () {
window.cookieconsent.initialise({
palette: {
Expand All @@ -75,5 +75,5 @@ export const onInitialClientRender = () => {
}

// Prevent further initialization
hd.hasInitialized = true
hd.hasInitialized = Date.now()
}
11 changes: 8 additions & 3 deletions src/components/gpt-ad.js
Expand Up @@ -82,10 +82,15 @@ class Ad extends React.Component {
* Start the timer before checking for blockers
*/
static scheduleBlockersCheck () {
if (window.herodamage.hasInitialized) {
setTimeout(Ad.blockersCheck, 500)
const hdInitTime = window.herodamage.hasInitialized
if (hdInitTime && Date.now() - hdInitTime > 500) {
if (Ad.adsReady()) {
setTimeout(Ad.blockersCheck, 500)
} else {
setTimeout(Ad.scheduleBlockersCheck, 50)
}
} else {
setTimeout(Ad.scheduleBlockersCheck, 20)
setTimeout(Ad.scheduleBlockersCheck, 50)
}
}

Expand Down
12 changes: 5 additions & 7 deletions src/html.js
Expand Up @@ -12,13 +12,11 @@ export default class HTML extends React.Component {
let googlePublisherTag, cookieconsent
if (process.env.NODE_ENV === 'production') {
googlePublisherTag = <script async='async' src='https://www.googletagservices.com/tag/js/gpt.js'/>
cookieconsent = (
<>
<link type="text/css" rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.4/cookieconsent.min.css"/>
<script async src="https://cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.1.0/cookieconsent.min.js"/>
</>
)
cookieconsent = (<>
<link type="text/css" rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.4/cookieconsent.min.css"/>
<script async src="https://cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.1.0/cookieconsent.min.js"/>
</>)
}
const { body, bodyAttributes, headComponents, htmlAttributes, postBodyComponents, preBodyComponents } = this.props
return (
Expand Down

0 comments on commit 7f3bef5

Please sign in to comment.