From 074f98cbdf409473357b2cd24cd6cb13fe75f149 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Tue, 21 Apr 2020 22:11:50 +0200 Subject: [PATCH] feat(plugin): enable server brotli if supported (closes #276) --- lib/plugin.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/plugin.js b/lib/plugin.js index a7fcc0f..3cbfc6c 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -4,6 +4,13 @@ import defu from 'defu' const globalName = '<%= globalName %>' +// Check Node.js brotli support +let brotliSupported +if (process.server) { + const zlib = require('zlib') + brotliSupported = typeof zlib.createBrotliDecompress == 'function' +} + // Axios.prototype cannot be modified const axiosExtra = { setBaseURL (baseURL) { @@ -206,8 +213,8 @@ export default (ctx, inject) => { } <% } %> - if (process.server) { - // Don't accept brotli encoding because Node can't parse it + // Don't accept brotli encoding because Node can't parse it + if (process.server && !brotliSupported) { axiosOptions.headers.common['accept-encoding'] = 'gzip, deflate' }