'next.js is it safe to import serverside script in clientside? [duplicate]
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) ...
}
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
