Last active
May 11, 2019 23:55
-
-
Save jmblog/8f75d0c941ec823e4d641d08189d3d43 to your computer and use it in GitHub Desktop.
I've published as https://github.com/jmblog/nuxt-netlify-http2-server-push
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.netlify.com/blog/2017/07/18/http/2-server-push-on-netlify/ | |
const debug = require('debug')('nuxt:netlify-http2-server-push'); | |
const path = require('path'); | |
const glob = require('glob'); | |
const fs = require('fs'); | |
module.exports = function module(moduleOptions) { | |
// This module is only enabled on production builds | |
if (this.options.dev) { | |
return; | |
} | |
const hook = builder => { | |
debug('Generating a _header file'); | |
generateHeaderFile(this, moduleOptions); | |
}; | |
this.nuxt.hook('generate:distCopied', hook); | |
}; | |
const generateHeaderFile = (context, options) => { | |
const generateDir = path.resolve(context.options.generate.dir); | |
const files = glob.sync(`${generateDir}/**/*.js`); | |
let _headers = '/*\n'; | |
files.forEach(file => { | |
_headers += ` Link: <${file.replace(generateDir, '')}>; rel=preload; as=script\n`; | |
debug(file.replace(generateDir, '')); | |
}); | |
fs.writeFileSync(`${generateDir}/_headers`, _headers); | |
debug('The `_header` file generated'); | |
}; |
Hey, this is nice thanks!
I have an issue when using it though, cannot compile my nuxt project:I use it like this in my nuxt.config.js:
modules: [ 'nuxt-netlify-http2-server-push', { // Specify relative path to the dist directory and its content type resources: [ { path: '**/*.js', as: 'script' }, { path: '/img/thomas-morice-logo-black-v2.svg', as: 'image' }, { path: '/fonts/**.*', as: 'fonts' } ] } ],
and here is the error that I have
✖ error nuxt › netlify-http2-server-push › The
resourcesproperty is required. ✖ fatal TypeError: Cannot read property 'length' of undefined
To fix the error I've mentioned before, you should embed your module and the resources
options inside an array in the nuxt.config.js file.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, this is nice thanks!
I have an issue when using it though, cannot compile my nuxt project:
I use it like this in my nuxt.config.js:
modules: [ 'nuxt-netlify-http2-server-push', { // Specify relative path to the dist directory and its content type resources: [ { path: '**/*.js', as: 'script' }, { path: '/img/thomas-morice-logo-black-v2.svg', as: 'image' }, { path: '/fonts/**.*', as: 'fonts' } ] } ],
and here is the error that I have
✖ error nuxt › netlify-http2-server-push › The
resourcesproperty is required. ✖ fatal TypeError: Cannot read property 'length' of undefined