javascriptcompressiondeflatepako

pako.deflate not reducing the size of my request


I have a POST request that has a large payload. I'm trying to deflate it with pako before making the request to the server so it is smaller. However my payload sizes are no different for the compressed version than the uncompressed. Am I missing something?

The options I'm sending in has an options.body that is a JSON.stringify(largeJsonObject) and is over 1024 long. When I look in the network tab I can see my request being sent, and the content is encoded, but the request size is the same as when it is not compressed


  async fetch(path, options) {
    const headers = {
      Accept: 'application/json',
      'Content-Type': 'application/json',
    }

    if (
      options.method === 'POST' &&
      options.body &&
      options.body.length > 1024
    ) {
      headers['Content-Encoding'] = 'deflate'
      options.body = pako.deflate(options.body)
    }

    if (this.loggedIn()) {
      headers['Authorization'] = 'Bearer ' + this.getToken()
      if (this.willTokenExpireSoon()) {
        await this.refreshToken()
      }
    }

    return fetch(process.env.REACT_APP_API_HOST + path, {
      headers,
      ...options,
    }).then(this._checkStatus)
  }

Solution

  • This was a PICNIC error (problem in chair not in computer). My server was handling the POST request, saving some data, and then returning the object back to the client uncompressed. This is why the Network tab showed the request being the same size - the payload was smaller in the request, but the response body was still large

    To solve it, I have added pako.deflate in my server also