Dive Into The Details Of Next.js
Next.js is a React Framework used for front-end development that enables us to use functionalities such as generating static websites and server-side rendering for React-based web applications.
One might ask why to use Next.js server side rendering instead of React.js client-side rendering?
It helps in better performance and SEO (Search Engine Optimization).
After creating Next.js app using npx create-next-app, this will scaffold a project with a pages folder among other folders.
This pages folder contains pages that correlate to routes in your application.
Routing dependency is already available with the Link component.
With the built-in Link component, it becomes easy to navigate between pages instead of creating Router Component and then be able to use Link component inside pages that are inside that Router Component.
No need to install additional dependencies to use Link components to use client-side route transitions.
Provides Dynamic Routing and can be used by naming your page’s posts/[blog] in this format. This URL will be accessed as pages/posts/1 or pages/posts/abc
Next.js provides us with functionalities that allow us to fetch data for pre-rendering.
1) getStaticProps: Refers to fetching of data at build-time.
2) getStaticPaths: This Next.js function helps us specify dynamic routes based on data and pre-render those pages.
3) getServerSideProps: This Next.js function helps us fetch data at request time to pre-render it.
Pre-Rendering In Next.js
There are two types of pre-rendering that Next.js offers that is Server-Side Rendering and Static Generation.
If a page uses static generation, then the HTML for that page is generated at build time that means that page HTML is reused at each request by the browser.
Preference is given to static generation as the page HTML generated can be cached by a CDN(Content Delivery Network).
We can use Static generation without Data or with Data.
- In the case of single HTML pages without data will be generated at build time.
- In the case of pages with data, we have two functions that Next.js provides, which we can use together or separately depending on the user: getStaticProps and getStaticPaths.
If your Page content depends on external data we call the getStaticProps function provided by Next.js, we fetch the data and pass it as props to our page.
- This function is called on the same page.
- If your page path depends on external data, we use getStaticPaths called from the same page which uses dynamic routing and can display data for that specific page depending on which path we want to pre-render.
If a page uses server-side rendering, the page HTML is generated at each request.
We use getServerSideProps, and it gets called by the server at each request to fetch data at request Time and passes it to the page.
It allows you Fast Refresh, Ability to update only those components that have been modified and re-render that component.
Next.js also provides Built-in CSS support.
We can apply StylesSheets globally by importing the styles.css file in pages/_app.js.
Now, this styles.css will be applied to all the components inside your project.
Next.js allows us to import stylesheets from node_modules that can be used anywhere in your application. Also provides support for component level CSS.
Next.js also provides us with Fast Refresh that is the changes that are only applied to a specific component only that component will be re-rendered.
Next.js also comes with built-in support for environment variables that need to install additional dependencies to access environment variables from anywhere inside your project components.
Next.js also provides Image Optimization. That is Images are lazy-loaded, which in turn means that images are loaded according to the viewport. Images are loaded according to what we first see on the rendered page, and then as we scroll down, images are loaded.
It does not penalise our build time.
It also provides us with an integrated TypeScript experience.
It also allows us to build an API using API routes. Any file inside pages/api will be treated as an API endpoint instead of carriers and will be server-side bundles.
Applying header for each page HTML can be applied easily with the help of the Head component.
Next.js is a particular arm of the React framework that enables swift server-side rendering for data-heavy products. While several traditional React applications use client-side rendering, Next.js goes against the conventions and leverages server-side to build a better product. This blog acts as Next.js 101, easing you into the What, Why, and How?
It is a part series to many more blogs to come and can help the developer community use Next.js for a quicker project turn around. If you’re stuck somewhere, you could always reach out to a friend, like ZealousWeb, and we can help you finish that project in the least possible time.
You can still use getStaticProps to update your static content which can also be dynamic by incremental static regeneration. You can specify at what time interval your page should be updated.