Replies: 2 comments
-
Hey there! Looks like middleware caching is not shared between middleware and rendering layers. Currently, middleware isolation means they don't share cache. If you're looking for shared caching, setting There’s no official shared cache yet—but suggest it might be a good enhancement! Let me know if you want ideas for implementing one. |
Beta Was this translation helpful? Give feedback.
-
Hi, Middleware runs in its own "process" if you will. Imagine if it did share the same resources as the process also handling requests, then it'd be a true bottleneck. Also fetch patching is done only when Next.js is just about to render your components, or Route Handlers. I should also say that, relying on this fetch caching level for anything else than Next.js' own features is also probably not what you want. ISR basically. If what you want is browser like caching, etc, you probably want to use the interceptor API in Node.js. AFAIK, as per spec, fetch true low level caching is optional, so while browsers have it, Node.js' undici implementation doesn't, but there's a way to apply it, using the interceptors API. Note I haven't tried this in prod, but you can read more about it here, https://undici.nodejs.org/#/docs/api/Dispatcher?id=cache-interceptor, Node.js' undici docs. import { type NextRequest, NextResponse } from 'next/server'
import { interceptors, Agent, setGlobalDispatcher } from 'undici'
const dispatcher = new Agent().compose(interceptors.cache())
setGlobalDispatcher(dispatcher)
async function fetchData() {
const res = await fetch("http://localhost:3000")
const data = await res.json()
console.log(data)
return data
}
export default async function middleware(request: NextRequest) {
const data = await fetchData()
return NextResponse.json(data)
}
export const config = {
matcher: '/',
runtime: 'nodejs'
} Screen.Recording.2025-08-25.at.10.48.55.movIn this case the server does: res.writeHead(200, {
'Content-Type': 'application/json',
'Cache-Control': 'public, max-age=5' // cache for 5 seconds
}); So the undici cache interceptor, reuses the same response for 5 seconds. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Hi,
After the Next.js 15.5 update, I saw that middleware is now supported in the Node.js runtime. I was curious about whether caching would be supported in this environment, but I couldn’t find any information.
So, I created a small test repo to verify this.
/cache
and check the logsHere’s an example of the logs I got.
From these logs, it looks like:
My current assumption is that middleware and rendering are isolated in the Node.js runtime, something like this:
What I’d like to know is:
Is there any plan to make these unisolated, so middleware and rendering can share cache? For example, something like this:
Thanks in advance for clarifying!
Additional information
No response
Example
https://github.com/sijunnoh/next-request-test
Beta Was this translation helpful? Give feedback.
All reactions