Skip to content

Instantly share code, notes, and snippets.

View z4nr34l's full-sized avatar
💻
QmVlcC1ib29wLCBib29wLWJlZXA=

Mateusz Janota z4nr34l

💻
QmVlcC1ib29wLCBib29wLWJlZXA=
View GitHub Profile
@z4nr34l
z4nr34l / next-real-ip.ts
Created April 10, 2024 08:49
Simple function to fetch users real IP address in nextjs app on vercel including cloudflare proxy
/**
* The fallback IP address to use if the real IP address cannot be determined.
*/
const FALLBACK_IP_ADDRESS = '0.0.0.0';
/**
* Returns the real IP address of the client.
* @param request - The incoming request.
* @param cfProxy - Whether the client is behind a Cloudflare proxy.
* @returns The real IP address of the client.
@z4nr34l
z4nr34l / middleware.ts
Created January 7, 2024 19:03
Next.js default middleware with path matching
// example code source: https://github.com/vercel/next.js/blob/canary/examples/middleware/middleware.ts
import { NextRequest, NextResponse } from 'next/server'
export function middleware(request: NextRequest) {
if (request.nextUrl.pathname === '/about') {
return NextResponse.redirect(new URL('/redirected', request.url))
}
if (request.nextUrl.pathname === '/another') {
return NextResponse.rewrite(new URL('/rewrite', request.url))