Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next.js - import server-side package in a file contains both server-side and client-side functionality?

Tags:

next.js

Let's say I have a file named utils.js, containing two functions s and c. s is a server-side function (Being called on an /api endpoint handler), and uses mongodb package. c is a client-side function (will be bundled and sent to the browser).

When compiling the app using next build, will it cause any issues? Does webpack know to bundle only part of a file/module? (considering server-side functions and imports as a "dead code" since they only being called from a server-side code)

Thanks

like image 879
user3343396 Avatar asked Sep 14 '20 07:09

user3343396


People also ask

What are the options for the next import function?

The above next import is a function that receives an object with the following options: dev: Boolean - Whether or not to launch Next.js in dev mode. Defaults to false dir: String - Location of the Next.js project. Defaults to '.' quiet: Boolean - Hide error messages containing server information. Defaults to false

Can I use next JS with my existing backend?

If you have an existing backend, you can still use it with Next.js (this is not a custom server). A custom Next.js server allows you to start a server 100% programmatically in order to use custom server patterns. Most of the time, you will not need this – but it's available for complete customization.

What is nextnext JS?

Next.js builds the HTML page at build time and serves the pre-rendered page from server to browser with minimal JavaScript code and when page is loaded by browser, its JavaScript code runs and makes the page fully interactive. (This Process is called Hydration) Static Generation (SSG): HTML is generated at build time.

What is the difference between server-side and client-side rendering?

Unlike the server-side rendering APIs, you can use client-side data fetching at the component level. If done at the page level, the data is fetched at runtime, and the content of the page is updated as the data changes.


Video Answer


2 Answers

If you need to know which functions get bundled to the client & which ones to the server, there's an easy way to know this → https://next-code-elimination.now.sh/

Just copy & paste the contents of your file into it & you'll see which code gets bundled to the client & which code is bundled to the server. If you have imports then make sure to put all the imports in one file so you can see how it works.

The thumb rule is:

Anything like getServerSideProps, getStaticProps & getStaticPaths will be removed from the bundled code. If you import anything from a file that uses server-side code like fs but doesn't use it in any of the above 3 functions, then it won't be removed (check at Next Code Elimination Tool) & will give you an error.

The tool is the answer. I copy-pasted my file in it & found the answer in an instant.

like image 167
deadcoder0904 Avatar answered Oct 10 '22 09:10

deadcoder0904


I think there will be errors but not in the build time. It is likely issues will happen in the run time. You won't be able to access file systems on the client side just like how you can't access the window object on the server-side.

In my current project, we have utility functions for both the server-side, client-side, or universal. All server-side functions are called in getServerSideProps to make sure they work as expected. All your server-side code in getServerSideProps will not be imported as part of the client-side bundle if that's what you mean by "dead code". According to the Next.js

Note: You can import modules in top-level scope for use in getServerSideProps. Imports used in getServerSideProps will not be bundled for the client-side.

This means you can write server-side code directly in getServerSideProps. This includes reading from the filesystem or a database.

I'm not aware of a way you can ask webpack to bundle part of the file or execute a subset of import statements.

I hope that provides some help.

Reference:

Docs - getServerSideProps

Custom Webpack Config

like image 40
Andrew Zheng Avatar answered Oct 10 '22 10:10

Andrew Zheng