SPA Resource Hints using meta

- W3 valid resource hints
- Supporting options.render.resourceHints
This commit is contained in:
Pooya Parsa 2017-08-19 14:20:45 +04:30
parent 80db40fc1f
commit b2a92e185e
4 changed files with 18 additions and 20 deletions

View File

@ -7,7 +7,6 @@ import ProgressBarPlugin from 'progress-bar-webpack-plugin'
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'
import { resolve } from 'path'
import base from './base.config.js'
import ScriptExtHtmlWebpackPlugin from 'script-ext-html-webpack-plugin'
/*
|--------------------------------------------------------------------------
@ -85,22 +84,6 @@ export default function webpackClientConfig () {
})
)
// Resource hints for SPA
config.plugins.push(
new ScriptExtHtmlWebpackPlugin({
preload: [
'app',
'vendor',
'common',
'manifest'
],
prefetch: {
test: /\.js$/,
chunks: 'async'
}
})
)
// Generate output HTML for SSR
if (this.options.build.ssr) {
config.plugins.push(

View File

@ -3,8 +3,9 @@ import { attrsStr } from 'utils'
import LRU from 'lru-cache'
export default class MetaRenderer {
constructor (nuxt) {
constructor (nuxt, renderer) {
this.nuxt = nuxt
this.renderer = renderer
this.options = nuxt.options
this.cache = LRU({})
}
@ -51,6 +52,21 @@ export default class MetaRenderer {
})
}
// Resource Hints
const clientManifest = this.renderer.resources.clientManifest
if (this.options.render.resourceHints && clientManifest) {
const publicPath = clientManifest.publicPath || '/_nuxt/'
// Pre-Load initial resources
if (Array.isArray(clientManifest.initial)) {
head += clientManifest.initial.map(r => `<link rel="preload" href="${publicPath}${r}" as="script" />`).join('')
}
// Pre-Fetch async resources
if (Array.isArray(clientManifest.async)) {
head += clientManifest.async.map(r => `<link rel="prefetch" href="${publicPath}${r}" />`).join('')
}
}
this.cache.set(url, head)
return head

View File

@ -131,7 +131,7 @@ export default class Renderer extends Tapable {
createRenderer () {
// Create Meta Renderer
this.metaRenderer = new MetaRenderer(this.nuxt)
this.metaRenderer = new MetaRenderer(this.nuxt, this)
// Check if SSR is disabled
if (this.noSSR) {

View File

@ -105,7 +105,6 @@
"pretty-error": "^2.1.1",
"progress-bar-webpack-plugin": "^1.10.0",
"resolve-url-loader": "^2.1.0",
"script-ext-html-webpack-plugin": "^1.8.5",
"serialize-javascript": "^1.4.0",
"serve-static": "^1.12.4",
"server-destroy": "^1.0.1",