Text Instructions Let's Start Our Journey With NextJs
তোমার প্রথম প্রেম জাভাস্ক্রিপ্ট। দ্বিতীয় প্রেম React আর তৃতীয় প্রেম হলো Next.Js !!! (মডিউল 24)
#module_release
#mission_be_a_nextjs_ninja
#module_24
আজকে তোমার জন্য স্বরনীয় দিন। আজকে থেকে তুমি তোমার জীবনের নতুন দিগন্ত শুরু করতে যাচ্ছ। খুবই এক্সসাইটিং আর খুবই ইন্টারেস্টিং এই জার্নি হবে তোমার। এই মডিউল দিয়েই শুরু হচ্ছে Next.Js Ninja হওয়ার মিশনInstall Next.Js: নিচে তিনটা কমান্ড দিয়ে দিলাম কি আছে জীবনে।
npx create-next-app@latest my-next-app
cd my-next-app
npm run dev
আজকের মডিউল: খুবই গুরুত্বপূর্ণ কিছু কথাবার্তা হবে আজকের মডিউলে। জাস্ট ১০টা-১১টা ভিডিও। এর মধ্যে থাকবে-- ১. Next.Js কি? ২. React এর পরিবর্তে কেন Next.Js ব্যবহার করবো? ৩. ক্লায়েন্ট সাইড, সার্ভার সাইড, pre-rendering কি?৪. Next.Js এর key ফিচারস গুলো নিয়ে আলোচনা করবো। ৫. আর! নো নো সব যদি এখানেই বলে দেয়ই তাহলে তো ভিডিও দেখার মজাই নস্ট হয়ে গেলো সো জলদি শুরু করে দাও
তোমার এই মিশন মিশন ৫ এর টেনটেটিভ আউটলাইনঃ
18th July, Tuesday: Module-24: Fundamentals of Next.Js
20th July, Thursday: Module-25: Next Level Data Fetching in Next.Js
23rd July, Sunday: Module-26: Explore NextAuth & App Router
25th July, Tuesday: Module-26.5: Practice Day
27th July, Thursday: Module-27: Assignment-6
শুরু হলো, এ রোমান্টিক রিলেশন উইথ Next.Js লেগে থাকলে কত কি যে হবে!! এখানেই নয় তো শেষ!
- 24-1. Let's Start Our Journey With NextJs
- 24-2. Why Next.Js? The Competitive Edge of Next.Js over React.Js
- 24-3: Clear concept of Pre-rendering and Client side rendering
- 24-4: Exploring the World of Next.Js Project Creation and Its Folder Structure
- 24-5: Key features of Next.Js with File based routing system
- 24-6: Exploring Dynamic Routes and Nested Routing
- 24-7: Catch all routes and custom 404 error page also Add CSS Component library
- 24-8: Navigation with Link Component and Programmatically Navigating
- 24-9.1: Explore Simple Layout System
- 24-9.2: Explore Dashboard & Nested Layout System
- 24-10: Using Head Component for better SEO, Image Component, import alias and Module summary
- What is Next.js?
- A React framework for the web
React with superpower
- What is Next.js's superpower?
- Automatic image, font and script optimizations for improved UX and core web vitals
- Pre-rendering (
SSG + SSR
) for blazing fast application - Next level data fetching (data fetching methods runs on the server)
- Powerful routing and layouts
- Build complex interfaces and ship less JavaScript
- Has file system based router built on concepts of pages and can share nested layouts
- API routes (Build API endpoint to securely connect with third party services)
-
Why Next.js?
Next.js is React.js's partners/combiners
-
The competitive edge of Next.js over React.js
-
What is rendering?
Process of turning HTML, CSS and JavaScript into an interactive page
- Client side rendering
Rendering on the runtime
- Pre-rendering
Generated when building and served when requested
- Static Site Generation (SSG)
- Generated on build time served when requested
- Server Side Rendering (SSR)
- Generated dynamically and served based on the request
- Static Site Generation (SSG)
- Next.js follows 2 types of folder structure
- Pages router
- App router (Not stable)
Creating components using app router will be server component that will be generated from server. This server components has limited CSS components library supports. When creating client component have to use 'use client' keyword at the top of the file. getStaticProps, getServersideProps, getStaticPaths will not work in app router.
- What's in Next.js?
- Build in optimizations
- Image optimization (converts to small size but maintains same resolution)
- Dynamic HTML streaming
- React server components
- Data fetching
- CSS supports
Same CSS can be used on different components but style will not overwrite
- Client and server side rendering
- Node.js and Edge runtimes
- Route handlers
- Powerful routing and layouts
- Middleware
- Build in optimizations
- File based routing system
Create a new file inside the pages folder and a new route will be created using the file name and file name of the page have to be start with small letter.
- For nested routing create a folder inside the pages folder and create a file inside the folder. The route will be created using the folder name and file name.
- By using
index.js
in a folder it will be the default route of the folder. And the route will be created using the folder name. Also other files inside the folder will be the sub route of the folder. The route will be created using the folder name and file name.
- By using
- For dynamic routing create a file inside the pages folder and the file name have to be start with
[
and end with]
such as[product].js
. The route will be created using the file name.
-
Dynamic routes
File name: [fileName].js
// dynamic route import {useRouter} from "next/router"; const NewsDetails = () => { const router = useRouter(); return ( <div> <h1>This is details page of: {router.query.newsId}</h1> </div> ); }; export default NewsDetails;
-
'Catch all routes' after dynamic route
[...slug].js
- This used for filtering most of the case.
src => pages => news => [...slug].js
then the route can behttp://localhost:3000/news/2021/08/24/this-is-a-test-news
- This used for filtering most of the case.
-
Custom 404 page.
- Have to create a file with name
404.js
inside the pages folder. The file name have to be start with404
.
File name: 404.js
const NotFound = () => { return ( <div> <img width='100%' src="https://voltexelectrical.com.au/skins/Skin_3/sitebanner/error-404-image-icon.png" alt="404 error"/> </div> ); }; export default NotFound;
- Have to create a file with name
-
How to use Link in Next.js
import {Button} from "antd"; import Link from "next/link"; const NewsPage = () => { return ( <div> <Button type="primary"> <Link href='/'> Back to Home </Link> </Button> </div> ); }; export default NewsPage;
-
Automatically redirect user to home page from 404 page after 5 seconds in Nest.js
import {useRouter} from "next/router"; const NotFound = () => { const router = useRouter(); setTimeout(() => { router.push('/') }, 5000); return ( <div> <img width='100%' src="https://voltexelectrical.com.au/skins/Skin_3/sitebanner/error-404-image-icon.png" alt="404 error"/> </div> ); }; export default NotFound;
-
Per-Page Layouts:
If you need multiple layouts, you can add a property getLayout to your page, allowing you to return a React component for the layout. This allows you to define the layout on a per-page basis. Since we're returning a function, we can have complex nested layouts if desired.
pages/index.js
import Layout from '../components/layout' import NestedLayout from '../components/nested-layout' export default function Page() { return ( <> /** Your content */ </> ) } Page.getLayout = function getLayout(page) { return ( <Layout> <NestedLayout>{page}</NestedLayout> </Layout> ) }
pages/_app.js
export default function MyApp({ Component, pageProps }) { // Use the layout defined at the page level, if available const getLayout = Component.getLayout || ((page) => page) return getLayout(<Component {...pageProps} />) }
- Per-Page Layouts
-
Nested Layouts
-
Position admin dashboard on the right side
If you need multiple layouts, you can add a property getLayout to your page, allowing you to return a React component for the layout. This allows you to define the layout on a per-page basis. Since we're returning a function, we can have complex nested layouts if desired.
src/components/Layouts/DashboardLayout.js
import React, { useState } from 'react'; import { DesktopOutlined, FileOutlined, PieChartOutlined, TeamOutlined, UserOutlined, } from '@ant-design/icons'; import { Breadcrumb, Layout, Menu, theme } from 'antd'; const { Header, Content, Footer, Sider } = Layout; function getItem(label, key, icon, children) { return { key, icon, children, label, }; } const items = [ getItem('Option 1', '1', <PieChartOutlined />), getItem('Option 2', '2', <DesktopOutlined />), getItem('User', 'sub1', <UserOutlined />, [ getItem('Tom', '3'), getItem('Bill', '4'), getItem('Alex', '5'), ]), getItem('Team', 'sub2', <TeamOutlined />, [getItem('Team 1', '6'), getItem('Team 2', '8')]), getItem('Files', '9', <FileOutlined />), ]; const DashboardLayout = ({children}) => { const [collapsed, setCollapsed] = useState(false); const { token: { colorBgContainer }, } = theme.useToken(); return ( <Layout style={{ minHeight: '100vh', }} > <Sider collapsible collapsed={collapsed} onCollapse={(value) => setCollapsed(value)}> <div className="demo-logo-vertical" /> <Menu theme="dark" defaultSelectedKeys={['1']} mode="inline" items={items} /> </Sider> <Layout> <Header style={{ padding: 0, background: colorBgContainer, }} /> <Content style={{ margin: '0 16px', }} > <Breadcrumb style={{ margin: '16px 0', }} > <Breadcrumb.Item>User</Breadcrumb.Item> <Breadcrumb.Item>Bill</Breadcrumb.Item> </Breadcrumb> <div style={{ padding: 24, minHeight: 360, background: colorBgContainer, }} > {children} </div> </Content> </Layout> </Layout> ); }; export default DashboardLayout;
src/components/Layouts/RootLayout.js
import { Breadcrumb, Layout, Menu, theme } from 'antd'; const { Header, Content, Footer } = Layout; const RootLayout = ({children}) => { const { token: { colorBgContainer }, } = theme.useToken(); return ( <Layout className="layout"> <Header style={{ display: 'flex', alignItems: 'center', }} > <div className="demo-logo" /> <Menu theme="dark" mode="horizontal" defaultSelectedKeys={['2']} items={new Array(15).fill(null).map((_, index) => { const key = index + 1; return { key, label: `nav ${key}`, }; })} /> </Header> <Content style={{ padding: '0 50px', }} > <Breadcrumb style={{ margin: '16px 0', }} > <Breadcrumb.Item>Home</Breadcrumb.Item> <Breadcrumb.Item>List</Breadcrumb.Item> <Breadcrumb.Item>App</Breadcrumb.Item> </Breadcrumb> <div className="site-layout-content" style={{ background: colorBgContainer, height: '100vh' }} > {children} </div> </Content> <Footer style={{ textAlign: 'center', }} > Ant Design ©2023 Created by Ant UED </Footer> </Layout> ); }; export default RootLayout;
pages/admin.js
import DashboardLayout from "../../components/Layouts/DashboardLayout"; import RootLayout from "../../components/Layouts/RootLayout"; const AdminHomePage = () => { return ( <div> <h1>Admin Page Without Layout</h1> </div> ); }; export default AdminHomePage; AdminHomePage.getLayout = function getLayout(page) { return ( <RootLayout> <DashboardLayout> {page} </DashboardLayout> </RootLayout> ) };
pages/_app.js
export default function MyApp({ Component, pageProps }) { // Use the layout defined at the page level, if available const getLayout = Component.getLayout || ((page) => page) return getLayout(<Component {...pageProps} />) }
-
How to import on Next.js
- Relative path
../components/Layouts/RootLayout.js
- Absolute path
@/components/Layouts/RootLayout.js
- Relative path
-
Next.js uses
Head
components for SEO friendly website -
Use Next.js
<title>
instead of react-helmet<Head> <title>Next Home Page</title> <meta name='Home page' description='This page is created by Next.js'/> </Head>
-
When using Image in Next.js remember the followings
- Image
<Image width={} height={} src='' alt=''/>
component must includewidth
andheight
property - When using external image in Next.js configure the
hostname
innext.config.js
- When using local image no need to configure the
hostname
innext.config.js
- Image