NextJS getUrl for cloudflare

Rating
Slug
Tags
Web Development
Software Engineering
NextJS
Cloudflare
Publish Date
export function getBaseUrl() { if (process.env.NEXT_PUBLIC_SITE_URL) { return process.env.NEXT_PUBLIC_SITE_URL } if (process.env.VERCEL_URL) { return `https://${process.env.VERCEL_URL}` } if (process.env.CF_PAGES_URL) { return `https://${process.env.CF_PAGES_URL}` } return `http://localhost:${process.env.PORT ?? 3000}` }
# .env NEXT_PUBLIC_SITE_URL=https://yourdomain
 
 
  • This should handle NextJS deployed in both vercel and cloudflare pages (next-on-page).