From 99a70d3f2aa16b06d2a93aa46c735b87d511b674 Mon Sep 17 00:00:00 2001 From: ECO Date: Mon, 5 Nov 2018 14:12:38 +0800 Subject: [PATCH 1/4] This pr fixes brotli gibberish response on server. https://github.com/nuxt-community/axios-module/issues/170 --- lib/plugin.template.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/plugin.template.js b/lib/plugin.template.js index 6c6d5eb9..89c07540 100755 --- a/lib/plugin.template.js +++ b/lib/plugin.template.js @@ -159,7 +159,8 @@ export default (ctx, inject) => { // https://github.com/mzabriskie/axios/blob/master/lib/defaults.js headers: { common : { - 'Accept': 'application/json, text/plain, */*' + 'Accept': 'application/json, text/plain, */*', + 'Accept-Encoding': process.server ? 'gzip, deflate' : 'gzip, deflate, br' }, delete: {}, get: {}, From ef066dcce8a72f1cd2a65a54747a232e2ca43dc5 Mon Sep 17 00:00:00 2001 From: ECO Date: Mon, 5 Nov 2018 14:19:16 +0800 Subject: [PATCH 2/4] Update plugin.template.js --- lib/plugin.template.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/plugin.template.js b/lib/plugin.template.js index 89c07540..537e0287 100755 --- a/lib/plugin.template.js +++ b/lib/plugin.template.js @@ -158,9 +158,9 @@ export default (ctx, inject) => { // Axios creates only one which is shared across SSR requests! // https://github.com/mzabriskie/axios/blob/master/lib/defaults.js headers: { + 'Accept-Encoding': process.browser ? 'gzip, deflate, br' : 'gzip, deflate', common : { - 'Accept': 'application/json, text/plain, */*', - 'Accept-Encoding': process.server ? 'gzip, deflate' : 'gzip, deflate, br' + 'Accept': 'application/json, text/plain, */*' }, delete: {}, get: {}, From 422b8fde36a8b660bd04c3880e85c0c050cc3412 Mon Sep 17 00:00:00 2001 From: ECO Date: Wed, 7 Nov 2018 20:38:12 +0800 Subject: [PATCH 3/4] Update plugin.template.js Don't set content-encoding if in browser --- lib/plugin.template.js | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/plugin.template.js b/lib/plugin.template.js index 537e0287..a79f2a75 100755 --- a/lib/plugin.template.js +++ b/lib/plugin.template.js @@ -148,6 +148,22 @@ const setupProgress = (axios, ctx) => { }<% } %> export default (ctx, inject) => { + let headers = { + common : { + 'Accept': 'application/json, text/plain, */*' + }, + delete: {}, + get: {}, + head: {}, + post: {}, + put: {}, + patch: {} + } + + if (process.server) { + headers['Accept-Encoding'] = 'gzip, deflate' + } + const axiosOptions = { // baseURL baseURL : process.browser @@ -157,18 +173,7 @@ export default (ctx, inject) => { // Create fresh objects for all default header scopes // Axios creates only one which is shared across SSR requests! // https://github.com/mzabriskie/axios/blob/master/lib/defaults.js - headers: { - 'Accept-Encoding': process.browser ? 'gzip, deflate, br' : 'gzip, deflate', - common : { - 'Accept': 'application/json, text/plain, */*' - }, - delete: {}, - get: {}, - head: {}, - post: {}, - put: {}, - patch: {} - } + headers: headers } <% if (options.proxyHeaders) { %> From 66b8e37e62b3dfcbab1ce505758b3d8a15438f5f Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 7 Nov 2018 17:08:57 +0330 Subject: [PATCH 4/4] refactor, fix and code style --- lib/plugin.template.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/plugin.template.js b/lib/plugin.template.js index a79f2a75..4dc46d68 100755 --- a/lib/plugin.template.js +++ b/lib/plugin.template.js @@ -148,7 +148,15 @@ const setupProgress = (axios, ctx) => { }<% } %> export default (ctx, inject) => { - let headers = { + // baseURL + const baseURL = process.browser + ? '<%= options.browserBaseURL %>' + : (process.env._AXIOS_BASE_URL_ || '<%= options.baseURL %>') + + // Create fresh objects for all default header scopes + // Axios creates only one which is shared across SSR requests! + // https://github.com/mzabriskie/axios/blob/master/lib/defaults.js + const headers = { common : { 'Accept': 'application/json, text/plain, */*' }, @@ -161,19 +169,12 @@ export default (ctx, inject) => { } if (process.server) { - headers['Accept-Encoding'] = 'gzip, deflate' + headers.common['Accept-Encoding'] = 'gzip, deflate' } const axiosOptions = { - // baseURL - baseURL : process.browser - ? '<%= options.browserBaseURL %>' - : (process.env._AXIOS_BASE_URL_ || '<%= options.baseURL %>'), - - // Create fresh objects for all default header scopes - // Axios creates only one which is shared across SSR requests! - // https://github.com/mzabriskie/axios/blob/master/lib/defaults.js - headers: headers + baseURL, + headers } <% if (options.proxyHeaders) { %>