Error Invalid character in header content ["Location"] with trailingSlash enabled
See original GitHub issueWhat 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
npm install next@11.1.2 react react-dom
- create a sample index page
- create a
next.config.js
file:
module.exports = {
trailingSlash: true,
};
npm run dev
- Visit the below URL in the browser:
http://localhost:3000/some-url/%E2%80%9Chttps://domain.com/analytics?pxid=111&%E2%80%9D
- See the error
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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)
At the very least, NextJS shouldn’t encounter an error in this situation.
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 againstcanary
) 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