Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get full browser url in next js, getServerSideProps

Now, i’m in http://localhost:3000/, but on prod i will be in a different url, for example http://example.com/, how can i get full browser url in getServerSideProps? I have to get http://localhost:3000/ or http://example.com/, if string will not contain port, it will be ok

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

‘next/router’ provide a couple of methods too, which you can use. For example:

import { useRouter } from 'next/router';

const RouterObject = () => {
  const { asPath, pathname, req, query, res } = useRouter();
  console.log(asPath);
  console.log(pathname); 
  console.log('host: ', req.headers.host);
}

If you have the host you can check if contains localhost. Then you know you are in your local environment.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading