@fortedigital/nextjs-cache-handler
Version:
Next.js cache handlers
86 lines (83 loc) • 2.97 kB
text/typescript
import { CacheHandler } from '../handlers/cache-handler.cjs';
import 'next/dist/server/lib/incremental-cache';
import '../handlers/cache-handler.types.cjs';
import 'next/dist/server/lib/incremental-cache/file-system-cache';
import 'next/dist/server/response-cache/types';
type CacheHandlerType = typeof CacheHandler;
/**
* Options for the `registerInitialCache` instrumentation.
*/
type RegisterInitialCacheOptions = {
/**
* Whether to populate the cache with fetch calls.
*
* @default true
*/
fetch?: boolean;
/**
* Whether to populate the cache with pre-rendered pages.
*
* @default true
*/
pages?: boolean;
/**
* Whether to populate the cache with routes.
*
* @default true
*/
routes?: boolean;
/**
* Override the default build directory.
*
* @default .next
*/
buildDir?: string;
/**
* The maximum number of concurrent operations.
* This speeds up the initial cache population because routes are read and processed in parallel.
* The default value is either `os.availableParallelism()` (i.e., in most cases the number of CPU cores) or,
* since most Next.js instances only have a single CPU core, 4, whichever is higher.
* Depending on your specific needs, this value can be adjusted to optimize the startup performance.
* By supplying 1, you can disable parallelism and run all operations sequentially.
*
* @default Math.max(4, os.availableParallelism())
*/
parallelism?: number;
};
/**
* Populates the cache with the initial data.
*
* By default, it includes the following:
* - Pre-rendered pages
* - Routes
* - Fetch calls
*
* @param CacheHandler - The configured CacheHandler class, not an instance.
*
* @param [options={}] - Options for the instrumentation. See {@link RegisterInitialCacheOptions}.
*
* @param [options.fetch=true] - Whether to populate the cache with fetch calls.
*
* @param [options.pages=true] - Whether to populate the cache with pre-rendered pages.
*
* @param [options.routes=true] - Whether to populate the cache with routes.
*
* @example file: `instrumentation.ts`
*
* ```js
* export async function register() {
* if (process.env.NEXT_RUNTIME === 'nodejs') {
* const { registerInitialCache } = await import('@fortedigital/nextjs-cache-handler/instrumentation');
* // Assuming that your CacheHandler configuration is in the root of the project and the instrumentation is in the src directory.
* // Please adjust the path accordingly.
* // CommonJS CacheHandler configuration is also supported.
* const CacheHandler = (await import('../cache-handler.mjs')).default;
* await registerInitialCache(CacheHandler);
* }
* }
* ```
*
*
*/
declare function registerInitialCache(CacheHandler: CacheHandlerType, options?: RegisterInitialCacheOptions): Promise<void>;
export { type RegisterInitialCacheOptions, registerInitialCache };