Skip to content

Commit

Permalink
fix: correctly ignore brotli encoding on server-side (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlexLichter authored and pi0 committed Nov 8, 2018
1 parent ceabf1f commit 4a52bfd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
9 changes: 5 additions & 4 deletions lib/plugin.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,6 @@ export default (ctx, inject) => {
patch: {}
}

if (process.server) {
headers.common['Accept-Encoding'] = 'gzip, deflate'
}

const axiosOptions = {
baseURL,
headers
Expand All @@ -183,6 +179,11 @@ export default (ctx, inject) => {
<% for (let h of options.proxyHeadersIgnore) { %>delete axiosOptions.headers.common['<%= h %>']
<% } %><% } %>

if (process.server) {
// Don't accept brotli encoding because Node can't parse it
axiosOptions.headers.common['Accept-Encoding'] = 'gzip, deflate'
}

// Create new axios instance
const axios = Axios.create(axiosOptions)

Expand Down
13 changes: 13 additions & 0 deletions test/axios.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,17 @@ describe('axios module', () => {
expect(d).not.toBeNull()
expect(b).not.toBe(d)
})

test('ssr no brotli', async () => {
const makeReq = login =>
axios
.get(url('/ssr' + (login ? '?login' : '')))
.then(r => r.data)
.then(h => /encoding-\$(.*)\$/.exec(h))
.then(m => (m && m[1] ? m[1] : null))

const result = await makeReq()

expect(result).toBe('gzip, deflate')
})
})
36 changes: 21 additions & 15 deletions test/fixture/pages/ssr.vue
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
<template>
<div>session-{{ axiosSessionId }}</div>
<div>
<div>session-{{ axiosSessionId }}</div>
<div>encoding-${{ axiosEncoding }}$</div>
</div>
</template>

<script>
// This will be intentially shared across requests
let reqCtr = 1
// This will be intentically shared across requests
let reqCtr = 1
export default {
async fetch ({ app, route }) {
let doLogin = route.query.login !== undefined
if (doLogin) {
app.$axios.setHeader('sessionId', reqCtr++)
}
},
computed: {
axiosSessionId () {
return this.$axios.defaults.headers.common.sessionId
export default {
async fetch ({app, route}) {
let doLogin = route.query.login !== undefined
if (doLogin) {
app.$axios.setHeader('sessionId', reqCtr++)
}
},
computed: {
axiosSessionId () {
return this.$axios.defaults.headers.common.sessionId
},
axiosEncoding () {
return this.$axios.defaults.headers.common['Accept-Encoding']
}
}
}
}
</script>
</script>

0 comments on commit 4a52bfd

Please sign in to comment.