Built-in and intuitive Routing System
Prerendering with SSG and SSR on per-page basis
Automatic code splitting for faster page loads
Client-side routing with optimized prefetching
Support for Sass, PostCSS and any CSS-in-JS library
Development environment with Fast Refresh support
API routes to build API endpoints with Serverless Functions
/pages:one-to-one static router for your site
"/" → pages/index.js
"/about" → pages/about.js
"/post/my-example (/post/:id)" → pages/post/[id]/index.js
"/post/my-example/a-comment (/post/:id/:comment)" → pages/post/[id]/[comment].js
filename as the route name and nested like folder structure
1. Built-in Link tag
<a>:Fully Refetch
<Link> : Using JS to change root div
2. Prefetch in background
Notes: Link only do route within APP
3. Code Splitting
<Link href="/">
<a>Back to home</a>
</Link>
"/public/test.img" 👉 src="/test.img"
To modify each page's metadata
When To Use ?
getStaticProps
Pre-render the page ahead of a user's request.
Recommend for most pages.
export default function Home(props) { ... }
export async function getStaticProps() {
// Get external data from the file system, API, DB, etc.
const data = ...
// The value of the `props` key will be
// passed to the `Home` component
return {
props: ...
}
}
Fetch data inside the app
Fetch external data
Directly query from DB
(Server-side Rendering)
When To Use ?
getServerSideProps
Pre-render a page data whose be fetched at request time
TTFB is slower than Static Generation.
export async function getServerSideProps(context) {
// you can do sth with ur context
return {
props: {
// props for your component
}
}
}
context is an object,
containing like
params, req, res, preview...
Use case ?
SWR : A React hook lib from Next.js
Private and User-Specific Pages
Like User Dashboard
import useSWR from 'swr'
function Profile() {
const { data, error } = useSWR('/api/user', fetch)
if (error) return <div>failed to load</div>
if (!data) return <div>loading...</div>
return <div>hello {data.name}!</div>
}
export async function getStaticPaths() {
return {
paths: [
{ params: { ... } } // See the "paths" section below
],
fallback: true or false // Not introduce this time
};
}
export const getStaticProps: GetStaticProps = async ({ params }) => {
// get { params: { ... } } here
return {
props: {
...
}
}
}
Access the router object by useRouter
import { useRouter } from 'next/router'
function ActiveLink({ children, href }) {
const router = useRouter()
const handleClick = (e) => {
e.preventDefault()
router.push(href)
}
return (
<a href={href} onClick={handleClick}>
{router.pathname}
</a>
)
}
export default ActiveLink
See here for more details.
To get metadata of the route, like pathname
To use router action, like push, replact
Build API endpoints with Serveless Functions
// req = HTTP incoming message, res = HTTP server response
export default function handler(req, res) {
// ...
}
/pages/api/handler.js
Use Case?
What is Vercel?
The Jamstack deployment platform.
( Built by the creators of Next.js )
Support
Features
D for Develop
S for Ship
P for Preview
Writing code in Next.js
Vercel will automatically ship main update to production
Not only code reviews, but deployment reviews
Vercel bot will follow each pull request and create a preview URL
Support Optimal UX from Google's Web Platform team
Built-in Next-specific ESlint to ensure best practice
Third-Party Scripts Optimization
Automatically prioritize loading of third-party scripts
<Script
src="url"
strategy="beforeInteractive" // lazyOnload, afterInteractive
/>
<Script
src={url} // consent mangagement
strategy="beforeInteractive"
onLoad={() => {
// If loaded successfully, then you can load other scripts in sequence
}}
/>
BeforeInteractive
AfterInteractive (default)
lazyOnLoad
Injected into HTML and run before bundled JS
like bot detection
Inject on the client-side and run after hydration
Wait to load during idle time, like chat support
Auto Size Detection
Blur placeholders
import Image from 'next/image'
import author from '../public/me.png'
export default function Home() {
return (
// When importing the image as the source, you
// don't need to define `width` and `height`.
<Image src={author} alt="Picture of the author" />
)
}
Default width and height would Reduce layout shift
Loading state maybe blink
Also support dynamic blurURL ( blurha.sh)
<Image
src="https://nextjs.org/static/images/learn.png"
blurDataURL="data:image/jpeg;base64,/9j/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAIAAoDASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAb/xAAhEAACAQMDBQAAAAAAAAAAAAABAgMABAUGIWEREiMxUf/EABUBAQEAAAAAAAAAAAAAAAAAAAMF/8QAGhEAAgIDAAAAAAAAAAAAAAAAAAECEgMRkf/aAAwDAQACEQMRAD8AltJagyeH0AthI5xdrLcNM91BF5pX2HaH9bcfaSXWGaRmknyJckliyjqTzSlT54b6bk+h0R//2Q=="
alt="Picture of the author"
placeholder="blur"
/>
Optimizing loading
Adding in-memory config caching layer
Improve Startup time
Use Service Worker, WebAssembly, and ES Modules
It act like figma 👇
For Better DX and Optimization