Skip to content

Rewrites proxy returning cut off response to browser #550

@JackParn33

Description

@JackParn33

Discord thread

When implementing NextJS rewrites to an external URL we found the response being returned to the browser was being cut off causing the page to display incorrectly.

This was fixed for us by adding a custom converter to remove the Accept-Encoding header from the proxy request (code below), however I think there needs to be a wider fix as I would expect others to encounter this hard to diagnose bug.

Also found that using the Streaming wrapper resolves the issue.

@conico974 has pointed out it is likely awaiting the wrong thing when the proxy is both using streaming and accept-encoding (especially when it's a full body like here)

. It probably should be res.on instead of _res, we need to wait for the end of the decompression here.

Custom converter fix

import converter from '@opennextjs/aws/converters/aws-apigw-v2.js';
import type { Converter } from '@opennextjs/aws/types/open-next';

const customConverter: Converter = {
  convertFrom: async event => {
    const result = await converter.convertFrom(event);

    if (result.headers) {
      const { 'Accept-Encoding': _, ...newHeaders } = result.headers;
      return { ...result, headers: newHeaders };
    }

    return result;
  },

  convertTo: converter.convertTo,

  name: 'custom-apigw-v2-no-accepts-encoding',
};

export default customConverter;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions