-
-
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'); | |
| }; |
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
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 › Theresourcesproperty 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.
@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