Skip to content

Commit

Permalink
Remove IP address example
Browse files Browse the repository at this point in the history
- See #68379
- Additionally, people can read the headers available in their system, we don't have to document them individually in the API for accessing them.
  • Loading branch information
delbaoliveira committed Sep 13, 2024
1 parent b1e21a7 commit 4122da1
Showing 1 changed file with 2 additions and 35 deletions.
37 changes: 2 additions & 35 deletions docs/02-app/02-api-reference/04-functions/headers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default async function Page() {

### Using the Authorization header

````jsx filename="app/page.js"
```jsx filename="app/page.js"
import { headers } from 'next/headers'

export default async function Page() {
Expand All @@ -65,40 +65,7 @@ export default async function Page() {

return <h1>{user.name}</h1>
}
#### IP Address

`headers()` can be used to get the IP address of the client.

```jsx filename="app/page.js"
import { Suspense } from 'react'
import { headers } from 'next/headers'
function IP() {
const FALLBACK_IP_ADDRESS = '0.0.0.0'
const forwardedFor = headers().get('x-forwarded-for')
if (forwardedFor) {
return forwardedFor.split(',')[0] ?? FALLBACK_IP_ADDRESS
}
return headers().get('x-real-ip') ?? FALLBACK_IP_ADDRESS
}
export default function Page() {
return (
<Suspense fallback={null}>
<IP />
</Suspense>
)
}
````
In addition to `x-forwarded-for`, `headers()` can also read:
- `x-real-ip`
- `x-forwarded-host`
- `x-forwarded-port`
- `x-forwarded-proto`
```

## Version History

Expand Down

0 comments on commit 4122da1

Please sign in to comment.