Can't get current url in getInitialProps.
See original GitHub issueBug report
Describe the bug
We need to display the full callback url (the same origin with the current url) in text input. For example:
If our portal is https://www.abc.com, the url of the callback is https://www.abc.com/callback.
But we find we can’t get the origin of the current url on server side through getInitialProps.
To Reproduce
static async getInitialProps({ req }) {
console.log(req.url);
}
It prints ‘/’.
Expected behavior
We expect console.log(req.url) prints ‘https://www.abc.com/’ which exactly equals to window.location.href.
More information
We tried next-absolute-url, it almost meets the requirement but one case. The protocol it returns is incorrect. It only returns http when the hostname is localhost, otherwise, it returns https. Obviously, it is not this case in real world. We can use http://www.abc.com, and the protocol is http, however, next-absolute-url returns https.
So, in a sentence, we can’t get the current protocol in getInitialProps on server side.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)

Top Related StackOverflow Question
Please dont suggest this anywhere!
An attacker could abuse your server for ddos attacks for example.
You should make sure you get your domain another way.
E.g. inject it as environment variable and use process.env here.