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

Add link preload to headComponents #558

Merged
merged 4 commits into from
Nov 17, 2016
Merged
Show file tree
Hide file tree
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
34 changes: 0 additions & 34 deletions lib/isomorphic/html-scripts.js

This file was deleted.

47 changes: 37 additions & 10 deletions lib/utils/static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = (locals, callback) => {
{ component, headComponents: [] },
{},
)
let { body, headComponents, ...bodyRenderProps } = results
let { body, headComponents, postBodyComponents, ...bodyRenderProps } = results

// If no one stepped up, we'll handle it.
if (!body) {
Expand All @@ -39,6 +39,10 @@ module.exports = (locals, callback) => {
headComponents = []
}

if (!postBodyComponents) {
postBodyComponents = []
}

// Add the chunk-manifest as a head component.
const chunkManifest = require('!raw!public/chunk-manifest.json')

Expand All @@ -53,21 +57,44 @@ module.exports = (locals, callback) => {
/>
)

const scripts = [`commons`, `app`]
// Path chunk.
scripts.push(pathChunkName(locals.path))
// layout component chunk
scripts.push(
pages.find((page) => page.path === locals.path).componentChunkName
)
let stats
try {
stats = require(`public/stats.json`)
} catch (e) {
// ignore
}
const dascripts = [
pages.find((page) => page.path === locals.path).componentChunkName,
pathChunkName(locals.path),
`app`,
`commons`,
]
dascripts.forEach((script) => {
const fetchKey = `assetsByChunkName[${script}][0]`
const prefixedScript = prefixLink(`/${_.get(stats, fetchKey, ``)}`)

// Add preload <link>s for scripts.
headComponents.unshift(
<link
rel="preload"
href={prefixedScript}
as="script"
/>
)

// Add script tags for the bottom of the page.
postBodyComponents.unshift(
<script key={prefixedScript} src={prefixedScript} />
)
})

const html = `<!DOCTYPE html>\n ${renderToStaticMarkup(
<Html
{...bodyRenderProps}
body={body}
headComponents={headComponents}
postBodyComponents={postBodyComponents}
body={body}
{...renderProps}
scripts={scripts}
/>
)}`
callback(null, html)
Expand Down
11 changes: 11 additions & 0 deletions scripts/publish-site.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
echo "=== Installing npm packages for Gatsby"
npm install
echo "=== Building ES5 version of Gatsby"
npm run build
cd www
echo "=== Installing the website dependencies"
npm install
echo "=== Copying built Gatsby to website."
cp -r ../dist ./node_modules/gatsby/
echo "=== Building website"
./node_modules/.bin/gatsby build
3 changes: 1 addition & 2 deletions www/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React from 'react'
import { prefixLink } from 'gatsby-helpers'
import { GoogleFont, TypographyStyle } from 'react-typography'
import typography from './utils/typography'
import HTMLScripts from 'html-scripts'
import HTMLStyles from 'html-styles'

module.exports = React.createClass({
Expand All @@ -29,7 +28,7 @@ module.exports = React.createClass({
</head>
<body>
<div id="react-mount" dangerouslySetInnerHTML={{ __html: this.props.body }} />
<HTMLScripts scripts={this.props.scripts} />
{this.props.postBodyComponents}
</body>
</html>
)
Expand Down