From 160e1d35e450dbaf05cf204a6106826b33c5fca3 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 20 Mar 2018 12:04:39 +0330 Subject: [PATCH] feat(spa): support shouldPrefetch and shouldPreload --- lib/core/meta.mjs | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/core/meta.mjs b/lib/core/meta.mjs index 2915fbdb15..eb9b25c09f 100644 --- a/lib/core/meta.mjs +++ b/lib/core/meta.mjs @@ -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 => `` ) .join('') } + // Pre-Fetch async resources if (Array.isArray(clientManifest.async)) { meta.resourceHints += clientManifest.async + .filter(file => shouldPrefetch(file)) .map(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)