Skip to content

Commit 931b1b5

Browse files
[Docs] Add manual installation instructions for pages (#51995)
Fixes: #51938
1 parent ec19537 commit 931b1b5

File tree

2 files changed

+68
-10
lines changed

2 files changed

+68
-10
lines changed

docs/01-getting-started/01-installation.mdx

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,15 @@ These scripts refer to the different stages of developing an application:
6969
- `start`: runs [`next start`](/docs/app/api-reference/next-cli#production) to start a Next.js production server.
7070
- `lint`: runs [`next lint`](/docs/app/api-reference/next-cli#lint) to set up Next.js' built-in ESLint configuration.
7171

72-
### Create the `app` folder
72+
### Creating directories
7373

74-
Next, create an `app` folder and add a `layout.tsx` and `page.tsx` file. These will be rendered when the user visits the root of your application.
74+
Next.js uses file-system routing, which means how you structure your files determines the routes in your application.
75+
76+
#### The `app` directory
77+
78+
For new applications, we recommend using the App Router. This router allows you to use React's latest features and is an evolution of the Pages Router based on community feedback.
79+
80+
To use the `app` router, create an `app/` folder, then add a `layout.tsx` and `page.tsx` file. These will be rendered when the user visits the root of your application (`/`).
7581

7682
<Image
7783
alt="App Folder Structure"
@@ -123,12 +129,64 @@ export default function Page() {
123129

124130
> **Good to know**: If you forget to create `layout.tsx`, Next.js will automatically create this file for you when running the development server with `next dev`.
125131
126-
### Create the `public` folder
132+
Learn more about [using the App Router](/docs/app/building-your-application/routing/defining-routes).
133+
134+
#### The `pages` directory (optional)
135+
136+
If you prefer to use the Pages Router instead of the App Router, you can create a `pages/` directory at the root of your project.
137+
138+
Then, add an `index.tsx` file inside your `pages` folder. This will be your home page (`/`):
139+
140+
```tsx filename="pages/index.tsx" switcher
141+
export default function Page() {
142+
return <h1>Hello, Next.js!</h1>
143+
}
144+
```
145+
146+
Next, add an `_app.tsx` file inside `pages/` to define the global layout. Learn more about the [custom App file](/docs/pages/building-your-application/routing/custom-app)).
147+
148+
```tsx filename="pages/_app.tsx" switcher
149+
import type { AppProps } from 'next/app'
150+
151+
export default function App({ Component, pageProps }: AppProps) {
152+
return <Component {...pageProps} />
153+
}
154+
```
155+
156+
```jsx filename="pages/_app.js" switcher
157+
export default function App({ Component, pageProps }) {
158+
return <Component {...pageProps} />
159+
}
160+
```
161+
162+
Finally, add a `_document.tsx` file inside `pages/` to control the initial response from the server. Learn more about the [custom Document file](/docs/pages/building-your-application/routing/custom-document).
163+
164+
```tsx filename="pages/_document.tsx"
165+
import { Html, Head, Main, NextScript } from 'next/document'
166+
167+
export default function Document() {
168+
return (
169+
<Html>
170+
<Head />
171+
<body>
172+
<Main />
173+
<NextScript />
174+
</body>
175+
</Html>
176+
)
177+
}
178+
```
179+
180+
Learn more about [using the Pages Router](/docs/pages/building-your-application/routing/pages-and-layouts).
181+
182+
> **Good to know**: Although you can use both routers in the same project, routes in `app` will be prioritized over `pages`. We recommend using only one router in your new project to avoid confusion.
183+
184+
### The `public` folder (optional)
127185

128186
You can optionally create a `public` folder to store static assets such as images, fonts, etc. Files inside `public` directory can then be referenced by your code starting from the base URL (`/`).
129187

130188
## Run the Development Server
131189

132190
1. Run `npm run dev` to start the development server.
133191
2. Visit `http://localhost:3000` to view your application.
134-
3. Edit `app/layout.tsx` or `app/page.tsx` and save to see the updated result in your browser.
192+
3. Edit `app/layout.tsx` (or `pages/index.tsx`) file and save it to see the updated result in your browser.

docs/01-getting-started/02-project-structure.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ This page provides an overview of the file and folder structure of a Next.js pro
2828

2929
## Top-level folders
3030

31-
| | |
32-
| ----------------------------------------------------------------------- | ---------------------------------- |
33-
| [`app`](/docs/app/building-your-application/routing) | App Router |
34-
| [`pages`](/docs/pages/building-your-application/routing) | Pages Router |
35-
| [`public`](/docs/getting-started/installation#create-the-public-folder) | Static assets to be served |
36-
| [`src`](/docs/app/building-your-application/configuring/src-directory) | Optional application source folder |
31+
| | |
32+
| ------------------------------------------------------------------------- | ---------------------------------- |
33+
| [`app`](/docs/app/building-your-application/routing) | App Router |
34+
| [`pages`](/docs/pages/building-your-application/routing) | Pages Router |
35+
| [`public`](/docs/getting-started/installation#the-public-folder-optional) | Static assets to be served |
36+
| [`src`](/docs/app/building-your-application/configuring/src-directory) | Optional application source folder |
3737

3838
## `app` Routing Conventions
3939

0 commit comments

Comments
 (0)