Registry / devops / next-cache-effective-pages

next-cache-effective-pages

JSON →
library1.8.0jsnpmunverified

A Next.js helper for creating cache-effective SSR pages with minimal effort (v1.8.0). It simplifies generating regeneratable static-like pages (e.g., sitemaps) with effective caching via Cache-Control headers and built-in protection against bandwidth attacks by stripping query parameters that defeat caching. Key differentiators: zero dependencies, lightweight (small bundle size), works with Next.js >=9, ships TypeScript types, and allows whitelisting specific query params. Release cadence is irregular, latest update 2024.

npm install next-cache-effective-pages
INSTALL
IMPORT
SIG · NEXT-CACHE-EFFECTI
N
next-cache-effective-pages
devopsjavascriptv1.8.0
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.

withCacheEffectivePage
import { withCacheEffectivePage } from 'next-cache-effective-pages'
const withCacheEffectivePage = require('next-cache-effective-pages')
ESM-only; package does not export a CommonJS version.
WithCacheEffectivePageOptions
import type { WithCacheEffectivePageOptions } from 'next-cache-effective-pages'
import { WithCacheEffectivePageOptions } from 'next-cache-effective-pages'
Type-only import; the interface is not a runtime value.
CacheEffectivePageResult
import type { CacheEffectivePageResult } from 'next-cache-effective-pages'
import { CacheEffectivePageResult } from 'next-cache-effective-pages'
Type-only import; the type is not exported as a runtime value.

Shows how to create a cache-effective sitemap page that revalidates every 15 minutes with bandwidth attack protection.

// pages/sitemap.xml.tsx import { withCacheEffectivePage } from 'next-cache-effective-pages'; interface Ctx { res: import('http').ServerResponse; } export default function Sitemap() {} export async function getServerSideProps(ctx: Parameters<typeof withCacheEffectivePage>[0] extends (args: infer P) => any ? P : never) { return withCacheEffectivePage(async ({ res }: Ctx) => { res.setHeader('Content-Type', 'text/xml'); const posts = await fetch('https://api.example.com/posts').then(r => r.json()); res.write(`<?xml version="1.0" encoding="UTF-8"?><urlset>${posts.map((p: any) => `<url><loc>https://example.com/posts/${p.id}</loc></url>`).join('')}</urlset>`); res.end(); })({ ...ctx, options: { secondsBeforeRevalidation: 60 * 15 } }); }
Debug
Known issues
breakingThe function signature changed in v1.0.0: options now must be passed in the context object, not as second argument.
fix
Pass options via `{...ctx, options: {...}}` instead of `withCacheEffectivePage(fn)(ctx, options)`.
affects: <1.0.0
deprecatedThe `secondsBeforeRevalidation` option is now optional; defaults to 0 (no cache). In future versions, it may become required.
fix
Always provide `secondsBeforeRevalidation` for predictable caching.
affects: >=1.0.0
gotchaThe library strips all query parameters except those in `allowedQueryParams`. This may break pages relying on query params for logic.
fix
Add required param names to `allowedQueryParams` array in options.
affects: >=1.0.0
gotchaThe exported component must be a default export and named `default`; otherwise, Next.js will not invoke `getServerSideProps`.
fix
Ensure the page component is default-exported (e.g., `export default function Sitemap() {}`).
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: withCacheEffectivePage is not a function
Incorrect import: using `const { withCacheEffectivePage } = require('next-cache-effective-pages')` in a CommonJS context. The package is ESM-only.
fix
Use `import { withCacheEffectivePage } from 'next-cache-effective-pages'` in an ES module.
Error: getServerSideProps must return a promise. Received undefined.
The function passed to `withCacheEffectivePage` did not call `res.end()` or return a value.
fix
Ensure the callback calls `res.end()` or returns a result that resolves to a valid props object.
Warning: 'secondsBeforeRevalidation' is not set; defaulting to 0 (no cache).
Missing `secondsBeforeRevalidation` option in the context.
fix
Add `options: { secondsBeforeRevalidation: <number> }` to the context object.
Upgrade
Version history
1.8.0latest on npm
Audit
Dependencies
nextrequiredPeer dependency: required to provide server-side rendering context
reactrequiredPeer dependency: required by Next.js for page rendering
react-domrequiredPeer dependency: required by Next.js for DOM rendering
Agent activity
2 hits · last 30 days
node
2
Resources
next-cache-effective-pages — npm install next-cache-effective-pages · libregistry