Unverified Commit bbaa1647 authored by Delba de Oliveira's avatar Delba de Oliveira Committed by GitHub

Docs IA 2.0: Move examples to middleware.js API reference (#80363)

Part of:
https://linear.app/vercel/issue/DOC-4686/clean-up-routing-section
parent 39fbaaf4
...@@ -37,7 +37,7 @@ Top-level files are used to configure your application, manage dependencies, run ...@@ -37,7 +37,7 @@ Top-level files are used to configure your application, manage dependencies, run
| [`next.config.js`](/docs/app/api-reference/config/next-config-js) | Configuration file for Next.js | | [`next.config.js`](/docs/app/api-reference/config/next-config-js) | Configuration file for Next.js |
| [`package.json`](/docs/app/getting-started/installation#manual-installation) | Project dependencies and scripts | | [`package.json`](/docs/app/getting-started/installation#manual-installation) | Project dependencies and scripts |
| [`instrumentation.ts`](/docs/app/guides/instrumentation) | OpenTelemetry and Instrumentation file | | [`instrumentation.ts`](/docs/app/guides/instrumentation) | OpenTelemetry and Instrumentation file |
| [`middleware.ts`](/docs/app/building-your-application/routing/middleware) | Next.js request middleware | | [`middleware.ts`](/docs/app/api-reference/file-conventions/middleware) | Next.js request middleware |
| [`.env`](/docs/app/guides/environment-variables) | Environment variables | | [`.env`](/docs/app/guides/environment-variables) | Environment variables |
| [`.env.local`](/docs/app/guides/environment-variables) | Local environment variables | | [`.env.local`](/docs/app/guides/environment-variables) | Local environment variables |
| [`.env.production`](/docs/app/guides/environment-variables) | Production environment variables | | [`.env.production`](/docs/app/guides/environment-variables) | Production environment variables |
......
...@@ -1023,7 +1023,7 @@ For both cases, we recommend: ...@@ -1023,7 +1023,7 @@ For both cases, we recommend:
### Optimistic checks with Middleware (Optional) ### Optimistic checks with Middleware (Optional)
There are some cases where you may want to use [Middleware](/docs/app/building-your-application/routing/middleware) and redirect users based on permissions: There are some cases where you may want to use [Middleware](/docs/app/api-reference/file-conventions/middleware) and redirect users based on permissions:
- To perform optimistic checks. Since Middleware runs on every route, it's a good way to centralize redirect logic and pre-filter unauthorized users. - To perform optimistic checks. Since Middleware runs on every route, it's a good way to centralize redirect logic and pre-filter unauthorized users.
- To protect static routes that share data between users (e.g. content behind a paywall). - To protect static routes that share data between users (e.g. content behind a paywall).
......
...@@ -31,7 +31,7 @@ Even though CSPs are designed to block malicious scripts, there are legitimate s ...@@ -31,7 +31,7 @@ Even though CSPs are designed to block malicious scripts, there are legitimate s
### Adding a nonce with Middleware ### Adding a nonce with Middleware
[Middleware](/docs/app/building-your-application/routing/middleware) enables you to add headers and generate nonces before the page renders. [Middleware](/docs/app/api-reference/file-conventions/middleware) enables you to add headers and generate nonces before the page renders.
Every time a page is viewed, a fresh nonce should be generated. This means that you **must use dynamic rendering to add nonces**. Every time a page is viewed, a fresh nonce should be generated. This means that you **must use dynamic rendering to add nonces**.
...@@ -124,7 +124,7 @@ export function middleware(request) { ...@@ -124,7 +124,7 @@ export function middleware(request) {
} }
``` ```
By default, Middleware runs on all requests. You can filter Middleware to run on specific paths using a [`matcher`](/docs/app/building-your-application/routing/middleware#matcher). By default, Middleware runs on all requests. You can filter Middleware to run on specific paths using a [`matcher`](/docs/app/api-reference/file-conventions/middleware#matcher).
We recommend ignoring matching prefetches (from `next/link`) and static assets that don't need the CSP header. We recommend ignoring matching prefetches (from `next/link`) and static assets that don't need the CSP header.
......
...@@ -30,7 +30,7 @@ let defaultLocale = 'en-US' ...@@ -30,7 +30,7 @@ let defaultLocale = 'en-US'
match(languages, locales, defaultLocale) // -> 'en-US' match(languages, locales, defaultLocale) // -> 'en-US'
``` ```
Routing can be internationalized by either the sub-path (`/fr/products`) or domain (`my-site.fr/products`). With this information, you can now redirect the user based on the locale inside [Middleware](/docs/app/building-your-application/routing/middleware). Routing can be internationalized by either the sub-path (`/fr/products`) or domain (`my-site.fr/products`). With this information, you can now redirect the user based on the locale inside [Middleware](/docs/app/api-reference/file-conventions/middleware).
```js filename="middleware.js" ```js filename="middleware.js"
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
......
...@@ -39,7 +39,7 @@ Depending on your needs, Next.js allows you to choose your data fetching strateg ...@@ -39,7 +39,7 @@ Depending on your needs, Next.js allows you to choose your data fetching strateg
### Middleware ### Middleware
[Next.js Middleware](/docs/app/building-your-application/routing/middleware) allows you to run code on the server before a request is completed. For instance, you can avoid a flash of unauthenticated content by redirecting a user to a login page in the middleware for authenticated-only pages. You can also use it for features like A/B testing, experimentation, and [internationalization](/docs/app/guides/internationalization). [Next.js Middleware](/docs/app/api-reference/file-conventions/middleware) allows you to run code on the server before a request is completed. For instance, you can avoid a flash of unauthenticated content by redirecting a user to a login page in the middleware for authenticated-only pages. You can also use it for features like A/B testing, experimentation, and [internationalization](/docs/app/guides/internationalization).
### Built-in Optimizations ### Built-in Optimizations
......
...@@ -39,7 +39,7 @@ Depending on your needs, Next.js allows you to choose your data fetching strateg ...@@ -39,7 +39,7 @@ Depending on your needs, Next.js allows you to choose your data fetching strateg
### Middleware ### Middleware
[Next.js Middleware](/docs/app/building-your-application/routing/middleware) allows you to run code on the server before a request is completed. This is especially useful to avoid having a flash of unauthenticated content when the user visits an authenticated-only page by redirecting the user to a login page. The middleware is also useful for experimentation and [internationalization](/docs/app/guides/internationalization). [Next.js Middleware](/docs/app/api-reference/file-conventions/middleware) allows you to run code on the server before a request is completed. This is especially useful to avoid having a flash of unauthenticated content when the user visits an authenticated-only page by redirecting the user to a login page. The middleware is also useful for experimentation and [internationalization](/docs/app/guides/internationalization).
### Built-in Optimizations ### Built-in Optimizations
......
...@@ -289,7 +289,7 @@ See the [`redirects` API reference](/docs/app/api-reference/config/next-config-j ...@@ -289,7 +289,7 @@ See the [`redirects` API reference](/docs/app/api-reference/config/next-config-j
> **Good to know**: > **Good to know**:
> >
> - `redirects` can return a 307 (Temporary Redirect) or 308 (Permanent Redirect) status code with the `permanent` option. > - `redirects` can return a 307 (Temporary Redirect) or 308 (Permanent Redirect) status code with the `permanent` option.
> - `redirects` may have a limit on platforms. For example, on Vercel, there's a limit of 1,024 redirects. To manage a large number of redirects (1000+), consider creating a custom solution using [Middleware](/docs/app/building-your-application/routing/middleware). See [managing redirects at scale](#managing-redirects-at-scale-advanced) for more. > - `redirects` may have a limit on platforms. For example, on Vercel, there's a limit of 1,024 redirects. To manage a large number of redirects (1000+), consider creating a custom solution using [Middleware](/docs/app/api-reference/file-conventions/middleware). See [managing redirects at scale](#managing-redirects-at-scale-advanced) for more.
> - `redirects` runs **before** Middleware. > - `redirects` runs **before** Middleware.
## `NextResponse.redirect` in Middleware ## `NextResponse.redirect` in Middleware
...@@ -344,7 +344,7 @@ export const config = { ...@@ -344,7 +344,7 @@ export const config = {
> >
> - Middleware runs **after** `redirects` in `next.config.js` and **before** rendering. > - Middleware runs **after** `redirects` in `next.config.js` and **before** rendering.
See the [Middleware](/docs/app/building-your-application/routing/middleware) documentation for more information. See the [Middleware](/docs/app/api-reference/file-conventions/middleware) documentation for more information.
## Managing redirects at scale (advanced) ## Managing redirects at scale (advanced)
...@@ -376,7 +376,7 @@ Consider the following data structure: ...@@ -376,7 +376,7 @@ Consider the following data structure:
} }
``` ```
In [Middleware](/docs/app/building-your-application/routing/middleware), you can read from a database such as Vercel's [Edge Config](https://vercel.com/docs/edge-config/get-started) or [Redis](https://vercel.com/docs/redis), and redirect the user based on the incoming request: In [Middleware](/docs/app/api-reference/file-conventions/middleware), you can read from a database such as Vercel's [Edge Config](https://vercel.com/docs/edge-config/get-started) or [Redis](https://vercel.com/docs/redis), and redirect the user based on the incoming request:
```ts filename="middleware.ts" switcher ```ts filename="middleware.ts" switcher
import { NextResponse, NextRequest } from 'next/server' import { NextResponse, NextRequest } from 'next/server'
......
...@@ -24,7 +24,7 @@ Image Optimization can be used with a [static export](/docs/app/guides/static-ex ...@@ -24,7 +24,7 @@ Image Optimization can be used with a [static export](/docs/app/guides/static-ex
## Middleware ## Middleware
[Middleware](/docs/app/building-your-application/routing/middleware) works self-hosted with zero configuration when deploying using `next start`. Since it requires access to the incoming request, it is not supported when using a [static export](/docs/app/guides/static-exports). [Middleware](/docs/app/api-reference/file-conventions/middleware) works self-hosted with zero configuration when deploying using `next start`. Since it requires access to the incoming request, it is not supported when using a [static export](/docs/app/guides/static-exports).
Middleware uses the [Edge runtime](/docs/app/api-reference/edge), a subset of all available Node.js APIs to help ensure low latency, since it may run in front of every route or asset in your application. If you do not want this, you can use the [full Node.js runtime](/blog/next-15-2#nodejs-middleware-experimental) to run Middleware. Middleware uses the [Edge runtime](/docs/app/api-reference/edge), a subset of all available Node.js APIs to help ensure low latency, since it may run in front of every route or asset in your application. If you do not want this, you can use the [full Node.js runtime](/blog/next-15-2#nodejs-middleware-experimental) to run Middleware.
......
...@@ -284,7 +284,7 @@ Features that require a Node.js server, or dynamic logic that cannot be computed ...@@ -284,7 +284,7 @@ Features that require a Node.js server, or dynamic logic that cannot be computed
- [Rewrites](/docs/app/api-reference/config/next-config-js/rewrites) - [Rewrites](/docs/app/api-reference/config/next-config-js/rewrites)
- [Redirects](/docs/app/api-reference/config/next-config-js/redirects) - [Redirects](/docs/app/api-reference/config/next-config-js/redirects)
- [Headers](/docs/app/api-reference/config/next-config-js/headers) - [Headers](/docs/app/api-reference/config/next-config-js/headers)
- [Middleware](/docs/app/building-your-application/routing/middleware) - [Middleware](/docs/app/api-reference/file-conventions/middleware)
- [Incremental Static Regeneration](/docs/app/guides/incremental-static-regeneration) - [Incremental Static Regeneration](/docs/app/guides/incremental-static-regeneration)
- [Image Optimization](/docs/app/api-reference/components/image) with the default `loader` - [Image Optimization](/docs/app/api-reference/components/image) with the default `loader`
- [Draft Mode](/docs/app/guides/draft-mode) - [Draft Mode](/docs/app/guides/draft-mode)
...@@ -306,7 +306,7 @@ export const dynamic = 'error' ...@@ -306,7 +306,7 @@ export const dynamic = 'error'
- [Rewrites](/docs/pages/api-reference/config/next-config-js/rewrites) - [Rewrites](/docs/pages/api-reference/config/next-config-js/rewrites)
- [Redirects](/docs/pages/api-reference/config/next-config-js/redirects) - [Redirects](/docs/pages/api-reference/config/next-config-js/redirects)
- [Headers](/docs/pages/api-reference/config/next-config-js/headers) - [Headers](/docs/pages/api-reference/config/next-config-js/headers)
- [Middleware](/docs/pages/building-your-application/routing/middleware) - [Middleware](/docs/pages/api-reference/file-conventions/middleware)
- [Incremental Static Regeneration](/docs/pages/guides/incremental-static-regeneration) - [Incremental Static Regeneration](/docs/pages/guides/incremental-static-regeneration)
- [Image Optimization](/docs/pages/api-reference/components/image) with the default `loader` - [Image Optimization](/docs/pages/api-reference/components/image) with the default `loader`
- [Draft Mode](/docs/pages/guides/draft-mode) - [Draft Mode](/docs/pages/guides/draft-mode)
......
...@@ -1116,7 +1116,7 @@ export default function Home() { ...@@ -1116,7 +1116,7 @@ export default function Home() {
### Prefetching links in Middleware ### Prefetching links in Middleware
It's common to use [Middleware](/docs/app/building-your-application/routing/middleware) for authentication or other purposes that involve rewriting the user to a different page. In order for the `<Link />` component to properly prefetch links with rewrites via Middleware, you need to tell Next.js both the URL to display and the URL to prefetch. This is required to avoid un-necessary fetches to middleware to know the correct route to prefetch. It's common to use [Middleware](/docs/app/api-reference/file-conventions/middleware) for authentication or other purposes that involve rewriting the user to a different page. In order for the `<Link />` component to properly prefetch links with rewrites via Middleware, you need to tell Next.js both the URL to display and the URL to prefetch. This is required to avoid un-necessary fetches to middleware to know the correct route to prefetch.
For example, if you want to serve a `/dashboard` route that has authenticated and visitor views, you can add the following in your Middleware to redirect the user to the correct page: For example, if you want to serve a `/dashboard` route that has authenticated and visitor views, you can add the following in your Middleware to redirect the user to the correct page:
......
...@@ -13,4 +13,4 @@ Once a file is defined, Next.js will automatically serve the file (with hashes i ...@@ -13,4 +13,4 @@ Once a file is defined, Next.js will automatically serve the file (with hashes i
> **Good to know**: > **Good to know**:
> >
> - Special Route Handlers like [`sitemap.ts`](/docs/app/api-reference/file-conventions/metadata/sitemap), [`opengraph-image.tsx`](/docs/app/api-reference/file-conventions/metadata/opengraph-image), and [`icon.tsx`](/docs/app/api-reference/file-conventions/metadata/app-icons), and other [metadata files](/docs/app/api-reference/file-conventions/metadata) are cached by default. > - Special Route Handlers like [`sitemap.ts`](/docs/app/api-reference/file-conventions/metadata/sitemap), [`opengraph-image.tsx`](/docs/app/api-reference/file-conventions/metadata/opengraph-image), and [`icon.tsx`](/docs/app/api-reference/file-conventions/metadata/app-icons), and other [metadata files](/docs/app/api-reference/file-conventions/metadata) are cached by default.
> - If using along with [`middleware.ts`](/docs/app/api-reference/file-conventions/middleware), [configure the matcher](/docs/app/building-your-application/routing/middleware#matcher) to exclude the metadata files. > - If using along with [`middleware.ts`](/docs/app/api-reference/file-conventions/middleware), [configure the matcher](/docs/app/api-reference/file-conventions/middleware#matcher) to exclude the metadata files.
...@@ -528,7 +528,7 @@ export async function GET(request) { ...@@ -528,7 +528,7 @@ export async function GET(request) {
> **Good to know**: > **Good to know**:
> >
> - To add CORS headers to multiple Route Handlers, you can use [Middleware](/docs/app/building-your-application/routing/middleware#cors) or the [`next.config.js` file](/docs/app/api-reference/config/next-config-js/headers#cors). > - To add CORS headers to multiple Route Handlers, you can use [Middleware](/docs/app/api-reference/file-conventions/middleware#cors) or the [`next.config.js` file](/docs/app/api-reference/config/next-config-js/headers#cors).
> - Alternatively, see our [CORS example](https://github.com/vercel/examples/blob/main/edge-functions/cors/lib/cors.ts) package. > - Alternatively, see our [CORS example](https://github.com/vercel/examples/blob/main/edge-functions/cors/lib/cors.ts) package.
### Webhooks ### Webhooks
......
...@@ -5,7 +5,7 @@ description: API Reference for the after function. ...@@ -5,7 +5,7 @@ description: API Reference for the after function.
`after` allows you to schedule work to be executed after a response (or prerender) is finished. This is useful for tasks and other side effects that should not block the response, such as logging and analytics. `after` allows you to schedule work to be executed after a response (or prerender) is finished. This is useful for tasks and other side effects that should not block the response, such as logging and analytics.
It can be used in [Server Components](/docs/app/getting-started/server-and-client-components) (including [`generateMetadata`](https://nextjs.org/docs/app/api-reference/functions/generate-metadata)), [Server Actions](/docs/app/getting-started/updating-data), [Route Handlers](/docs/app/building-your-application/routing/route-handlers), and [Middleware](/docs/app/building-your-application/routing/middleware). It can be used in [Server Components](/docs/app/getting-started/server-and-client-components) (including [`generateMetadata`](https://nextjs.org/docs/app/api-reference/functions/generate-metadata)), [Server Actions](/docs/app/getting-started/updating-data), [Route Handlers](/docs/app/building-your-application/routing/route-handlers), and [Middleware](/docs/app/api-reference/file-conventions/middleware).
The function accepts a callback that will be executed after the response (or prerender) is finished: The function accepts a callback that will be executed after the response (or prerender) is finished:
......
...@@ -1065,17 +1065,17 @@ export const metadata = { ...@@ -1065,17 +1065,17 @@ export const metadata = {
} }
``` ```
| Metadata | Recommendation | | Metadata | Recommendation |
| ----------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `<meta http-equiv="...">` | Use appropriate HTTP Headers via [`redirect()`](/docs/app/api-reference/functions/redirect), [Middleware](/docs/app/building-your-application/routing/middleware#nextresponse), [Security Headers](/docs/app/api-reference/config/next-config-js/headers) | | `<meta http-equiv="...">` | Use appropriate HTTP Headers via [`redirect()`](/docs/app/api-reference/functions/redirect), [Middleware](/docs/app/api-reference/file-conventions/middleware#nextresponse), [Security Headers](/docs/app/api-reference/config/next-config-js/headers) |
| `<base>` | Render the tag in the layout or page itself. | | `<base>` | Render the tag in the layout or page itself. |
| `<noscript>` | Render the tag in the layout or page itself. | | `<noscript>` | Render the tag in the layout or page itself. |
| `<style>` | Learn more about [styling in Next.js](/docs/app/getting-started/css). | | `<style>` | Learn more about [styling in Next.js](/docs/app/getting-started/css). |
| `<script>` | Learn more about [using scripts](/docs/app/guides/scripts). | | `<script>` | Learn more about [using scripts](/docs/app/guides/scripts). |
| `<link rel="stylesheet" />` | `import` stylesheets directly in the layout or page itself. | | `<link rel="stylesheet" />` | `import` stylesheets directly in the layout or page itself. |
| `<link rel="preload />` | Use [ReactDOM preload method](#link-relpreload) | | `<link rel="preload />` | Use [ReactDOM preload method](#link-relpreload) |
| `<link rel="preconnect" />` | Use [ReactDOM preconnect method](#link-relpreconnect) | | `<link rel="preconnect" />` | Use [ReactDOM preconnect method](#link-relpreconnect) |
| `<link rel="dns-prefetch" />` | Use [ReactDOM prefetchDNS method](#link-reldns-prefetch) | | `<link rel="dns-prefetch" />` | Use [ReactDOM prefetchDNS method](#link-reldns-prefetch) |
### Resource hints ### Resource hints
......
...@@ -36,7 +36,7 @@ For example, a Client Component with `usePathname` will be rendered into HTML on ...@@ -36,7 +36,7 @@ For example, a Client Component with `usePathname` will be rendered into HTML on
> - Reading the current URL from a [Server Component](/docs/app/getting-started/server-and-client-components) is not supported. This design is intentional to support layout state being preserved across page navigations. > - Reading the current URL from a [Server Component](/docs/app/getting-started/server-and-client-components) is not supported. This design is intentional to support layout state being preserved across page navigations.
> - Compatibility mode: > - Compatibility mode:
> - `usePathname` can return `null` when a [fallback route](/docs/pages/api-reference/functions/get-static-paths#fallback-true) is being rendered or when a `pages` directory page has been [automatically statically optimized](/docs/pages/building-your-application/rendering/automatic-static-optimization) by Next.js and the router is not ready. > - `usePathname` can return `null` when a [fallback route](/docs/pages/api-reference/functions/get-static-paths#fallback-true) is being rendered or when a `pages` directory page has been [automatically statically optimized](/docs/pages/building-your-application/rendering/automatic-static-optimization) by Next.js and the router is not ready.
> - When using `usePathname` with rewrites in [`next.config`](/docs/app/api-reference/config/next-config-js/rewrites) or [`Middleware`](/docs/app/building-your-application/routing/middleware), `useState` and `useEffect` must also be used in order to avoid hydration mismatch errors. > - When using `usePathname` with rewrites in [`next.config`](/docs/app/api-reference/config/next-config-js/rewrites) or [`Middleware`](/docs/app/api-reference/file-conventions/middleware), `useState` and `useEffect` must also be used in order to avoid hydration mismatch errors.
> - Next.js will automatically update your types if it detects both an `app` and `pages` directory in your project. > - Next.js will automatically update your types if it detects both an `app` and `pages` directory in your project.
## Parameters ## Parameters
......
...@@ -34,7 +34,7 @@ module.exports = { ...@@ -34,7 +34,7 @@ module.exports = {
Changing these values affects _all_ Next.js pages, including the following: Changing these values affects _all_ Next.js pages, including the following:
- [`middleware.js`](/docs/pages/building-your-application/routing/middleware) - [`middleware.js`](/docs/pages/api-reference/file-conventions/middleware)
- [`instrumentation.js`](/docs/pages/guides/instrumentation) - [`instrumentation.js`](/docs/pages/guides/instrumentation)
- `pages/_document.js` - `pages/_document.js`
- `pages/_app.js` - `pages/_app.js`
......
...@@ -38,7 +38,7 @@ module.exports = { ...@@ -38,7 +38,7 @@ module.exports = {
Redirects are checked before the filesystem which includes pages and `/public` files. Redirects are checked before the filesystem which includes pages and `/public` files.
When using the Pages Router, redirects are not applied to client-side routing (`Link`, `router.push`) unless [Middleware](/docs/app/building-your-application/routing/middleware) is present and matches the path. When using the Pages Router, redirects are not applied to client-side routing (`Link`, `router.push`) unless [Middleware](/docs/app/api-reference/file-conventions/middleware) is present and matches the path.
When a redirect is applied, any query values provided in the request will be passed through to the redirect destination. For example, see the following redirect configuration: When a redirect is applied, any query values provided in the request will be passed through to the redirect destination. For example, see the following redirect configuration:
......
...@@ -144,7 +144,7 @@ When using Sub-path Routing, the user would be redirected to `/fr`. ...@@ -144,7 +144,7 @@ When using Sub-path Routing, the user would be redirected to `/fr`.
### Prefixing the Default Locale ### Prefixing the Default Locale
With Next.js 12 and [Middleware](/docs/pages/building-your-application/routing/middleware), we can add a prefix to the default locale with a [workaround](https://github.com/vercel/next.js/discussions/18419). With Next.js 12 and [Middleware](/docs/pages/api-reference/file-conventions/middleware), we can add a prefix to the default locale with a [workaround](https://github.com/vercel/next.js/discussions/18419).
For example, here's a `next.config.js` file with support for a few languages. Note the `"default"` locale has been added intentionally. For example, here's a `next.config.js` file with support for a few languages. Note the `"default"` locale has been added intentionally.
...@@ -159,7 +159,7 @@ module.exports = { ...@@ -159,7 +159,7 @@ module.exports = {
} }
``` ```
Next, we can use [Middleware](/docs/pages/building-your-application/routing/middleware) to add custom routing rules: Next, we can use [Middleware](/docs/pages/api-reference/file-conventions/middleware) to add custom routing rules:
```ts filename="middleware.ts" ```ts filename="middleware.ts"
import { NextRequest, NextResponse } from 'next/server' import { NextRequest, NextResponse } from 'next/server'
...@@ -185,7 +185,7 @@ export async function middleware(req: NextRequest) { ...@@ -185,7 +185,7 @@ export async function middleware(req: NextRequest) {
} }
``` ```
This [Middleware](/docs/pages/building-your-application/routing/middleware) skips adding the default prefix to [API Routes](/docs/pages/building-your-application/routing/api-routes) and [public](/docs/pages/api-reference/file-conventions/public-folder) files like fonts or images. If a request is made to the default locale, we redirect to our prefix `/en`. This [Middleware](/docs/pages/api-reference/file-conventions/middleware) skips adding the default prefix to [API Routes](/docs/pages/building-your-application/routing/api-routes) and [public](/docs/pages/api-reference/file-conventions/public-folder) files like fonts or images. If a request is made to the default locale, we redirect to our prefix `/en`.
### Disabling Automatic Locale Detection ### Disabling Automatic Locale Detection
...@@ -352,4 +352,4 @@ export async function getStaticProps({ locale }) { ...@@ -352,4 +352,4 @@ export async function getStaticProps({ locale }) {
- `locales`: 100 total locales - `locales`: 100 total locales
- `domains`: 100 total locale domain items - `domains`: 100 total locale domain items
> **Good to know**: These limits have been added initially to prevent potential [performance issues at build time](#dynamic-routes-and-getstaticprops-pages). You can workaround these limits with custom routing using [Middleware](/docs/pages/building-your-application/routing/middleware) in Next.js 12. > **Good to know**: These limits have been added initially to prevent potential [performance issues at build time](#dynamic-routes-and-getstaticprops-pages). You can workaround these limits with custom routing using [Middleware](/docs/pages/api-reference/file-conventions/middleware) in Next.js 12.
...@@ -65,7 +65,7 @@ You should use `getStaticProps` if: ...@@ -65,7 +65,7 @@ You should use `getStaticProps` if:
When combined with [Incremental Static Regeneration](/docs/pages/guides/incremental-static-regeneration), `getStaticProps` will run in the background while the stale page is being revalidated, and the fresh page served to the browser. When combined with [Incremental Static Regeneration](/docs/pages/guides/incremental-static-regeneration), `getStaticProps` will run in the background while the stale page is being revalidated, and the fresh page served to the browser.
`getStaticProps` does not have access to the incoming request (such as query parameters or HTTP headers) as it generates static HTML. If you need access to the request for your page, consider using [Middleware](/docs/pages/building-your-application/routing/middleware) in addition to `getStaticProps`. `getStaticProps` does not have access to the incoming request (such as query parameters or HTTP headers) as it generates static HTML. If you need access to the request for your page, consider using [Middleware](/docs/pages/api-reference/file-conventions/middleware) in addition to `getStaticProps`.
## Using getStaticProps to fetch data from a CMS ## Using getStaticProps to fetch data from a CMS
......
--- ---
title: Middleware title: Middleware
description: Learn how to use Middleware to run code before a request is completed. description: Learn how to use Middleware to run code before a request is completed.
source: app/building-your-application/routing/middleware source: app/api-reference/file-conventions/middleware
--- ---
{/* DO NOT EDIT. The content of this doc is generated from the source above. To edit the content of this page, navigate to the source page in your editor. 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. */} {/* DO NOT EDIT. The content of this doc is generated from the source above. To edit the content of this page, navigate to the source page in your editor. 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. */}
...@@ -5,7 +5,7 @@ description: This document clarifies the "Beta Middleware Used" error in Next.js ...@@ -5,7 +5,7 @@ description: This document clarifies the "Beta Middleware Used" error in Next.js
## Why This Error Occurred ## Why This Error Occurred
The "Beta Middleware Used" error occurs if you are using [Middleware](/docs/pages/building-your-application/routing/middleware) in a version of Next.js prior to `v12.2`. The Middleware feature was still in beta in those versions and not yet covered by [semver](https://semver.org/) or Semantic Versioning rules. The "Beta Middleware Used" error occurs if you are using [Middleware](/docs/pages/api-reference/file-conventions/middleware) in a version of Next.js prior to `v12.2`. The Middleware feature was still in beta in those versions and not yet covered by [semver](https://semver.org/) or Semantic Versioning rules.
## Possible Ways to Fix It ## Possible Ways to Fix It
......
...@@ -22,4 +22,4 @@ export function middleware(req: NextRequest, ev: NextFetchEvent) { ...@@ -22,4 +22,4 @@ export function middleware(req: NextRequest, ev: NextFetchEvent) {
## Useful Links ## Useful Links
- [Middleware](/docs/pages/building-your-application/routing/middleware) - [Middleware](/docs/pages/api-reference/file-conventions/middleware)
...@@ -4,7 +4,7 @@ title: Using Node.js Modules in Edge Runtime ...@@ -4,7 +4,7 @@ title: Using Node.js Modules in Edge Runtime
## Why This Error Occurred ## Why This Error Occurred
The code in your [Middleware](/docs/pages/building-your-application/routing/middleware) or your [Edge API Routes](/docs/pages/building-your-application/routing/api-routes) is using a feature from Node.js runtime. The code in your [Middleware](/docs/pages/api-reference/file-conventions/middleware) or your [Edge API Routes](/docs/pages/building-your-application/routing/api-routes) is using a feature from Node.js runtime.
However, the Edge Runtime does not support [Node.js APIs and globals](/docs/pages/api-reference/edge#unsupported-apis). However, the Edge Runtime does not support [Node.js APIs and globals](/docs/pages/api-reference/edge#unsupported-apis).
...@@ -17,5 +17,5 @@ For example, you might replace the Node.js `crypto` module with the [Web Crypto ...@@ -17,5 +17,5 @@ For example, you might replace the Node.js `crypto` module with the [Web Crypto
## Useful Links ## Useful Links
- [Edge Runtime Supported APIs](/docs/pages/api-reference/edge) - [Edge Runtime Supported APIs](/docs/pages/api-reference/edge)
- [Next.js Middleware](/docs/pages/building-your-application/routing/middleware) - [Next.js Middleware](/docs/pages/api-reference/file-conventions/middleware)
- [JWT Example](https://vercel.com/templates/next.js/jwt-authentication) - [JWT Example](https://vercel.com/templates/next.js/jwt-authentication)
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
title: Returning response body in Middleware title: Returning response body in Middleware
--- ---
> **Note**: In Next.js v13.0.0 you can now respond to Middleware directly by returning a `NextResponse`. For more information, see [Producing a Response](/docs/pages/building-your-application/routing/middleware#producing-a-response). > **Note**: In Next.js v13.0.0 you can now respond to Middleware directly by returning a `NextResponse`. For more information, see [Producing a Response](/docs/pages/api-reference/file-conventions/middleware#producing-a-response).
## Why This Error Occurred ## Why This Error Occurred
[Middleware](/docs/pages/building-your-application/routing/middleware) can no longer produce a response body as of `v12.2+`. [Middleware](/docs/pages/api-reference/file-conventions/middleware) can no longer produce a response body as of `v12.2+`.
## Possible Ways to Fix It ## Possible Ways to Fix It
...@@ -14,7 +14,7 @@ Migrate to using `rewrite`/`redirect` to Pages/API Routes handling a response. ...@@ -14,7 +14,7 @@ Migrate to using `rewrite`/`redirect` to Pages/API Routes handling a response.
### Explanation ### Explanation
To respect the differences in client-side and server-side navigation, and to help ensure that developers do not build insecure Middleware, Middleware can no longer produce a response body. This ensures that Middleware is only used to `rewrite`, `redirect`, or modify the incoming request (e.g. [setting cookies](/docs/pages/building-your-application/routing/middleware#using-cookies)). To respect the differences in client-side and server-side navigation, and to help ensure that developers do not build insecure Middleware, Middleware can no longer produce a response body. This ensures that Middleware is only used to `rewrite`, `redirect`, or modify the incoming request (e.g. [setting cookies](/docs/pages/api-reference/file-conventions/middleware#using-cookies)).
The following patterns will no longer work: The following patterns will no longer work:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment