mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-30 23:32:38 +00:00
feat(spa): support shouldPrefetch and shouldPreload
This commit is contained in:
parent
18f80676f7
commit
160e1d35e4
@ -43,12 +43,16 @@ export default class MetaRenderer {
|
||||
HEAD: '',
|
||||
BODY_SCRIPTS: ''
|
||||
}
|
||||
|
||||
// Get vue-meta context
|
||||
const m = await this.getMeta(url)
|
||||
|
||||
// HTML_ATTRS
|
||||
meta.HTML_ATTRS = m.htmlAttrs.text()
|
||||
|
||||
// BODY_ATTRS
|
||||
meta.BODY_ATTRS = m.bodyAttrs.text()
|
||||
|
||||
// HEAD tags
|
||||
meta.HEAD =
|
||||
m.meta.text() +
|
||||
@ -57,28 +61,40 @@ export default class MetaRenderer {
|
||||
m.style.text() +
|
||||
m.script.text() +
|
||||
m.noscript.text()
|
||||
|
||||
// BODY_SCRIPTS
|
||||
meta.BODY_SCRIPTS = m.script.text({ body: true }) + m.noscript.text({ body: true })
|
||||
|
||||
// Resources Hints
|
||||
|
||||
meta.resourceHints = ''
|
||||
// Resource Hints
|
||||
|
||||
const clientManifest = this.renderer.resources.clientManifest
|
||||
|
||||
const shouldPreload = this.options.render.bundleRenderer.shouldPreload || (() => true)
|
||||
const shouldPrefetch = this.options.render.bundleRenderer.shouldPrefetch || (() => true)
|
||||
|
||||
if (this.options.render.resourceHints && clientManifest) {
|
||||
const publicPath = clientManifest.publicPath || '/_nuxt/'
|
||||
// Pre-Load initial resources
|
||||
|
||||
// Preload initial resources
|
||||
if (Array.isArray(clientManifest.initial)) {
|
||||
meta.resourceHints += clientManifest.initial
|
||||
.filter(file => shouldPreload(file))
|
||||
.map(
|
||||
r => `<link rel="preload" href="${publicPath}${r}" as="script" />`
|
||||
)
|
||||
.join('')
|
||||
}
|
||||
|
||||
// Pre-Fetch async resources
|
||||
if (Array.isArray(clientManifest.async)) {
|
||||
meta.resourceHints += clientManifest.async
|
||||
.filter(file => shouldPrefetch(file))
|
||||
.map(r => `<link rel="prefetch" href="${publicPath}${r}" />`)
|
||||
.join('')
|
||||
}
|
||||
|
||||
// Add them to HEAD
|
||||
if (meta.resourceHints) {
|
||||
meta.HEAD += meta.resourceHints
|
||||
@ -87,12 +103,14 @@ export default class MetaRenderer {
|
||||
|
||||
// Emulate getPreloadFiles from vue-server-renderer (works for JS chunks only)
|
||||
meta.getPreloadFiles = () =>
|
||||
clientManifest.initial.map(r => ({
|
||||
file: r,
|
||||
fileWithoutQuery: r,
|
||||
asType: 'script',
|
||||
extension: 'js'
|
||||
}))
|
||||
clientManifest.initial
|
||||
.filter(file => shouldPreload(file))
|
||||
.map(r => ({
|
||||
file: r,
|
||||
fileWithoutQuery: r,
|
||||
asType: 'script',
|
||||
extension: 'js'
|
||||
}))
|
||||
|
||||
// Set meta tags inside cache
|
||||
this.cache.set(url, meta)
|
||||
|
Loading…
Reference in New Issue
Block a user