Skip to content

Commit

Permalink
fix: correctly ignore brotli encoding on server-side
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Lichter committed Nov 8, 2018
1 parent ceabf1f commit b5321e6
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
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
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
@@ -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 b5321e6

Please sign in to comment.