question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Next 9.5.4] [ERR_INVALID_CHAR]: Invalid character in header content ["Location"]

See original GitHub issue

Bug report

Describe the bug

internal server error occurs when redirect path includes query strings of non-ascii character(like Japanese).

in this PR, non-ascii character is passed to ‘Location’ header without encoding. https://github.com/vercel/next.js/pull/17323/files

To Reproduce

  1. Set next.config.js with trailing slash config true.
<!-- next.config.js -->
module.exports = {
  trailingSlash: true
};
  1. Open link with query strings of non-ascii character
<!-- indes.jsx -->
export default function IndexPage() {
  return (
    <div>
      <a href="/about?tag=日本語">
        with query strings
      </a>
    </div>
  );
}
  1. Redirected to /about?tag=日本語 page, not /about/?tag=日本語. and 500 error occurs.
TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["Location"]
    at ServerResponse.setHeader (_http_outgoing.js:488:3)
    at Object.fn (/node_modules/next/next-server/server/next-server.ts:569:15)
    at Router.execute (/node_modules/next/next-server/server/router.ts:200:40)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at DevServer.run (/node_modules/next/next-server/server/next-server.ts:855:23)

here is a demo. https://codesandbox.io/s/non-ascii-query-error-c8esl

Expected behavior

redirected to /about/?tag=日本語, with trailing slash. otherwise, encode japanese character.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:16
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
hinsxdcommented, Jun 7, 2021

We encountered this problem when trying to redirect to a url with non-ascii characters

Our solution: wrap the non-ascii characters with encodeURIComponent. Done.

0reactions
github-actions[bot]commented, Feb 22, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next.js bug: ERR_INVALID_CHAR Invalid character in header ...
Next. js bug: ERR_INVALID_CHAR Invalid character in header content ["Location"] - CodeSandbox.
Read more >
How to fix: TypeError: Invalid character in header content ...
I am using Next.js, and I have a login page. When I use specific character such as š on username and write wrong...
Read more >
Invalid character in header content ["Location"]
I'm getting this error message in the browser (500) when I click on logout. Deployed on vercel: https://next-auth-app-1.now.sh/ Invalid ...
Read more >
TypeError: Invalid character in header content ["Authorization ...
this a exercise from Andrew Meads Complete node.js developer course, the problem seems to be on "set("Authorization",.... any idea how to ...
Read more >
Response header name ' ' contains invalid characters ... - IBM
Response header name ' ' contains invalid characters, aborting request ... used to review the content of the HTTP request to find the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found