Search code examples
next.jsserver-side-renderinglimesurvey

Page with getInitialProps and getStaticProps


I have a page where I need to fetch data with axios which contains credentials and depending on the parameter in the url make another request using the data from the previous request.

I don't know how I can approach this since I can use both getInitialProps and getStaticProps in pages, and in components I can't use them at all.

The code below works, but I don't know how to break it down so that the login is done on the servar side so that I can still get parameters from the URL.

function Result({surveyId, responseId, sessionKey}) {
  return (
    <>
      <div>surveyId: {surveyId}</div>
      <div>responseId: {responseId}</div>
      <div>sessionKey: {sessionKey}</div>
    </>
  )
}

Result.getInitialProps = async ({ query }) => {
  // Example of URL
  // http://localhost:3000/result?surveyId=12345&responseId=6
  const surveyId = query.surveyId
  const responseId = query.responseId

  const data = { method: 'get_session_key', params: ['admin', 'password'], id: 1 }

  const options = { 
    headers: {
      'connection': 'keep-alive',
      'content-type': 'application/json'
    }
  }
  
  const sessionKey = await axios.post(url, data, options).then(
    (response) => {
      return response.data.result
    },
    (error) => {
      console.log(error)
    }
  )

  return { 
    surveyId: surveyId,
    responseId: responseId,
    sessionKey: sessionKey,
  }
}


Solution

  • getStaticProps, getInitialProps or getServerSideProps, they all executed only in the pages. Because client makes the request to the pages, so behind the scene, the way how next.js sets up the routing, whenever a request hits a route, next.js first checks if this page has any of those function, and if it has it runs those functions first, gets the results as props and then runs the component functions and passes the props to the component.

    getsStaticProps is used for static file generation. A good example is generating of blogs. When you run npm run build,next js will run all api calls and populate the page with blogs. So actually, if you check the build folder, inside html files of pages that getStaticPath is executed, all the data will be already inside that html file. Data will be cached by the server so when user makes a request to those statically generated files, data will be served right away. So you should not run the login process in the getStaticProps since login is a dynamic process.

    All those functions are used for prerendering for better SEO optimization. But if you want to load user-specific data you dont need to prerender user-specific data for seo purpose. You could just do client-side as well.

    Or you could use next.js api functions, you would be writing the function inside api directory, and from getServerSideProps you would send a request to the api route. That way, if you need to run same code in a different page, instead of writing the same code for authentication, you would be making request the api function and it would handle for you.