Workroom Supplies
next js:-
rendering:-
react js rendering ui on client side where as next js render ui on server side and also give flexibity in rendering
client side rendering means server send html and js file browser excute those file and render the content on window
server side rendering means server renders the files and send the complte renderd page to browser that is shown on window
client side is not good for seo as it makes site crawling hard so we need server side rendering
routing:-
in react js we use react-routes package for routing between pages but next js uses file based routing system i.e we create folder for every page we want
next js have flexibility to create full stack app
code splitting:-
in react we have to do manula code splitting and optimization but next do it automatically
next js manages multiple things:-
routing
code splitting
seo
rendering automatically
-------------------------------
to created a next js app
npx create-next-app@latest ./
the above will create a next app on the current directory with the latest version
routing can be done in 2 ways using src folder or app folder
component can be of 2 types server component and client component and by default all component in app folder in nextjs
to use a component as client component add "use client" at top of the page
whenever we use hooks we should use client side component
when to use server or client component?
read about rendering in nextjs docs
basically write server component and if there is any error switch it to client component aso use client component if using any react functionality like hooks, events etc.
routing in nextjs:-
to create route we just need to create a folder in app folder for ex create a folder with about name and add a file page.js now the contnet in page.js will run on localhost:5000/about (very simple) we can create nesting routes in the same way by creating folder inside folder
to have dynamic route we create a folder with [postid] like this and then page.js in this page.js we can have dynamic postid as prop
layout.js act as entry point to our app and page.js as home page of our app but we can also create layout.js page for particualr folder in app directory so this layout file help us to implement a functionality for pages of particular routest
loading.js can also be created for particular folder it helps us to show a component which can have a skeleton structure or a simpler spinner till our page is getting loaded.
error.js file is used to handle error in our app we can created error.js file for each route. error component must be client component. this file run automatically run when error occurs
what is app route and page route in next js and there diffrence ?
data fetching priciples:-
server side rendering(ssr) :- request a new rendering cycle and data fetching
// here data will not be stored in cache ad fetched again every time component is rendered
await fetch("",
{cache:"no-store"}
)
static site generation(ssg) :- if we remove cache part from the above request it become by default static site generation. idle for content that dont chagne frequently
incremental static generation(isg) :- it combines the both from above we will store cache but for particular time using {next:{revalidate: 10}}
next.js api endpoint:- next js also allow us to create api endpoint in it allowing us to create full fleged app with backend
next js have everything a backend have middleware parsing authecheck
we can create api foder in app directory and write our all api thre rather that having route.js in pages directory which will clash with pages route
app -> api -> post => route.js
we can defines meta data in 2 ways
1:- static metadata
in component write
export const metadata ={
title:"Home"
}
2:- dynamic metadata
export async function generateMetadata({params,searchparams}){
const product = await getProduct(params.id);
return {title: product.title}
}
setting project:-
npm i bcrypt mongodb mongoose next-auth
we create components folder folder at rood directory to create reusable component
other folders we can created are util(for utility function) models(for mongodb models) styles(we can created global styles in this folder)
.env for enviorment variables
2 main files in app folder are page.js and layout.js
npm run dev
if error occur because of root directory go to jsconfig.json and remove / from path
To link from one page to another we use
import Link from "next/link"
import Image from "next/image" :- allow us to use image and automatically optimize it
crs:- client side rendering
empty html page js and css are send seprately and browser compiles that code and generate html page for itself it ws bad for seo
ssr :-server side rendering
in this method a page is rendered on server and send to client every time client request a page is rendered on server and send to client
ssg:- static site generation (generate on build time)
build -> server -> client
we use ssg approch when we know content will not update that frequently so we build the pages and put them on server and whenever client request the page we send it to client it increase speed but build takes time so it is only usefull in wesite where contnet is not updated frequently it can also be used with wordpress
isr :- incremental static regeneration
it is a mixcture of of both of the above read about these more
based on some condition you can decide your approch:-
build time
dynamic contnet i.e how often your content gets updated
seo
render time
code evolution:-
why use next js?
features :- routing in react had to install package but next js gives file based routing to us , api routes you can also write your backend in next js,renering it support client side and server sie rendering, optimization
there are 2 types of componet server side component and client side component(introduced in react 18)
server side component are used when we have task like reading files and fetching data from api all component in nextjs are default server side
server side component are not allowed to use broser api or hooks
to use a component as client component add "use client" at top if we are using hooks and browser feature we use client component
catch all segments:-
it is similar to dynamic route but it is used to catch all the routes and use a single file. it is mostly used in site where we show documentation it allow all the nested url to use same page so we dont nee to make nested files
use 3 dots in dynamic folder and create page.tsx
docs/[...slug]
you can get slug using prop
custom not found page:-
crerate a file not-found.tsx to create a custom not-found.tsx component will be named NotFound
we can also programatically naviage to not found page by calling notFound() method fromnext/navigation;
we can also create not found page for particular route by creating not-found.tsx in the particular path folder
File colocation:-
it means that there can be multiple fiels in a route folder but it wont work without page.tsx with default export
private folder:-
it means that folder(and its subfolder) is private and should not be considered in routing
to create a pricate folder use _ before foldername like _lib
route group:- it means grouping similar routes in a single folder like for authentication routest such as login,singnup,forgot-passwrod all can be added in single path called auth but this will also add auth in url like auth/login to remove auth from url write route group folder anme with () like (auth) now we dont need to use auth in url and we have groupd our url together
(auth) -> login,signup,forgot-passoword
layout:- its a tsx file used to hold ui that is shared between multiple pages
Tie-Back Hooks | |||
Silent Gliss |
|
Sorry, there are no products in this collection