Skip to content

Commit

Permalink
fix: update geolocation examples to next 15 (#973)
Browse files Browse the repository at this point in the history
### Description

Since we're using canary, the current geolocation examples are broken.
This PR updates the geolocation examples since `NextRequest.geo` was
removed in Next 15.

I opted to use the `@vercel/functions` package as recommended in the
[Upgrade
guide](https://nextjs.org/docs/app/building-your-application/upgrading/version-15#nextrequest-geolocation),
however we could always refactor to using the raw headers.

### Type of Change

- [x] Example updates (Bug fixes, new features, etc.)
  • Loading branch information
jamsch authored Nov 13, 2024
1 parent d88c90e commit 15f108d
Show file tree
Hide file tree
Showing 17 changed files with 8,494 additions and 6,782 deletions.
3 changes: 2 additions & 1 deletion edge-middleware/geolocation-country-block/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ marketplace: false
# Geolocation Country Block

```ts
import { geolocation } from '@vercel/functions'
import type { NextRequest } from 'next/server'

// Block Austria, prefer Germany
const BLOCKED_COUNTRY = 'AT'

export default function middleware(req: NextRequest) {
const country = req.geo.country || 'US'
const country = geolocation(req).country || 'US'

if (country === BLOCKED_COUNTRY) {
return new Response('Blocked for legal reasons', { status: 451 })
Expand Down
5 changes: 3 additions & 2 deletions edge-middleware/geolocation-country-block/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { geolocation } from '@vercel/functions'
import type { NextRequest } from 'next/server'

import { NextResponse } from 'next/server'

// Block Austria, prefer Germany
Expand All @@ -11,8 +11,9 @@ export const config = {
}

export default function middleware(req: NextRequest) {
const geo = geolocation(req)
// Extract country
const country = req.geo.country || 'US'
const country = geo.country || 'US'

// Specify the correct pathname
if (country === BLOCKED_COUNTRY) {
Expand Down
1 change: 1 addition & 0 deletions edge-middleware/geolocation-country-block/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"lint": "next lint"
},
"dependencies": {
"@vercel/functions": "^1.5.0",
"next": "canary",
"react": "latest",
"react-dom": "latest"
Expand Down
Loading

0 comments on commit 15f108d

Please sign in to comment.