1

in next.js, we can use

getStaticProps (Static Generation): Fetch data at build time.

getStaticPaths (Static Generation): Specify dynamic routes to pre-render based >on data.

getServerSideProps (Server-side Rendering): Fetch data on each request.

to run serverside code, but in order to do that, I need to import serverside module in that script, for example, I want to import an authentication module to check the user is genuine or not in getserversideprops . (or database schemas, for example, mongoose)

Since I cannot import in a function, I have to import on the top of the file which means that anyone can see that import and see how I authenticate the user.

example:

import a from 'auth"  

getserversideprops(){

if(a(req) ==true) ... 

}

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
sleeping ZZ
  • 99
  • 2
  • 6
  • 2
    Does this answer your question: [How to best import "server-only" code in Next.js?](https://stackoverflow.com/a/66142570/1870780)? – juliomalves Jan 25 '22 at 16:46

1 Answers1

0

getServerSideProps will run only on the server and this code will not be bundled with the client or the final generated page. Since you should never ever ship the server side code to the client, Next.js already handles it.

Next.js is clever enough to see where you are going to use the imported script, if you are using it on the server, it wont be added to the bundle.

Yilmaz
  • 35,338
  • 10
  • 157
  • 202