You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this example, we use the standard Web API Response ([MDN](https://developer.mozilla.org/en-US/docs/Web/API/Response)).
42
46
43
47
## API
44
48
45
-
Middleware is created by using a `middleware` function that lives inside a `_middleware` file. Its API is based upon the native [`FetchEvent`](https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent), [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response), and [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) objects.
49
+
Middleware is created by using a `middleware` function that lives inside a `middleware` file. Its API is based upon the native [`FetchEvent`](https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent), [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response), and [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) objects.
46
50
47
51
These native Web API objects are extended to give you more control over how you manipulate and configure a response, based on the incoming requests.
48
52
@@ -75,31 +79,6 @@ Middleware can be used for anything that shares logic for a set of pages, includ
75
79
76
80
## Execution Order
77
81
78
-
If your Middleware is created in `/pages/_middleware.ts`, it will run on all routes within the `/pages` directory. The below example assumes you have `about.tsx` and `teams.tsx` routes.
79
-
80
-
```bash
81
-
- package.json
82
-
- /pages
83
-
_middleware.ts # Willrunonallroutesunder /pages
84
-
index.tsx
85
-
about.tsx
86
-
teams.tsx
87
-
```
88
-
89
-
If you _do_ have sub-directories with nested routes, Middleware will run from the top down. For example, if you have `/pages/about/_middleware.ts` and `/pages/about/team/_middleware.ts`, `/about` will run first and then `/about/team`. The below example shows how this works with a nested routing structure.
90
-
91
-
```bash
92
-
- package.json
93
-
- /pages
94
-
index.tsx
95
-
- /about
96
-
_middleware.ts # Willrunfirst
97
-
about.tsx
98
-
- /teams
99
-
_middleware.ts # Willrunsecond
100
-
teams.tsx
101
-
```
102
-
103
82
Middleware runs directly after `redirects` and `headers`, before the first filesystem lookup. This excludes `/_next` files.
Copy file name to clipboardExpand all lines: docs/advanced-features/react-18/server-components.md
+1-9Lines changed: 1 addition & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -94,15 +94,7 @@ export default function Document() {
94
94
95
95
### `next/app`
96
96
97
-
If you're using `_app.js`, the usage is the same as [Custom App](/docs/advanced-features/custom-app).
98
-
If you're using `_app.server.js` as a server component, see the example below where it only receives the `children` prop as React elements. You can wrap any other client or server components around `children` to customize the layout of your app.
99
-
100
-
```js
101
-
// pages/_app.server.js
102
-
exportdefaultfunctionApp({ children }) {
103
-
return children
104
-
}
105
-
```
97
+
The usage of `_app.js` is the same as [Custom App](/docs/advanced-features/custom-app). Using custom app as server component such as `_app.server.js` is not recommended, to keep align with non server components apps for client specific things like global CSS imports.
Copy file name to clipboardExpand all lines: docs/api-reference/create-next-app.md
+4-2Lines changed: 4 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,8 +31,10 @@ pnpm create next-app -- --ts
31
31
-**--ts, --typescript** - Initialize as a TypeScript project.
32
32
-**-e, --example [name]|[github-url]** - An example to bootstrap the app with. You can use an example name from the [Next.js repo](https://github.com/vercel/next.js/tree/canary/examples) or a GitHub URL. The URL can use any branch and/or subdirectory.
33
33
-**--example-path [path-to-example]** - In a rare case, your GitHub URL might contain a branch name with a slash (e.g. bug/fix-1) and the path to the example (e.g. foo/bar). In this case, you must specify the path to the example separately: `--example-path foo/bar`
34
-
-**--use-npm** - Explicitly tell the CLI to bootstrap the app using npm. To bootstrap using yarn we recommend running `yarn create next-app`
35
-
-**--use-pnpm** - Explicitly tell the CLI to bootstrap the app using pnpm. To bootstrap using yarn we recommend running `yarn create next-app`
34
+
-**--use-npm** - Explicitly tell the CLI to bootstrap the app using npm
35
+
-**--use-pnpm** - Explicitly tell the CLI to bootstrap the app using pnpm
36
+
37
+
Note: To bootstrap using `yarn` we recommend running `yarn create next-app`
Copy file name to clipboardExpand all lines: docs/api-reference/next.config.js/custom-page-extensions.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ module.exports = {
16
16
17
17
> **Note**: The default value of `pageExtensions` is [`['tsx', 'ts', 'jsx', 'js']`](https://github.com/vercel/next.js/blob/f1dbc9260d48c7995f6c52f8fbcc65f08e627992/packages/next/server/config-shared.ts#L161).
18
18
19
-
> **Note**: configuring `pageExtensions` also affects `_document.js`, `_app.js`, `_middleware.js` as well as files under `pages/api/`. For example, setting `pageExtensions: ['page.tsx', 'page.ts']` means the following files: `_document.tsx`, `_app.tsx`, `_middleware.ts`, `pages/users.tsx` and `pages/api/users.ts` will have to be renamed to `_document.page.tsx`, `_app.page.tsx`, `_middleware.page.ts`, `pages/users.page.tsx` and `pages/api/users.page.ts` respectively.
19
+
> **Note**: configuring `pageExtensions` also affects `_document.js`, `_app.js`, `middleware.js` as well as files under `pages/api/`. For example, setting `pageExtensions: ['page.tsx', 'page.ts']` means the following files: `_document.tsx`, `_app.tsx`, `middleware.ts`, `pages/users.tsx` and `pages/api/users.ts` will have to be renamed to `_document.page.tsx`, `_app.page.tsx`, `middleware.page.ts`, `pages/users.page.tsx` and `pages/api/users.page.ts` respectively.
20
20
21
21
## Including non-page files in the `pages` directory
22
22
@@ -32,7 +32,7 @@ module.exports = {
32
32
33
33
Then rename your pages to have a file extension that includes `.page` (ex. rename `MyPage.tsx` to `MyPage.page.tsx`).
34
34
35
-
> **Note**: Make sure you also rename `_document.js`, `_app.js`, `_middleware.js`, as well as files under `pages/api/`.
35
+
> **Note**: Make sure you also rename `_document.js`, `_app.js`, `middleware.js`, as well as files under `pages/api/`.
36
36
37
37
Without this config, Next.js assumes every tsx/ts/jsx/js file in the `pages` directory is a page or API route, and may expose unintended routes vulnerable to denial of service attacks, or throw an error like the following when building the production bundle:
Copy file name to clipboardExpand all lines: docs/api-reference/next/server.md
+17-3Lines changed: 17 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ The `next/server` module provides several exports for server-only helpers, such
8
8
9
9
## NextMiddleware
10
10
11
-
Middleware is created by using a `middleware` function that lives inside a `_middleware` file. The Middleware API is based upon the native [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request), [`FetchEvent`](https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent), and [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) objects.
11
+
Middleware is created by using a `middleware` function that lives inside a `middleware` file. The Middleware API is based upon the native [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request), [`FetchEvent`](https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent), and [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) objects.
12
12
13
13
These native Web API objects are extended to give you more control over how you manipulate and configure a response, based on the incoming requests.
14
14
@@ -105,7 +105,6 @@ The following static methods are available on the `NextResponse` class directly:
105
105
-`redirect()` - Returns a `NextResponse` with a redirect set
106
106
-`rewrite()` - Returns a `NextResponse` with a rewrite set
107
107
-`next()` - Returns a `NextResponse` that will continue the middleware chain
108
-
-`json()` - A convenience method to create a response that encodes the provided JSON data
109
108
110
109
```ts
111
110
import { NextResponse } from'next/server'
@@ -120,7 +119,7 @@ export function middleware(req: NextRequest) {
0 commit comments