Unverified Commit a7a0dca7 authored by Joseph's avatar Joseph Committed by GitHub

docs: note per-route cost of boundary files, cached Route Handler checks, and…

docs: note per-route cost of boundary files, cached Route Handler checks, and per-instance rate limits (#100985)

## Summary

- `not-found.mdx`, `forbidden.mdx`, `unauthorized.mdx`: each of these
files is sent with every route under its segment, even when the route
never calls `notFound()`, `forbidden()`, or `unauthorized()`, and the
Client Components it imports load on those pages. For the root
`app/not-found.js`, that is every page of the app.
- `backend-for-frontend.mdx`, rate limiting: a counter kept in memory
applies per server instance and resets on restart.
- `backend-for-frontend.mdx`, access to protected resources: a response
cached with `Cache-Control: public` can be served by a CDN without
running the handler, so its checks do not apply to cache hits.

Commits:

- docs: note boundary files ship with every route, cached Route Handler
responses skip checks, and in-memory rate limits are per instance

<!-- NEXT_JS_LLM -->
parent 07bf5809
...@@ -830,6 +830,8 @@ export async function POST(request) { ...@@ -830,6 +830,8 @@ export async function POST(request) {
} }
``` ```
A rate limiter that keeps its counts in memory tracks each server instance separately, and the counts reset when an instance restarts. When your app runs on more than one instance, keep the counts in a shared data store or use your host's rate limiting.
### Verify payloads ### Verify payloads
Never trust incoming request data. Validate content type and size, and sanitize against XSS before use. Never trust incoming request data. Validate content type and size, and sanitize against XSS before use.
...@@ -842,6 +844,8 @@ Store user-generated static assets in dedicated services. When possible, upload ...@@ -842,6 +844,8 @@ Store user-generated static assets in dedicated services. When possible, upload
Always verify credentials before granting access. Do not rely on proxy alone for authentication and authorization. Always verify credentials before granting access. Do not rely on proxy alone for authentication and authorization.
Checks in a Route Handler run only when the handler runs. When a response sets `Cache-Control: public` with a `max-age` or `s-maxage`, a CDN can store it and serve later requests for the same URL without calling the handler. Which headers a CDN reads, and what it includes in the cache key, depends on your host. Credential checks and rate limits do not apply to requests served from the cache, so cache only responses that are safe to serve to anyone with the URL.
Remove sensitive or unnecessary data from responses and backend logs. Remove sensitive or unnecessary data from responses and backend logs.
Rotate credentials and API keys regularly. Rotate credentials and API keys regularly.
......
...@@ -45,6 +45,8 @@ export default function Forbidden() { ...@@ -45,6 +45,8 @@ export default function Forbidden() {
`forbidden.js` components do not accept any props. `forbidden.js` components do not accept any props.
> **Good to know**: Next.js sends each `forbidden.js` file with every route under its segment, even when `forbidden()` is never called, and loads the Client Components it imports on those pages. Prefer Server Components and [`<Link>`](/docs/app/api-reference/components/link) there over interactive UI.
## Version History ## Version History
| Version | Changes | | Version | Changes |
......
...@@ -132,7 +132,10 @@ Unlike `not-found.js`, this file must return a full HTML document, including `<h ...@@ -132,7 +132,10 @@ Unlike `not-found.js`, this file must return a full HTML document, including `<h
`not-found.js` or `global-not-found.js` components do not accept any props. `not-found.js` or `global-not-found.js` components do not accept any props.
> **Good to know**: In addition to catching expected `notFound()` errors, the root `app/not-found.js` and `app/global-not-found.js` files handle any unmatched URLs for your whole application. This means users that visit a URL that is not handled by your app will be shown the exported UI. > **Good to know**:
>
> - In addition to catching expected `notFound()` errors, the root `app/not-found.js` and `app/global-not-found.js` files handle any unmatched URLs for your whole application. This means users that visit a URL that is not handled by your app will be shown the exported UI.
> - Next.js sends each `not-found.js` file with every route under its segment, not only with pages that are not found, and loads the Client Components it imports on those pages. For the root `app/not-found.js`, that is every page of your app, so prefer Server Components and [`<Link>`](/docs/app/api-reference/components/link) there over interactive UI.
## Examples ## Examples
......
...@@ -45,6 +45,8 @@ export default function Unauthorized() { ...@@ -45,6 +45,8 @@ export default function Unauthorized() {
`unauthorized.js` components do not accept any props. `unauthorized.js` components do not accept any props.
> **Good to know**: Next.js sends each `unauthorized.js` file with every route under its segment, even when `unauthorized()` is never called, and loads the Client Components it imports on those pages. Prefer Server Components and [`<Link>`](/docs/app/api-reference/components/link) there over interactive UI.
## Examples ## Examples
### Displaying login UI to unauthenticated users ### Displaying login UI to unauthenticated users
......
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