1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

Add link rel=preload support.

This commit is contained in:
Arunoda Susiripala 2017-04-12 16:11:54 +05:30
parent 6e0e7b4d5a
commit 95558ae47c

View file

@ -33,9 +33,45 @@ export class Head extends Component {
_documentProps: PropTypes.any
}
getChunkPreloadLink (filename) {
const { __NEXT_DATA__ } = this.context._documentProps
let { buildStats } = __NEXT_DATA__
const hash = buildStats ? buildStats[filename].hash : '-'
return (
<link
key={filename}
rel='preload'
href={`/_next/${hash}/${filename}`}
as='script'
/>
)
}
getPreloadMainLinks () {
const { dev } = this.context._documentProps
if (dev) {
return [
this.getChunkPreloadLink('manifest.js'),
this.getChunkPreloadLink('commons.js'),
this.getChunkPreloadLink('main.js')
]
}
// In the production mode, we have a single asset with all the JS content.
return [
this.getChunkPreloadLink('app.js')
]
}
render () {
const { head, styles } = this.context._documentProps
const { head, styles, __NEXT_DATA__ } = this.context._documentProps
const { pathname, buildId } = __NEXT_DATA__
return <head>
<link rel='preload' href={`/_next/${buildId}/page${pathname}`} as='script' />
<link rel='preload' href={`/_next/${buildId}/page/_error`} as='script' />
{this.getPreloadMainLinks()}
{(head || []).map((h, i) => React.cloneElement(h, { key: i }))}
{styles || null}
{this.props.children}