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.

Can't get current url in getInitialProps.

See original GitHub issue

Bug 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:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
lionelyoungcommented, May 14, 2020
export async function getServerSideProps(context) {
...
  const res = await fetch("http://" + context["req"].headers.host + "/stuff")
....
}
1reaction
freehuntxcommented, May 27, 2021
export async function getServerSideProps(context) {
...
  const res = await fetch("http://" + context["req"].headers.host + "/stuff")
....
}

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get page URL or hostname in NextJs Project?
I will try with window.location.hostname , but it doesn't work. The error: window not defined. enter image description here.
Read more >
getInitialProps - Data Fetching
For the initial page load, getInitialProps will run on the server only. getInitialProps will then run on the client when navigating to a...
Read more >
What is Next.js getInitialProps and getServerSideProps?
getInitialProps behavior can be tricky for developers who are new to Next.js. The function runs only on the server at the initial page...
Read more >
How to get current route in Next.js
Using useRouter(). Using withRouter(). Using Context prop in getInitialProps. Let's understand all three methods with the implementation. Method ...
Read more >
[Solved]-Get URL pathname in nextjs-Reactjs
js file. This is not client side rendered and hence the only way to access you current path would be to pass it...
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