Registry / devops / cached-middleware-fetch-next

cached-middleware-fetch-next

JSON →
library0.8.2jsnpmunverified

A Next.js fetch wrapper for edge middleware that uses Vercel Runtime Cache as its caching backend. Current stable version is 0.8.2. It provides a drop-in replacement for the native fetch API, mimicking Next.js's Data Cache behavior in edge middleware where standard Data Cache is unavailable. Features include support for Next.js fetch options (cache, next.revalidate, next.tags), SWR (Stale-While-Revalidate) caching strategy via waitUntil(), automatic cache key generation including body for POST/PUT, GraphQL query caching, cache status headers, and graceful fallback. Requires @vercel/functions >=2.2.0 and next >=14.0.0 as peer dependencies, and Node >=18.0.0. Ships TypeScript types. The library addresses a known limitation of Next.js on Vercel (https://github.com/vercel/next.js/pull/80378) where fetch caching is not available in edge middleware.

npm install cached-middleware-fetch-next
INSTALL
IMPORT
SIG · CACHED-MIDDLEWARE-
C
cached-middleware-fetch-next
devopsjavascriptv0.8.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

cachedFetch
import { cachedFetch } from 'cached-middleware-fetch-next'
import cachedFetch from 'cached-middleware-fetch-next'
Named export only. Default import will not work.
defineCacheOptions
import { defineCacheOptions } from 'cached-middleware-fetch-next'
Utility function for defining cache options with TypeScript inference.
CacheOptions
import type { CacheOptions } from 'cached-middleware-fetch-next'
import { CacheOptions } from 'cached-middleware-fetch-next'
TypeScript type, use type import for tree-shaking.

Demonstrates basic usage of cachedFetch in Next.js middleware with revalidation, tags, and cache status headers.

import { cachedFetch } from 'cached-middleware-fetch-next'; export async function middleware(request: NextRequest) { // Cached fetch with revalidation every 60 seconds const response = await cachedFetch('https://api.example.com/data', { next: { revalidate: 60, tags: ['api-data'] } }); if (!response.ok) { return NextResponse.next(); // fallback } const data = await response.json(); // Add cache status header for debugging const newResponse = NextResponse.next(); newResponse.headers.set('X-Cache-Status', response.headers.get('X-Cache-Status') ?? 'MISS'); return newResponse; }
Debug
Known issues
breakingVercel Runtime Cache is cleared on every deployment, invalidating all cached entries.
fix
Plan for cache invalidation on deployment. Use tags for selective revalidation via waitUntil.
affects: >=0.0.0
breakingRequires @vercel/functions peer dependency to be installed and available at runtime.
fix
Run 'npm install @vercel/functions' and ensure it's deployed with the application.
affects: >=0.0.0
deprecatedThe 'cache' option with value 'force-cache' is the default; explicit use is unnecessary and may be deprecated in future.
fix
Omit the 'cache' option or use 'cache: 'force-cache'' only if overriding a global default.
affects: >=0.5.0
gotchaPOST request caching includes the request body in the cache key; ensure body is deterministic or avoid caching POST with side effects.
fix
For POST requests that change server state, do not use caching or set 'cache: 'no-store''.
affects: >=0.7.0
gotchaThe SWR strategy uses waitUntil() which may cause unexpected behavior in serverless environments if not handled properly.
fix
Ensure your environment supports waitUntil() (Vercel Edge Functions do). Avoid using in environments where waitUntil is unsupported.
affects: >=0.8.0
gotchaGraphQL caching: POST requests with different queries are cached separately, but multiple identical queries should use same body shape.
fix
Ensure GraphQL request bodies are exactly identical for cache hits; include query and variables consistently.
affects: >=0.8.0
Errors
Common errors & fixes
Error: Cannot find module '@vercel/functions'
@vercel/functions peer dependency not installed.
fix
npm install @vercel/functions
TypeError: cachedFetch is not a function
Default import used instead of named import.
fix
Replace 'import cachedFetch from ...' with 'import { cachedFetch } from ...'
Error: next.revalidate must be a number or false, got undefined
Missing or improperly formatted next.revalidate option.
fix
Provide a valid number (seconds) or false: { next: { revalidate: 60 } } or { next: { revalidate: false } }
Error: waitUntil is not defined
Environment does not support waitUntil (e.g., older Node.js version or non-Vercel environment).
fix
Upgrade Node to >=18.0.0 or ensure deployment on Vercel Edge Functions. Alternatively, avoid SWR options.
Upgrade
Version history
0.8.2latest on npm
Audit
Dependencies
@vercel/functionsrequiredPeer dependency required for Vercel Runtime Cache integration
nextrequiredPeer dependency required for Next.js extended fetch API compatibility (next: { revalidate, tags })
Agent activity
6 hits · last 30 days
node
6
Resources