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'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.