mirror of
https://github.com/hanzoai/next.js.git
synced 2026-09-24 17:00:51 +00:00
This PR document the caching semantics in Next.js, how they interact, and what APIs affect them. We're also taking the opportunity to consolidate terminology, remove duplicate content, and update sections of the docs that relate to caching.
### Documentation
- [x] Create a new section for caching
- [x] Explain how the different caching mechanisms work
- [x] Request Memoization (React Cache)
- [x] Persistent Data Cache
- [x] Persistent Full Route Cache
- [x] In-memory, client-side Router Cache
- [x] Document how different APIs affect caching
- [x] Document cache interactions
- [x] Clean up stale information in the other docs sections
- [x] Routing Section
- [x] Move advanced navigation topics from fundamentals to **How Navigation Works** section
- [x] Rewrite the **How Navigation Works** section
- [x] Rendering Section
- [x] Simplify fundamentals page
- [x] Rewrite the **Static and Dynamic Rendering** pages
- [ ] ~Create a page to explain how **Client and Server Components** are rendered~. Moved to this PR: https://github.com/vercel/next.js/pull/51579
- [x] Data fetching section
- [x] Consolidate data fetching story for fetching, caching, and revalidating
- [x] Clarify data fetching story with 3rd party libraries and React `cache`
- [x] Create **Data Fetching Patterns** page
- [x] Document other related behaviors:
- [x] Update information on scroll position for back/forward navigation
- [x] Remove the concepts of **soft and hard navigation**
- [x] Remove the concepts of **static and dynamic data fetching**
- [x] Use consistent terminology **runtime** 👉🏼 **request time**. Runtime for Edge and Node.js, request time to describe when dynamic stuff happens
- [x] `generateStaticParams` being able to seed the Full Route Cache
- [x] Polish 💅🏼
---
### Related PRs:
- Diagrams: https://github.com/vercel/front/pull/24142
- Redirects: https://github.com/vercel/front/pull/24179
182 lines
5.0 KiB
Plaintext
182 lines
5.0 KiB
Plaintext
---
|
|
title: Tailwind CSS
|
|
description: Style your Next.js Application using Tailwind CSS.
|
|
---
|
|
|
|
{/* The content of this doc is shared between the app and pages router. You can use the `<PagesOnly>Content</PagesOnly>` component to add content that is specific to the Pages Router. Any shared content should not be wrapped in a component. */}
|
|
|
|
<PagesOnly>
|
|
|
|
<details open>
|
|
<summary>Examples</summary>
|
|
|
|
- [With Tailwind CSS](https://github.com/vercel/next.js/tree/canary/examples/with-tailwindcss)
|
|
|
|
</details>
|
|
|
|
</PagesOnly>
|
|
|
|
[Tailwind CSS](https://tailwindcss.com/) is a utility-first CSS framework that works exceptionally well with Next.js.
|
|
|
|
## Installing Tailwind
|
|
|
|
Install the Tailwind CSS packages and run the `init` command to generate both the `tailwind.config.js` and `postcss.config.js` files:
|
|
|
|
```bash filename="Terminal"
|
|
npm install -D tailwindcss postcss autoprefixer
|
|
npx tailwindcss init -p
|
|
```
|
|
|
|
## Configuring Tailwind
|
|
|
|
Inside `tailwind.config.js`, add paths to the files that will use Tailwind CSS class names:
|
|
|
|
```js filename="tailwind.config.js"
|
|
/** @type {import('tailwindcss').Config} */
|
|
module.exports = {
|
|
content: [
|
|
'./app/**/*.{js,ts,jsx,tsx,mdx}', // Note the addition of the `app` directory.
|
|
'./pages/**/*.{js,ts,jsx,tsx,mdx}',
|
|
'./components/**/*.{js,ts,jsx,tsx,mdx}',
|
|
|
|
// Or if using `src` directory:
|
|
'./src/**/*.{js,ts,jsx,tsx,mdx}',
|
|
],
|
|
theme: {
|
|
extend: {},
|
|
},
|
|
plugins: [],
|
|
}
|
|
```
|
|
|
|
You do not need to modify `postcss.config.js`.
|
|
|
|
<AppOnly>
|
|
|
|
## Importing Styles
|
|
|
|
Add the [Tailwind CSS directives](https://tailwindcss.com/docs/functions-and-directives#directives) that Tailwind will use to inject its generated styles to a [Global Stylesheet](/docs/app/building-your-application/styling/css-modules#global-styles) in your application, for example:
|
|
|
|
```css filename="app/globals.css"
|
|
@tailwind base;
|
|
@tailwind components;
|
|
@tailwind utilities;
|
|
```
|
|
|
|
Inside the [root layout](/docs/app/building-your-application/routing/pages-and-layouts#root-layout-required) (`app/layout.tsx`), import the `globals.css` stylesheet to apply the styles to every route in your application.
|
|
|
|
```tsx filename="app/layout.tsx" switcher
|
|
import type { Metadata } from 'next'
|
|
|
|
// These styles apply to every route in the application
|
|
import './globals.css'
|
|
|
|
export const metadata: Metadata = {
|
|
title: 'Create Next App',
|
|
description: 'Generated by create next app',
|
|
}
|
|
|
|
export default function RootLayout({
|
|
children,
|
|
}: {
|
|
children: React.ReactNode
|
|
}) {
|
|
return (
|
|
<html lang="en">
|
|
<body>{children}</body>
|
|
</html>
|
|
)
|
|
}
|
|
```
|
|
|
|
```jsx filename="app/layout.js" switcher
|
|
// These styles apply to every route in the application
|
|
import './globals.css'
|
|
|
|
export const metadata = {
|
|
title: 'Create Next App',
|
|
description: 'Generated by create next app',
|
|
}
|
|
|
|
export default function RootLayout({ children }) {
|
|
return (
|
|
<html lang="en">
|
|
<body>{children}</body>
|
|
</html>
|
|
)
|
|
}
|
|
```
|
|
|
|
## Using Classes
|
|
|
|
After installing Tailwind CSS and adding the global styles, you can use Tailwind's utility classes in your application.
|
|
|
|
```tsx filename="app/page.tsx" switcher
|
|
export default function Page() {
|
|
return <h1 className="text-3xl font-bold underline">Hello, Next.js!</h1>
|
|
}
|
|
```
|
|
|
|
```jsx filename="app/page.js" switcher
|
|
export default function Page() {
|
|
return <h1 className="text-3xl font-bold underline">Hello, Next.js!</h1>
|
|
}
|
|
```
|
|
|
|
</AppOnly>
|
|
|
|
<PagesOnly>
|
|
|
|
## Importing Styles
|
|
|
|
Add the [Tailwind CSS directives](https://tailwindcss.com/docs/functions-and-directives#directives) that Tailwind will use to inject its generated styles to a [Global Stylesheet](/docs/pages/building-your-application/styling/css-modules#global-styles) in your application, for example:
|
|
|
|
```css filename="styles/globals.css"
|
|
@tailwind base;
|
|
@tailwind components;
|
|
@tailwind utilities;
|
|
```
|
|
|
|
Inside the [custom app file](/docs/pages/building-your-application/routing/custom-app) (`pages/_app.js`), import the `globals.css` stylesheet to apply the styles to every route in your application.
|
|
|
|
```tsx filename="pages/_app.tsx" switcher
|
|
// These styles apply to every route in the application
|
|
import '@/styles/globals.css'
|
|
import type { AppProps } from 'next/app'
|
|
|
|
export default function App({ Component, pageProps }: AppProps) {
|
|
return <Component {...pageProps} />
|
|
}
|
|
```
|
|
|
|
```jsx filename="pages/_app.js" switcher
|
|
// These styles apply to every route in the application
|
|
import '@/styles/globals.css'
|
|
|
|
export default function App({ Component, pageProps }) {
|
|
return <Component {...pageProps} />
|
|
}
|
|
```
|
|
|
|
## Using Classes
|
|
|
|
After installing Tailwind CSS and adding the global styles, you can use Tailwind's utility classes in your application.
|
|
|
|
```tsx filename="pages/index.tsx" switcher
|
|
export default function Page() {
|
|
return <h1 className="text-3xl font-bold underline">Hello, Next.js!</h1>
|
|
}
|
|
```
|
|
|
|
```jsx filename="pages/index.js" switcher
|
|
export default function Page() {
|
|
return <h1 className="text-3xl font-bold underline">Hello, Next.js!</h1>
|
|
}
|
|
```
|
|
|
|
</PagesOnly>
|
|
|
|
## Usage with Turbopack
|
|
|
|
As of Next.js 13.1, Tailwind CSS and PostCSS are supported with [Turbopack](https://turbo.build/pack/docs/features/css#tailwind-css).
|