Content SDK + Next.js App Router: middleware that actually works (even when you develop locally)
The Content SDK with the Next.js App Router is a solid step forward. Finally App Router support, React Server Components, streaming—inside a Sitecore head application. Sitecore’s documentation on this is genuinely decent.
But the moment you think, “Cool, I’ll just run this locally in containers like I’ve been doing for years”… you get the digital equivalent of a door slammed in your face.
The problem: local containers and App Router don’t (yet) play nice
I was messing around with the Content SDK and the SitecoreAI starterkit. Plan: run everything locally in containers so multiple devs can serialize items without stepping on each other’s toes.
Reality: local containers don’t work anymore in combination with the App Router beta. Jeroen Breuer ran into the same thing.
His fix got me closer, but it didn’t fully work for our setup. So I built an alternative solution.
Why this happens (in normal human language)
The Content SDK App Router setup leans heavily on Next.js middleware. Middleware runs on every request and gets compiled in the Edge runtime. Next.js is pretty clear about this: keep middleware lean, modular, and route-aware, or you’ll wreck performance and predictability.
In the default Content SDK setup, some middleware gets instantiated eagerly—even if you don’t need it locally at all. And locally you usually don’t have an Edge context, but you do have sites.json, redirects, personalize config, locale routing… basically the perfect recipe for “it works on my machine.” Except now it doesn’t. Irony included for free.
The fix: lazy middleware chain + Edge detection
I had AI make a small but meaningful adjustment:
- Locale middleware always first and always on.
In multi-language setups, you want locale set before multisite/redirects/personalize tries anything. - Only instantiate Edge-only middleware if you’re actually in Edge mode.
Don’t call constructors “just in case.” - Build the middleware chain dynamically.
No unnecessary work in local/dev.