What is Next.js?

Next.js is a Production grade React Framework.

 

"Next.js gives you the best developer experience with all the features you need for production: hybrid static & server rendering, TypeScript support, smart bundling, route pre-fetching, and more. No config needed."

Out Of the Box Features

 

  • Code-Splitting and Bundling
  • Typescript Support
  • Sass and CSS support
  • Fast Refresh
  • Lint Config
  • Compatible with Webpack 5

"Next.js focus on performance since you start using it, it is production ready"

File-System Routing

  • Next.js has a file-system based router built on the concept of pages.
  • When a file is added to the pages directory it's automatically available as a route.
  • The files inside the pages directory can be used to define most common patterns.

Index Routes

  • The router will automatically route files named index to the root of the directory.
pages/index.js → /

pages/blog/index.js → /blog

Nested Routes

  • The router supports nested files. If you create a nested folder structure files will be automatically routed in the same way still.
pages/blog/first-post.js → /blog/first-post

pages/dashboard/settings/username.js → /dashboard/settings/username

Dynamic Route Segments

 

  • To match a dynamic segment you can use the bracket syntax. This allows you to match named parameters.
pages/blog/[slug].js → /blog/:slug (/blog/hello-world)

pages/[username]/settings.js → /:username/settings (/foo/settings)

pages/post/[...all].js → /post/* (/post/2020/id/title)

SSR vs SSG vs Html Export

CSR - Client Side Rendering

Pre-Rendering

  • By default, Next.js pre-renders every page. This means that Next.js generates HTML for each page in advance, instead of having it all done by client-side JavaScript. Pre-rendering can result in better performance and SEO.

  • Each generated HTML is associated with minimal JavaScript code necessary for that page. When a page is loaded by the browser, its JavaScript code runs and makes the page fully interactive. (This process is called hydration.)

SSR

  • The HTML is generated on each request.

  • To make a page use Server-side Rendering, export getServerSideProps. Because Server-side Rendering results in slower performance than Static Generation, use this only if absolutely necessary.

SSG - Static Site Generation

  • The HTML is generated at build time and will be reused on each request.

  • To make a page use Static Generation, either export the page component, or export getStaticProps (and getStaticPaths if necessary). It's great for pages that can be pre-rendered ahead of a user's request. You can also use it with Client-side Rendering to bring in additional data.

HTML Export

  • HTML is generated once in export time and reused everytime

  • next export command allows you to export your app to static HTML, which can be run standalone without the need of a Node.js server.

Coding Time

Next.js v12

  • Rust Compiler: ~3x faster Fast Refresh and ~5x faster builds

  • Middleware (beta): Enabling full flexibility in Next.js with code over configuration

  • React 18 Support: Native Next.js APIs are now supported, as well as Suspense

  • React Server Components (alpha): including SSR streaming

Thanks!

Next.js

By Cesar Guerrero Rincon