Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between `next export` and `next build` in Next.js?

I have developed a web app that uses Nextjs app for the frontend and a Python (Django) API server as the backend. Most of my front-end pages have API calls to my backend server (in ComponentDidMount or in response to user actions like button clicks).

I want to deploy this app to my server. I am using Nginx as the reverse proxy. The backend deployment is sorted. I am confused about deploying the nextjs app.

After reading the docs I figured there are 2 ways to do this:

  1. Run next build and then next start. This will start a nodejs server on port 3000. I can channel traffic to this port using Nginx.
  2. Run next export. This will generate an out directory. I can channel incoming traffic to this directory using a reverse proxy like Nginx.

Which of the 2 options should I use? What are the differences?

like image 567
GunnerFan Avatar asked May 11 '20 07:05

GunnerFan


People also ask

Should I use next export?

When to use next export? next export is recommended if you have some dynamic pages which need to some data to be fetched only at 'build' time. It is perfect for pages like landing pages, blogs, news articles etc, or for other kinds of pages which are updated only during code builds.

What is build in NextJS?

js Build API. next build generates an optimized version of your application for production. This standard output includes: HTML files for pages using getStaticProps or Automatic Static Optimization.

What is static export?

The static export is an advanced OpenCms feature, that allows to gain an important performance improvement by mirroring pages and other resources into the real file system of the server.


Video Answer


2 Answers

Answering my own question with the help of the answer I received on the NextJS discussions forum link here

Basics of next build, start and export

next build builds the production application in the .next folder. You need to run this command irrespective of whether you want to run next start or next export.

After building, next start starts a Node.js server that supports hybrid pages, serving both statically generated and server-side rendered pages.

next export will export all your pages to static HTML files that you can serve with any host. This is similar to what create-react-app does, but you can still build dynamic pages at build-time with exportPathMap.

Note: Statically generated pages, using next export, are still reactive i.e. any dynamic JS code, which updates your pages at run time, will continue to run as usual (like any other ReactJS app). Next.js will hydrate your application client-side to give it full interactivity. Its just that the dynamic section of the page will only be rendered in the browser at run time and hence it won't be available to search engine crawlers.

When to use next export?

next export is recommended if you have some dynamic pages which need to some data to be fetched only at 'build' time. It is perfect for pages like landing pages, blogs, news articles etc, or for other kinds of pages which are updated only during code builds. You can setup a background task to build your code at regular intervals if your page data needs to be updated more frequently. This will speed up your 'first paint' speed and also provide the entire page data to search engine crawlers for indexing.

When to use next start?

If you do not need any data fetching at build time i.e. your pages are rendered dynamically at run time then you should use next build and then next start. This will also generate static HTML for sections of your page which are not dynamic. This will speed up the 'first paint' time of your pages. See this Automatic Static Optimization.

like image 61
GunnerFan Avatar answered Oct 21 '22 16:10

GunnerFan


next export, also produces an optimized production ready build, but it builds fully static app, which means only html, css and javascript but no server-side code.

Since there is no server side code, you do not need a server to host your app. This makes hosting your app easier and cheaper because you dont need to maintain and scale a server. There are plenty of hosts in the market which offers very good pricing.

Since next export builds a static app, it means you cannot use api routes, server-side functions therefore you cannot revalidate. Revalidation means, your app will be checking database for a certain of time that you specify. Let's say you have a blogging app, you save your blog posts to database, with server side coe, you get those on server side, pass them to the client and client renders the posts and with revalidation option set, your app would automatically check the database if the content changed. Since you cannot use this feature, anytime content of your app changes, you have to redeploy your app.

like image 29
Yilmaz Avatar answered Oct 21 '22 18:10

Yilmaz