Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When exactly is Next.js “build-time” happening?

I'm reading Next.js fetching data part of the documentation and one question came to my mind.

Next.js can fetch data with getStaticProps, getStaticPaths, getInitialProps and getServerSideProps, right?

But some happens on Build-time as I quote:

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

When is this Build-time happening?

Is it when we run npm run build? (To build the production build)

Or when the user accesses our Next.js app? (On each request)

like image 428
Gabriel TN Avatar asked Oct 25 '20 04:10

Gabriel TN


People also ask

What is NextJS build time?

Build time (or build step) is the name given to a series of steps that prepare your application code for production. When you build your application, Next. js will transform your code into production-optimized files ready to be deployed to servers and consumed by users.

Is next JS production ready?

Next. js offers a production-ready solution to routing and several other optimizations that will allow you to build an app with little to no configuration and you are left to focus on building a product in a pre-configured development environment.

Why next JS is fast?

Actually, it is based on Node. js and Babel, and it integrates with React to develop Single Page Applications. This makes server-side convenient and easier. Next JS is a JavaScript framework that allows developers to create user-friendly and blazing fast static websites and static web applications with React.


1 Answers

Build time happens when you're building the app for production (next build). Runtime happens when the app is running in production (next start).

getInitialProps (gIP) runs on both the client and the server during runtime for every page request. Most common use case is retrieving some sort of shared data (like a session that lets the client and server know if a user is navigating to a specific page) before the requested page loads. This always runs before getServerSideProps. While its usage is technically discouraged, sometimes it absolutely necessary to do some logic on both the client and server.

getServerSideProps (gSSP) only runs on the server during runtime for every page request. Most common use case would be to retrieve up-to-date, page-specific data (like a product's pricing and displaying how much of it is in stock) from a database before a page loads. This is important when you want a page to be search engine optimized (SEO), where the search engine indexes the most up-to-date site data (we wouldn't want our product to show "In stock - 100 units," when it's actually a discontinued product!).

getStaticProps (gSProps) only runs during build-time (sort of). It's great for sites whose data and pages don't update that often. The advantage of this approach is the page is statically generated, so if a user requests the page, they'll download an optimized page where the dynamic data is already baked into it. Most common use case would be a blog or some sort of large shopping catalog of products that may not change that often.

getStaticPaths (gSPaths) only runs during build-time (sort of). It's great for pre-rendering similar paths (like a /blog/:id) that require dynamic data to be used at build-time. When used in conjunction with gSProps, it generates the dynamic pages from a database/flat file/some sort of data structure, which Next can then statically serve. An example use case would be a blog that has many blog posts which share the same page layout and similar page URL structure, but require the content to be dynamically baked into the page when the app is being built.

Why are gSProps and gSPaths sort of ran during build-time? Well, Next introduced revalidate with fallback options that allows these two Nextjs functions to be ran during runtime after a timeout in seconds. This is useful if you want your page to be statically regenerated, but it should only be regenerated every n amount of seconds. The upside is that the page doesn't need to request dynamic data when navigated to, but the downside is that the page may initially show stale data. A page won't be regenerated until a user visits it (to trigger the revalidation), and then the same user (or another user) visits the same page to see the most up-to-date version of it. This has the unavoidable consequence of User A seeing stale data while user B sees accurate data. For some apps, this is no big deal, for others it's unacceptable.

And lastly, there's client-side rendering (CSR) which are assets requested at run-time on the client (the browser) which aren't vital for SEO or can't be run on the server-side (like attaching an Event Listener to the document; this can't be done because server's don't have DOMs). Most common use case would be a user-specific dashboard page that is only relevant to that user and therefore doesn't need to be indexed by a search engine or some sort of JavaScript that manipulates the DOM and therefore can't be run on the server.

Other things of note: gIP and gSSP are render blocking. Meaning, the page won't load until their code resolves/returns props. This has the unavoidable consequence of showing a blank page before the page loads. This also means slower page response times where: Page is requested, gIP/gSSP runs code that blocks page from loading, gIP/gSSP code resolves, assets are then downloaded, page begins to load the JavaScript on the client while the HTML is being loaded into the DOM, JavaScript is done running, page is then rehydrated and then it becomes interactive. In summation, this results in a slower TTI.

like image 124
Matt Carlotta Avatar answered Sep 18 '22 09:09

Matt Carlotta