Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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');
};
@thomasmorice-zz
Copy link

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 resources property 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