Skip to content

Commit

Permalink
Move script loader after webpack manifest (#3569)
Browse files Browse the repository at this point in the history
This should ensure that when any external scripts are loaded using
script loader, the webpack manifest is already present.
  • Loading branch information
szimek authored and KyleAMathews committed Jan 18, 2018
1 parent fa4572d commit 59bdc37
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions packages/gatsby/cache-dir/static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,8 @@ module.exports = (locals, callback) => {
)
})

// Add script loader for page scripts to the head.
// Taken from https://www.html5rocks.com/en/tutorials/speed/script-loading/
const scriptsString = scripts.map(s => `"${s}"`).join(`,`)
headComponents.push(
<script
key={`script-loader`}
dangerouslySetInnerHTML={{
__html: `/*<![CDATA[*/!function(e,t,r){function n(){for(;d[0]&&"loaded"==d[0][f];)c=d.shift(),c[o]=!i.parentNode.insertBefore(c,i)}for(var s,a,c,d=[],i=e.scripts[0],o="onreadystatechange",f="readyState";s=r.shift();)a=e.createElement(t),"async"in i?(a.async=!1,e.head.appendChild(a)):i[f]?(d.push(a),a[o]=n):e.write("<"+t+' src="'+s+'" defer></'+t+">"),a.src=s}(document,"script",[${scriptsString}])/*]]>*/`,
}}
/>
)

// Add the chunk-manifest at the end of body element.
const chunkManifest = require(`!raw!../public/chunk-manifest.json`)

postBodyComponents.unshift(
<script
id="webpack-manifest"
Expand All @@ -208,6 +195,18 @@ module.exports = (locals, callback) => {
/>
)

// Add script loader for page scripts to the end of body element (after webpack manifest).
// Taken from https://www.html5rocks.com/en/tutorials/speed/script-loading/
const scriptsString = scripts.map(s => `"${s}"`).join(`,`)
postBodyComponents.push(
<script
key={`script-loader`}
dangerouslySetInnerHTML={{
__html: `/*<![CDATA[*/!function(e,t,r){function n(){for(;d[0]&&"loaded"==d[0][f];)c=d.shift(),c[o]=!i.parentNode.insertBefore(c,i)}for(var s,a,c,d=[],i=e.scripts[0],o="onreadystatechange",f="readyState";s=r.shift();)a=e.createElement(t),"async"in i?(a.async=!1,e.head.appendChild(a)):i[f]?(d.push(a),a[o]=n):e.write("<"+t+' src="'+s+'" defer></'+t+">"),a.src=s}(document,"script",[${scriptsString}])/*]]>*/`,
}}
/>
)

const html = `<!DOCTYPE html>${renderToStaticMarkup(
<Html
{...bodyProps}
Expand Down

0 comments on commit 59bdc37

Please sign in to comment.