Skip to content

Instantly share code, notes, and snippets.

@jmblog
Last active May 11, 2019 23:55
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmblog/8f75d0c941ec823e4d641d08189d3d43 to your computer and use it in GitHub Desktop.
Save jmblog/8f75d0c941ec823e4d641d08189d3d43 to your computer and use it in GitHub Desktop.
// 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');
};
@jericopulvera
Copy link

How do I use this?

@jmblog
Copy link
Author

jmblog commented Oct 21, 2018

@pi0 @jericopulvera Thanks for your comment and I'm so sorry for my late reply.

I've published this module. Please check the repo. 😌
https://github.com/jmblog/nuxt-netlify-http2-server-push

@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

@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