Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.
This repository was archived by the owner on May 10, 2021. It is now read-only.

Redirecting from "getInitialProps" puts the response in html #116

Closed
@JoviDeCroock

Description

@JoviDeCroock

Describe the bug

When deploying an application on netlify with next-on-netlify which uses a page with getInitialProps that's used for redirecting, like the following:

    res.writeHead(302, {
      Location: `en-us`,
    }).end();

It treats this as html, which means that we get to see a html-page which includes:

{"isBase64Encoded":true,"multiValueHeaders":{"Location":["/en-us/"]},"statusCode":302}

rather than actually being redirected to the pages/[locale] handling this, this seems to work correctly when using next dev.

Error in netlify-logs:

ERROR	Unhandled error during request: Error: "Redirect.getInitialProps()" should resolve to an object. But found "undefined" instead.
    at loadGetInitialProps (/var/task/src/web/out_functions/next_index/nextJsPage.js:50131:11)
    at async Function.appGetInitialProps (/var/task/src/web/out_functions/next_index/nextJsPage.js:5470:21)
    at async Function.module.exports.cha2.App.getInitialProps (/var/task/src/web/out_functions/next_index/nextJsPage.js:35698:20)
    at async loadGetInitialProps (/var/task/src/web/out_functions/next_index/nextJsPage.js:50123:17)
    at async renderToHTML (/var/task/src/web/out_functions/next_index/nextJsPage.js:13361:1275)
    at async renderReqToHTML (/var/task/src/web/out_functions/next_index/nextJsPage.js:49246:22)
    at async Module.render (/var/task/src/web/out_functions/next_index/nextJsPage.js:49363:22)

Could this be because of the wrong response-type being detected?

EDIT: adding return {} to getInitialProps makes it work on netlify, trying to figure out why props being undefined is an issue on netlify but not on next dev.

I noticed this piece in the NextPage.js code

function isResSent(res) {
  return res.finished || res.headersSent;
}

When calling res.end() we should set res.finished to true, this is documented here, we also got headersSent but don't know when to set this to true.

To Reproduce

Steps to reproduce:

  1. Go to /
  2. See html content being the JSON string

Versions

  • Next: 10.0.2
  • next-on-netlify: 2.6.3

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