Next is a free, lightweight, and opensource react framework for static and server-side rendering. By using Next.js, easy to build a frontend react web Applications.
The list of next.js main features:
- Automatic Routing
- Dynamic Components-Easy to import JavaScript modules and react components dynamically
- Server -side Rendering
- Automatics code splitting for faster page loads
- Source map support
- Single file components
- Static export
- Prefetching
- Simple depolyment
- Full CSS Support-Built in CSS and Pre-processing via Plugins
- Easy to customize babel and webpack configurations
- TypeScript Support-Next.js written in Typescript.
- AMP pages
To Install Next.js, you need have node.js install. (Latest version)
After install NodeJS, Check Below NPM code
npm install โsave next react react-dom
Add a build script:
{
โscriptsโ: {
โdevโ: โnextโ,
โbuildโ: โnext buildโ,
โstartโ: โnext startโ
}
}
Next,Run
After that, the file-system is the main API. Every .js file becomes a route that gets automatically processed and rendered.
Create a pageโs directory inside your project.
Populate ./pages/index.js with the following contents:
function HomePage() {
return <div>Welcome to Next.js!</div>
}
export default HomePage
Check More: https://nextjs.org/docs/getting-started
SSR stands for Server-side rendering. It is a one of the popular techniques for rendering a normally client-side only single page app (SPA) on the server and then sending a fully rendered page to the client. Server side rendered pages will load faster because the content is available to the browser sooner.
Open next.config.js and add the generateBuildId function
// next.config.js
module.exports = {
generateBuildId: async () => {
// For example get the latest git commit hash here
return โmy-build-idโ;
}
};
Add the onDemandEntries config:
module.exports = {
onDemandEntries: {
// period (in ms) where the server will keep pages in the buffer
maxInactiveAge: 25 * 1000,
// number of pages that should be kept simultaneously without being disposed
pagesBufferLength: 2,
},
}
By default, localhost:3000
Next.js is React framework for SSR. There is Angular Universal for SSR with Angular.
You can use any CSS-in-JS solution in your and bundles styled-jsx supporting scoped css.
Create-react-app and next.js project is the routing system since Next.js creates different bundles for each base address. For each route, a separate file is created in the page folder. So, you need to use next/Link component instead of react-router-dom. Also, keep in mind that for static files the static folder is used, but not public, and bundles are located in the folder.next