Skip to content

Commit

Permalink
always use default export for middleware function (#966)
Browse files Browse the repository at this point in the history
### Description


https://vercel.com/docs/functions/edge-middleware/middleware-api#edge-middleware-setup

Although Next.js can still recognize non-default export middleware
function, it's good to be consistent across examples.
  • Loading branch information
smaeda-ks authored Oct 23, 2024
1 parent d92f98d commit 0c03e43
Show file tree
Hide file tree
Showing 20 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion edge-middleware/ab-testing-google-optimize/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const config = {
matcher: ['/marketing', '/about'],
}

export function middleware(req: NextRequest) {
export default function middleware(req: NextRequest) {
let cookie = req.cookies.get(COOKIE_NAME)?.value

if (!cookie) {
Expand Down
2 changes: 1 addition & 1 deletion edge-middleware/add-header/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Below is the code from [middleware.ts](middleware.ts) showing how to add respons
```ts
import { NextResponse } from 'next/server'

export function middleware() {
export default function middleware() {
// Store the response so we can modify its headers
const response = NextResponse.next()

Expand Down
2 changes: 1 addition & 1 deletion edge-middleware/add-header/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextResponse } from 'next/server'

export function middleware() {
export default function middleware() {
// Store the response so we can modify its headers
const response = NextResponse.next()

Expand Down
2 changes: 1 addition & 1 deletion edge-middleware/basic-auth-password/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const config = {
matcher: ['/', '/index'],
}

export function middleware(req: NextRequest) {
export default function middleware(req: NextRequest) {
const basicAuth = req.headers.get('authorization')
const url = req.nextUrl

Expand Down
2 changes: 1 addition & 1 deletion edge-middleware/cookies/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const config = {
matcher: '/',
}

export function middleware(req: NextRequest) {
export default function middleware(req: NextRequest) {
// Parse the cookie
const isInBeta = JSON.parse(req.cookies.get('beta')?.value || 'false')

Expand Down
2 changes: 1 addition & 1 deletion edge-middleware/cookies/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const config = {
matcher: '/',
}

export function middleware(req: NextRequest) {
export default function middleware(req: NextRequest) {
// Parse the cookie
const isInBeta = JSON.parse(req.cookies.get('beta')?.value || 'false')

Expand Down
2 changes: 1 addition & 1 deletion edge-middleware/crypto/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const config = {
matcher: '/',
}

export function middleware(req: NextRequest) {
export default function middleware(req: NextRequest) {
const token = crypto.randomUUID()
const url = req.nextUrl

Expand Down
2 changes: 1 addition & 1 deletion edge-middleware/geolocation-country-block/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { NextRequest } from 'next/server'
// Block Austria, prefer Germany
const BLOCKED_COUNTRY = 'AT'

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

if (country === BLOCKED_COUNTRY) {
Expand Down
2 changes: 1 addition & 1 deletion edge-middleware/geolocation-country-block/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const config = {
matcher: '/',
}

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

Expand Down
2 changes: 1 addition & 1 deletion edge-middleware/image-response/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const config = {
matcher: '/',
}

export function middleware(req: NextRequest) {
export default function middleware(req: NextRequest) {
req.nextUrl.pathname = '/api/img'
return NextResponse.rewrite(req.nextUrl)
}
2 changes: 1 addition & 1 deletion edge-middleware/json-response/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ marketplace: false
```ts
import type { NextRequest } from 'next/server'

export function middleware(req: NextRequest) {
export default function middleware(req: NextRequest) {
return new Response(JSON.stringify({ message: 'hello world!' }), {
status: 200,
headers: {
Expand Down
2 changes: 1 addition & 1 deletion edge-middleware/json-response/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export const config = {
matcher: '/',
}

export function middleware() {
export default function middleware() {
return NextResponse.json({ message: 'hello world!' })
}
2 changes: 1 addition & 1 deletion edge-middleware/modify-request-header/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Below is the code from [middleware.ts](middleware.ts) showing how to add/update/
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

export function middleware(request: NextRequest) {
export default function middleware(request: NextRequest) {
// Clone the request headers
// You can modify them with headers API: https://developer.mozilla.org/en-US/docs/Web/API/Headers
const requestHeaders = new Headers(request.headers)
Expand Down
2 changes: 1 addition & 1 deletion edge-middleware/modify-request-header/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

export function middleware(request: NextRequest) {
export default function middleware(request: NextRequest) {
// Clone the request headers
// You can modify them with headers API: https://developer.mozilla.org/en-US/docs/Web/API/Headers
const requestHeaders = new Headers(request.headers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const config = {
matcher: '/edge',
}

export function middleware(req: NextRequest) {
export default function middleware(req: NextRequest) {
// Get country
const country = req.geo.country?.toLowerCase() || 'us'

Expand Down
2 changes: 1 addition & 1 deletion edge-middleware/power-parity-pricing/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const config = {
matcher: '/',
}

export function middleware(req: NextRequest) {
export default function middleware(req: NextRequest) {
// Get country
const country = req.geo.country?.toLowerCase() || 'us'

Expand Down
2 changes: 1 addition & 1 deletion edge-middleware/query-params-filter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const config = {
matcher: '/',
}

export function middleware(req: NextRequest) {
export default function middleware(req: NextRequest) {
const url = req.nextUrl
let changed = false

Expand Down
2 changes: 1 addition & 1 deletion edge-middleware/query-params-filter/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const config = {
matcher: '/',
}

export function middleware(req: NextRequest) {
export default function middleware(req: NextRequest) {
const url = req.nextUrl
let changed = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const config = {
matcher: '/',
}
export function middleware(req) {
export default function middleware(req) {
// Parse user agent
const { device } = userAgent(req)
Expand Down
2 changes: 1 addition & 1 deletion edge-middleware/user-agent-based-rendering/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const config = {
matcher: '/',
}

export function middleware(req: NextRequest) {
export default function middleware(req: NextRequest) {
// Parse user agent
const { device } = userAgent(req)

Expand Down

0 comments on commit 0c03e43

Please sign in to comment.