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.

Error Invalid character in header content ["Location"] with trailingSlash enabled

See original GitHub issue

What version of Next.js are you using?

11.1.2

What version of Node.js are you using?

12.18.1

What browser are you using?

Brave

What operating system are you using?

macOS

How are you deploying your application?

next start

Describe the Bug

When the trailingSlash is enabled, the redirection doesn’t work properly in some of the cases.

Example URL:

http://localhost:3000/some-url/%E2%80%9Chttps://domain.com/analytics?pxid=111&%E2%80%9D

Returned error:

error - TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["Location"]
    at ServerResponse.setHeader (_http_outgoing.js:521:3)
    at Object.fn (/Library/WebServer/Documents/nextjs/node_modules/next/dist/server/next-server.js:554:25)
    at Router.execute (/Library/WebServer/Documents/nextjs/node_modules/next/dist/server/router.js:205:48)
    at DevServer.run (/Library/WebServer/Documents/nextjs/node_modules/next/dist/server/next-server.js:841:47)
    at DevServer.run (/Library/WebServer/Documents/nextjs/node_modules/next/dist/server/dev/next-dev-server.js:355:32)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async DevServer.handleRequest (/Library/WebServer/Documents/nextjs/node_modules/next/dist/server/next-server.js:292:20) {
  code: 'ERR_INVALID_CHAR'
}

Expected Behavior

All of the URLS tried to be accessed are properly redirected without any errors thrown.

To Reproduce

  1. npm install next@11.1.2 react react-dom
  2. create a sample index page
  3. create a next.config.js file:
module.exports = {
  trailingSlash: true,
};
  1. npm run dev
  2. Visit the below URL in the browser:
http://localhost:3000/some-url/%E2%80%9Chttps://domain.com/analytics?pxid=111&%E2%80%9D
  1. See the error

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
tills13commented, Dec 7, 2021

Is it invalid?

The query is ?pxid=111&%E2%80%9D' which is parsed into { 'pxid' => '111', '”' => '' }

Per https://url.spec.whatwg.org/#concept-urlencoded-parser (and is the behaviour of URL)

> const u = new URL('http://localhost:3000/some-url/%E2%80%9Chttps://domain.com/analytics?pxid=111&%E2%80%9D')
> u.search
'?pxid=111&%E2%80%9D'
> u.searchParams
URLSearchParams { 'pxid' => '111', '”' => '' }

At the very least, NextJS shouldn’t encounter an error in this situation.

0reactions
github-actions[bot]commented, Dec 1, 2022

Please verify that your issue can be recreated with next@canary.

Why was this issue marked with the please verify canary label?

We noticed the provided reproduction was using an older version of Next.js, instead of canary.

The canary version of Next.js ships daily and includes all features and fixes that have not been released to the stable version yet. You can think of canary as a public beta. Some issues may already be fixed in the canary version, so please verify that your issue reproduces by running npm install next@canary and test it in your project, using your reproduction steps.

If the issue does not reproduce with the canary version, then it has already been fixed and this issue can be closed.

How can I quickly verify if my issue has been fixed in canary?

The safest way is to install next@canary in your project and test it, but you can also search through closed Next.js issues for duplicates or check the Next.js releases.

My issue has been open for a long time, why do I need to verify canary now?

Next.js does not backport bug fixes to older versions of Next.js. Instead, we are trying to introduce only a minimal amount of breaking changes between major releases.

What happens if I don’t verify against the canary version of Next.js?

An issue with the please verify canary that receives no meaningful activity (e.g. new comments that acknowledge verification against canary) will be automatically closed and locked after 30 days.

If your issue has not been resolved in that time and it has been closed/locked, please open a new issue, with the required reproduction, using next@canary.

I did not open this issue, but it is relevant to me, what can I do to help?

Anyone experiencing the same issue is welcome to provide a minimal reproduction following the above steps. Furthermore, you can upvote the issue using the 👍 reaction on the topmost comment (please do not comment “I have the same issue” without repro steps). Then, we can sort issues by votes to prioritize.

I think my reproduction is good enough, why aren’t you looking into it quicker?

We look into every Next.js issue and constantly monitor open issues for new comments.

However, sometimes we might miss one or two due to the popularity/high traffic of the repository. We apologize, and kindly ask you to refrain from tagging core maintainers, as that will usually not result in increased priority.

Upvoting issues to show your interest will help us prioritize and address them as quickly as possible. That said, every issue is important to us, and if an issue gets closed by accident, we encourage you to open a new one linking to the old issue and we will look into it.

Useful Resources

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix: TypeError: Invalid character in header content ...
js, and I have a login page. When I use specific character such as š on username and write wrong password, I am...
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 >
To slash or not to slash | Google Search Central Blog
If your site has a directory structure, it's more conventional to use a trailing slash with your directory URLs (for example, example.com/directory/ rather...
Read more >
Signing and authenticating REST requests
Requests are allowed or denied in part based on the identity of the requester. ... Under the Amazon S3 authentication scheme, the Authorization...
Read more >
Next.js bug: ERR_INVALID_CHAR Invalid character in header ...
Next.js bug: ERR_INVALID_CHAR Invalid character in header content ["Location"]. 0. Embed Fork Create Sandbox Sign in. Sandbox Info.
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