Published
- 4 min read
Nextjs Facts 2
CSS support
- Next.js supports CSS out of the box, allowing you to import CSS files directly into your components.
- You can use CSS modules to scope your styles locally to a component, preventing style conflicts.
- Next.js also supports global CSS files that are imported into your application and applied globally.
- You can use CSS-in-JS libraries like styled-components or emotion to style your components with JavaScript.
// components/Button.js
import React from 'react'
import styles from './Button.module.css'
const Button = ({ children }) => {
return <button className={styles.button}>{children}</button>
}
export default Button
Client and Server Rendering
- Next.js supports both client-side and server-side rendering, allowing you to choose the best rendering strategy for your application.
- Client-side rendering (CSR) is the default rendering mode in Next.js, where the initial HTML is generated on the server and then hydrated on the client.
- Server-side rendering (SSR) allows you to generate HTML on the server for each request, improving SEO and providing a better user experience.
- You can use getServerSideProps or getStaticProps to fetch data on the server and pass it to your components for rendering.
// pages/index.js
export async function getServerSideProps() {
const data = await fetchData()
return {
props: { data }
}
}
export default function Home({ data }) {
return (
<div>
{data.map(item => (
<div key={item.id}>{item.name}</div>
))}
</div>
)
}
Server Actions
- Next.js allows you to define server actions using API routes, which are serverless functions that run on the server.
- API routes are defined in the pages/api directory and can be used to handle HTTP requests like GET, POST, PUT, DELETE, etc.
- You can use API routes to interact with databases, external APIs, or perform server-side logic without exposing your server code to the client.
- API routes are automatically deployed with your Next.js application and can be accessed at the /api endpoint.
// pages/api/hello.js
export default function handler(req, res) {
res.status(200).json({ message: 'Hello World' })
}
Route Handlers
- Next.js allows you to define route handlers using catch-all routes, which match any route that hasn’t been matched by other routes.
- Catch-all routes are defined using square brackets in the file name, like […slug].js, and can be used to handle dynamic routes with multiple segments.
- You can access the dynamic segments of the route using the useRouter hook or the useRouter hook in your components.
- Catch-all routes are useful for building dynamic pages with flexible routing patterns.
// pages/[...slug].js
import { useRouter } from 'next/router'
export default function DynamicPage() {
const router = useRouter()
const { slug } = router.query
return <div>Dynamic Page: {slug.join('/')}</div>
}
Advanced Routing
- Next.js supports advanced routing features like nested routes, dynamic routes, and optional catch-all routes.
- Nested routes allow you to create a hierarchy of pages with nested folders and files, improving the organization of your application.
- Dynamic routes enable you to create pages with dynamic segments in the URL, like /posts/[id], where id can be any value.
- Optional catch-all routes allow you to match any number of segments in the URL, like /blog/[[…slug]], where slug can be an empty array or an array of values.
// pages/posts/[id].js
import { useRouter } from 'next/router'
export default function PostPage() {
const router = useRouter()
const { id } = router.query
return <div>Post: {id}</div>
}
Nested Layouts
- Next.js allows you to create nested layouts by nesting components within the _app.js file.
- Nested layouts enable you to define a hierarchy of layouts for your application, with each layout wrapping its child components.
- You can use the getLayout function to dynamically choose the layout for a specific page based on its route or other conditions.
- Nested layouts provide a flexible way to structure your application and manage the layout of different pages.
// pages/_app.js
import Layout from '../components/Layout'
function MyApp({ Component, pageProps }) {
const getLayout = Component.getLayout || ((page) => page)
return getLayout(<Component {...pageProps} />)
}
export default MyApp
Middleware
- Next.js allows you to define middleware functions to intercept and modify requests before they are handled by your API routes.
- Middleware functions are defined as higher-order functions that take the request, response, and next function as arguments.
- You can use middleware functions to add authentication, logging, error handling, or other functionality to your API routes.
- Middleware functions can be applied globally to all API routes or selectively to specific routes based on their path or method.
// middleware/auth.js
export default function authMiddleware(handler) {
return (req, res) => {
if (!req.headers.authorization) {
return res.status(401).json({ message: 'Unauthorized' })
}
return handler(req, res)
}
}
// pages/api/protected.js
import authMiddleware from '../../middleware/auth'
export default authMiddleware((req, res) => {
res.status(200).json({ message: 'Protected Route' })
})