MCPcopy Create free account
hub / github.com/vercel/examples / middleware

Function middleware

edge-middleware/user-agent-based-rendering/middleware.ts:8–20  ·  view source on GitHub ↗
(req: NextRequest)

Source from the content-addressed store, hash-verified

6}
7
8export default function middleware(req: NextRequest) {
9 // Parse user agent
10 const { device } = userAgent(req)
11
12 // Check the viewport
13 const viewport = device.type === 'mobile' ? 'mobile' : 'desktop'
14
15 // Update the expected url
16 req.nextUrl.pathname = `_viewport/${viewport}`
17
18 // Return rewrited response
19 return NextResponse.rewrite(req.nextUrl)
20}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected