Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion examples/cache-handler-redis/cache-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ CacheHandler.onCreation(async () => {
// NB do not throw exceptions in the redis error listener,
// because it will prevent reconnection after a socket exception.
client.on("error", (e) => {
if (typeof process.env.NEXT_PRIVATE_DEBUG_CACHE !== "undefined") {
// Check if debug logging is enabled with proper truthy value check
const debugValue = process.env.NEXT_PRIVATE_DEBUG_CACHE
const isDebugEnabled = debugValue &&
['1', 'true', 'yes'].includes(debugValue.toLowerCase().trim())

if (isDebugEnabled) {
console.warn("Redis error", e);
}
});
Expand Down
5 changes: 3 additions & 2 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isDebugCacheEnabled } from '../lib/debug-utils'
import type { RenderOpts, PreloadCallbacks } from './types'
import type {
ActionResult,
Expand Down Expand Up @@ -1798,7 +1799,7 @@ async function renderToHTMLOrFlightImpl(
workStore.pendingRevalidatedTags
) {
const pendingPromise = executeRevalidates(workStore).finally(() => {
if (process.env.NEXT_PRIVATE_DEBUG_CACHE) {
if (isDebugCacheEnabled()) {
console.log('pending revalidates promise finished for:', url)
}
})
Expand Down Expand Up @@ -1966,7 +1967,7 @@ async function renderToHTMLOrFlightImpl(
workStore.pendingRevalidatedTags
) {
const pendingPromise = executeRevalidates(workStore).finally(() => {
if (process.env.NEXT_PRIVATE_DEBUG_CACHE) {
if (isDebugCacheEnabled()) {
console.log('pending revalidates promise finished for:', url)
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* considered expired/missing in such cache handlers.
*/

import { isDebugCacheEnabled } from '../debug-utils'
import { LRUCache } from '../lru-cache'
import type { CacheEntry, CacheHandlerV2 } from './types'
import {
Expand Down Expand Up @@ -41,7 +42,7 @@ const memoryCache = new LRUCache<PrivateCacheEntry>(
)
const pendingSets = new Map<string, Promise<void>>()

const debug = process.env.NEXT_PRIVATE_DEBUG_CACHE
const debug = isDebugCacheEnabled()
? console.debug.bind(console, 'DefaultCacheHandler:')
: undefined

Expand Down
91 changes: 91 additions & 0 deletions packages/next/src/server/lib/debug-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { isDebugCacheEnabled } from './debug-utils'

describe('isDebugCacheEnabled', () => {
const originalEnv = process.env.NEXT_PRIVATE_DEBUG_CACHE

afterEach(() => {
// Restore original value
if (originalEnv === undefined) {
delete process.env.NEXT_PRIVATE_DEBUG_CACHE
} else {
process.env.NEXT_PRIVATE_DEBUG_CACHE = originalEnv
}
})

it('should return true for "1"', () => {
process.env.NEXT_PRIVATE_DEBUG_CACHE = '1'
expect(isDebugCacheEnabled()).toBe(true)
})

it('should return true for "true" (case-insensitive)', () => {
process.env.NEXT_PRIVATE_DEBUG_CACHE = 'true'
expect(isDebugCacheEnabled()).toBe(true)

process.env.NEXT_PRIVATE_DEBUG_CACHE = 'TRUE'
expect(isDebugCacheEnabled()).toBe(true)

process.env.NEXT_PRIVATE_DEBUG_CACHE = 'True'
expect(isDebugCacheEnabled()).toBe(true)
})

it('should return true for "yes" (case-insensitive)', () => {
process.env.NEXT_PRIVATE_DEBUG_CACHE = 'yes'
expect(isDebugCacheEnabled()).toBe(true)

process.env.NEXT_PRIVATE_DEBUG_CACHE = 'YES'
expect(isDebugCacheEnabled()).toBe(true)

process.env.NEXT_PRIVATE_DEBUG_CACHE = 'Yes'
expect(isDebugCacheEnabled()).toBe(true)
})

it('should handle whitespace correctly', () => {
process.env.NEXT_PRIVATE_DEBUG_CACHE = ' true '
expect(isDebugCacheEnabled()).toBe(true)

process.env.NEXT_PRIVATE_DEBUG_CACHE = ' 1 '
expect(isDebugCacheEnabled()).toBe(true)
})

it('should return false for "0"', () => {
process.env.NEXT_PRIVATE_DEBUG_CACHE = '0'
expect(isDebugCacheEnabled()).toBe(false)
})

it('should return false for "false" (case-insensitive)', () => {
process.env.NEXT_PRIVATE_DEBUG_CACHE = 'false'
expect(isDebugCacheEnabled()).toBe(false)

process.env.NEXT_PRIVATE_DEBUG_CACHE = 'FALSE'
expect(isDebugCacheEnabled()).toBe(false)
})

it('should return false for "no" (case-insensitive)', () => {
process.env.NEXT_PRIVATE_DEBUG_CACHE = 'no'
expect(isDebugCacheEnabled()).toBe(false)

process.env.NEXT_PRIVATE_DEBUG_CACHE = 'NO'
expect(isDebugCacheEnabled()).toBe(false)
})

it('should return false when undefined', () => {
delete process.env.NEXT_PRIVATE_DEBUG_CACHE
expect(isDebugCacheEnabled()).toBe(false)
})

it('should return false for empty string', () => {
process.env.NEXT_PRIVATE_DEBUG_CACHE = ''
expect(isDebugCacheEnabled()).toBe(false)
})

it('should return false for arbitrary values', () => {
process.env.NEXT_PRIVATE_DEBUG_CACHE = 'arbitrary'
expect(isDebugCacheEnabled()).toBe(false)

process.env.NEXT_PRIVATE_DEBUG_CACHE = 'debug'
expect(isDebugCacheEnabled()).toBe(false)

process.env.NEXT_PRIVATE_DEBUG_CACHE = '2'
expect(isDebugCacheEnabled()).toBe(false)
})
})
17 changes: 17 additions & 0 deletions packages/next/src/server/lib/debug-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Check if debug logging is enabled based on the NEXT_PRIVATE_DEBUG_CACHE environment variable.
* Debug logging is enabled only if the variable is set to one of: '1', 'true', 'yes' (case-insensitive).
* It is disabled if set to '0', 'false', 'no', or unset.
*
* @returns true if debug logging should be enabled, false otherwise
*/
export function isDebugCacheEnabled(): boolean {
const value = process.env.NEXT_PRIVATE_DEBUG_CACHE

if (!value) {
return false
}

const normalizedValue = value.toLowerCase().trim()
return normalizedValue === '1' || normalizedValue === 'true' || normalizedValue === 'yes'
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isDebugCacheEnabled } from '../debug-utils'
import type { RouteMetadata } from '../../../export/routes/types'
import type { CacheHandler, CacheHandlerContext, CacheHandlerValue } from '.'
import type { CacheFs } from '../../../shared/lib/utils'
Expand Down Expand Up @@ -38,7 +39,7 @@ export default class FileSystemCache implements CacheHandler {
private flushToDisk?: FileSystemCacheContext['flushToDisk']
private serverDistDir: FileSystemCacheContext['serverDistDir']
private revalidatedTags: string[]
private static debug: boolean = !!process.env.NEXT_PRIVATE_DEBUG_CACHE
private static debug: boolean = isDebugCacheEnabled()
private static memoryCache: LRUCache<CacheHandlerValue> | undefined

constructor(ctx: FileSystemCacheContext) {
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/server/lib/incremental-cache/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isDebugCacheEnabled } from '../debug-utils'
import type { CacheFs } from '../../../shared/lib/utils'
import type { PrerenderManifest } from '../../../build'
import {
Expand Down Expand Up @@ -93,8 +94,7 @@ export class IncrementalCache implements IncrementalCacheType {
readonly isOnDemandRevalidate?: boolean
readonly revalidatedTags?: readonly string[]

private static readonly debug: boolean =
!!process.env.NEXT_PRIVATE_DEBUG_CACHE
private static readonly debug: boolean = isDebugCacheEnabled()
private readonly locks = new Map<string, Promise<void>>()

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/next/src/server/route-modules/app-route/module.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isDebugCacheEnabled } from '../../lib/debug-utils'
import type { NextConfig } from '../../config-shared'
import type { AppRouteRouteDefinition } from '../../route-definitions/app-route-route-definition'
import type { AppSegmentConfig } from '../../../build/segment-config/app/app-segment-config'
Expand Down Expand Up @@ -333,7 +334,7 @@ export class AppRouteRouteModule extends RouteModule<
context.renderOpts.pendingWaitUntil = executeRevalidates(
workStore
).finally(() => {
if (process.env.NEXT_PRIVATE_DEBUG_CACHE) {
if (isDebugCacheEnabled()) {
console.log(
'pending revalidates promise finished for:',
requestStore.url
Expand Down
3 changes: 2 additions & 1 deletion packages/next/src/server/use-cache/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import DefaultCacheHandler from '../lib/cache-handlers/default.external'
import type { CacheHandlerCompat } from '../lib/cache-handlers/types'
import { isDebugCacheEnabled } from '../lib/debug-utils'

const debug = process.env.NEXT_PRIVATE_DEBUG_CACHE
const debug = isDebugCacheEnabled()
? (message: string, ...args: any[]) => {
console.log(`use-cache: ${message}`, ...args)
}
Expand Down
4 changes: 3 additions & 1 deletion packages/next/src/server/use-cache/use-cache-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ export type UseCacheLayoutComponentProps = {
[slot: string]: any
}

import { isDebugCacheEnabled } from '../lib/debug-utils'

const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'

const debug = process.env.NEXT_PRIVATE_DEBUG_CACHE
const debug = isDebugCacheEnabled()
? console.debug.bind(console, 'use-cache:')
: undefined

Expand Down
Loading