Skip to main content

One post tagged with "nextjs"

View All Tags

Handling Affiliate Cookies and Internationalized Routing in Next.js Middleware

· 3 min read
samundrak
JavaScript Dev

When building complex Next.js applications, middleware becomes an essential place to handle cross-cutting concerns such as authentication, localization, and tracking. One common challenge is setting cookies conditionally while ensuring that the middleware returns the correct routing response—especially when dealing with internationalized routing (i18n).

In this article, I’ll walk you through a practical example where we:

  • Set an affiliate tracking cookie based on the request referer
  • Use next-i18n-router middleware to route users based on locale
  • Avoid common pitfalls that cause 404s or missing cookies