Home > Back-end >  Access the url that user has entered from in getServerSideProps Next JS
Access the url that user has entered from in getServerSideProps Next JS

Time:10-11

I am currently trying to find the page that the user has came from to enter this current one in the server side props, im not sure if thats possible? or are there any ways that i could access that

TLDR: get the url which user is coming from and not where is going to, example:

coming from: http://localhost:6060/product/id/55555-62/

going to: http://localhost:6060/products/

I would like to access the id query, in the /products/ getServerSideProps

export const getServerSideProps = wrapper.getServerSideProps(store => async ({ req }) => {
  console.log(req);
 

  return {
    props: {},
  };
});

CodePudding user response:

https://nextjs.org/docs/api-reference/data-fetching/get-server-side-props#context-parameter

resolvedUrl: A normalized version of the request URL that strips the _next/data prefix for client transitions and includes original query values.

export const getServerSideProps = (ctx) => {...}

Inside ctx you can find params, query, resolvedUrl etc

CodePudding user response:

Would the referrer enough for the thing you needed it for?

req.headers.referer

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer

  • Related