Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: move script loader after webpack manifest #3569

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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