Replies: 2 comments 3 replies
|
I had drafted this a while ago: https://github.com/vercel/next.js/pull/89933/changes — which is not ideal, more of a workaround. Let me check with a colleague who might have more input. |
|
Current behavior is:
The handler loader already uses dynamic What Next does not do is run handler files through the A modern ESM setup can look like this: // cache-handlers/remote-handler.mts
import type {
CacheEntry,
CacheHandler,
} from "next/dist/server/lib/cache-handlers/types"
const cache = new Map<string, CacheEntry>()
const handler: CacheHandler = {
async get(key) {
return cache.get(key)
},
async set(key, pendingEntry) {
cache.set(key, await pendingEntry)
},
async refreshTags() {},
async getExpiration() {
return 0
},
async updateTags() {},
}
export default handlerCompile // next.config.mjs
import { fileURLToPath } from "node:url"
export default {
cacheHandlers: {
remote: fileURLToPath(
new URL("./cache-handlers/dist/remote-handler.mjs", import.meta.url),
),
},
}Run the handler compile step from One caveat: So the CommonJS examples are a documentation limitation, not an ESM runtime limitation. Direct TypeScript loading is the unsupported part. |
Uh oh!
There was an error while loading. Please reload this page.
Summary
All the examples of custom
cacheHandlerson https://nextjs.org/docs/app/api-reference/config/next-config-js/cacheHandlers seem to be written as awful, old school CommonJS stuff. The configuration refers to them viarequire.resolve, the file itself is.js, and it usesmodule.exportsandrequire.We're in 2026, can I really not write a custom cache handler for Next in Typescript using ESM?
Additional information
No response
Example
No response
All reactions