Unverified Commit b05c26f2 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add handling for custom cache handlers (#71505)

Follow-up to https://github.com/vercel/next.js/pull/71455 this adds
wiring up for the cache handlers config and exposing it to
use-cache-wrapper. Also adds mapping of remote -> default cache handler
for dev.
parent afebda3f
...@@ -441,6 +441,7 @@ export function getEdgeServerEntry(opts: { ...@@ -441,6 +441,7 @@ export function getEdgeServerEntry(opts: {
JSON.stringify(opts.middlewareConfig || {}) JSON.stringify(opts.middlewareConfig || {})
).toString('base64'), ).toString('base64'),
serverActions: opts.config.experimental.serverActions, serverActions: opts.config.experimental.serverActions,
cacheHandlers: JSON.stringify(opts.config.experimental.cacheHandlers || {}),
} }
return { return {
......
...@@ -2175,6 +2175,7 @@ export default async function build( ...@@ -2175,6 +2175,7 @@ export default async function build(
pageType, pageType,
dynamicIO: isAppDynamicIOEnabled, dynamicIO: isAppDynamicIOEnabled,
cacheHandler: config.cacheHandler, cacheHandler: config.cacheHandler,
cacheHandlers: config.experimental.cacheHandlers,
isrFlushToDisk: ciEnvironment.hasNextSupport isrFlushToDisk: ciEnvironment.hasNextSupport
? false ? false
: config.experimental.isrFlushToDisk, : config.experimental.isrFlushToDisk,
......
...@@ -36,7 +36,8 @@ export async function loadEntrypoint( ...@@ -36,7 +36,8 @@ export async function loadEntrypoint(
| 'pages-api', | 'pages-api',
replacements: Record<`VAR_${string}`, string>, replacements: Record<`VAR_${string}`, string>,
injections?: Record<string, string>, injections?: Record<string, string>,
imports?: Record<string, string | null> imports?: Record<string, string | null>,
importMaps?: Record<string, Record<string, string>>
): Promise<string> { ): Promise<string> {
const filepath = path.resolve( const filepath = path.resolve(
path.join(TEMPLATES_ESM_FOLDER, `${entrypoint}.js`) path.join(TEMPLATES_ESM_FOLDER, `${entrypoint}.js`)
...@@ -44,6 +45,28 @@ export async function loadEntrypoint( ...@@ -44,6 +45,28 @@ export async function loadEntrypoint(
let file = await fs.readFile(filepath, 'utf8') let file = await fs.readFile(filepath, 'utf8')
const importMapItems: Record<string, Record<string, string>> = {}
for (const key of Object.keys(importMaps || {})) {
importMapItems[key] = {}
for (const [innerKey, importPath] of Object.entries(
importMaps?.[key] || {}
)) {
file = `import ${key}_${innerKey} from "${importPath}"\n${file}`
importMapItems[key][innerKey] = `${key}_${innerKey}`
}
}
file = file.replace(
new RegExp(`cacheHandlers = {}`),
`cacheHandlers = {\n${Object.entries(importMapItems['cacheHandlers'] || {})
.map(([key, value]) => {
return `${key}: ${value}`
})
.join(',')}\n}`
)
// Update the relative imports to be absolute. This will update any relative // Update the relative imports to be absolute. This will update any relative
// imports to be relative to the root of the `next` package. // imports to be relative to the root of the `next` package.
let count = 0 let count = 0
......
...@@ -17,6 +17,12 @@ import { createServerModuleMap } from '../../server/app-render/action-utils' ...@@ -17,6 +17,12 @@ import { createServerModuleMap } from '../../server/app-render/action-utils'
declare const incrementalCacheHandler: any declare const incrementalCacheHandler: any
// OPTIONAL_IMPORT:incrementalCacheHandler // OPTIONAL_IMPORT:incrementalCacheHandler
const cacheHandlers = {}
if (!(globalThis as any).__nextCacheHandlers) {
;(globalThis as any).__nextCacheHandlers = cacheHandlers
}
const Document: DocumentType = null! const Document: DocumentType = null!
const appMod = null const appMod = null
const errorMod = null const errorMod = null
......
...@@ -40,6 +40,12 @@ declare const user500RouteModuleOptions: any ...@@ -40,6 +40,12 @@ declare const user500RouteModuleOptions: any
// INJECT:errorRouteModuleOptions // INJECT:errorRouteModuleOptions
// INJECT:user500RouteModuleOptions // INJECT:user500RouteModuleOptions
const cacheHandlers = {}
if (!(globalThis as any).__nextCacheHandlers) {
;(globalThis as any).__nextCacheHandlers = cacheHandlers
}
const pageMod = { const pageMod = {
...userlandPage, ...userlandPage,
routeModule: new RouteModule({ routeModule: new RouteModule({
......
...@@ -99,6 +99,7 @@ import type { OutgoingHttpHeaders } from 'http' ...@@ -99,6 +99,7 @@ import type { OutgoingHttpHeaders } from 'http'
import type { AppSegmentConfig } from './segment-config/app/app-segment-config' import type { AppSegmentConfig } from './segment-config/app/app-segment-config'
import type { AppSegment } from './segment-config/app/app-segments' import type { AppSegment } from './segment-config/app/app-segments'
import { collectSegments } from './segment-config/app/app-segments' import { collectSegments } from './segment-config/app/app-segments'
import { createIncrementalCache } from '../export/helpers/create-incremental-cache'
export type ROUTER_TYPE = 'pages' | 'app' export type ROUTER_TYPE = 'pages' | 'app'
...@@ -1490,6 +1491,7 @@ export async function isPageStatic({ ...@@ -1490,6 +1491,7 @@ export async function isPageStatic({
maxMemoryCacheSize, maxMemoryCacheSize,
nextConfigOutput, nextConfigOutput,
cacheHandler, cacheHandler,
cacheHandlers,
cacheLifeProfiles, cacheLifeProfiles,
pprConfig, pprConfig,
buildId, buildId,
...@@ -1511,6 +1513,7 @@ export async function isPageStatic({ ...@@ -1511,6 +1513,7 @@ export async function isPageStatic({
isrFlushToDisk?: boolean isrFlushToDisk?: boolean
maxMemoryCacheSize?: number maxMemoryCacheSize?: number
cacheHandler?: string cacheHandler?: string
cacheHandlers?: Record<string, string | undefined>
cacheLifeProfiles?: { cacheLifeProfiles?: {
[profile: string]: import('../server/use-cache/cache-life').CacheLife [profile: string]: import('../server/use-cache/cache-life').CacheLife
} }
...@@ -1518,6 +1521,16 @@ export async function isPageStatic({ ...@@ -1518,6 +1521,16 @@ export async function isPageStatic({
pprConfig: ExperimentalPPRConfig | undefined pprConfig: ExperimentalPPRConfig | undefined
buildId: string buildId: string
}): Promise<PageIsStaticResult> { }): Promise<PageIsStaticResult> {
await createIncrementalCache({
cacheHandler,
cacheHandlers,
distDir,
dir,
dynamicIO,
flushToDisk: isrFlushToDisk,
cacheMaxMemorySize: maxMemoryCacheSize,
})
const isPageStaticSpan = trace('is-page-static-utils', parentId) const isPageStaticSpan = trace('is-page-static-utils', parentId)
return isPageStaticSpan return isPageStaticSpan
.traceAsyncFn(async (): Promise<PageIsStaticResult> => { .traceAsyncFn(async (): Promise<PageIsStaticResult> => {
......
...@@ -24,6 +24,7 @@ export type EdgeSSRLoaderQuery = { ...@@ -24,6 +24,7 @@ export type EdgeSSRLoaderQuery = {
pagesType: PAGE_TYPES pagesType: PAGE_TYPES
sriEnabled: boolean sriEnabled: boolean
cacheHandler?: string cacheHandler?: string
cacheHandlers?: string
preferredRegion: string | string[] | undefined preferredRegion: string | string[] | undefined
middlewareConfig: string middlewareConfig: string
serverActions?: { serverActions?: {
...@@ -75,11 +76,14 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> = ...@@ -75,11 +76,14 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
pagesType, pagesType,
sriEnabled, sriEnabled,
cacheHandler, cacheHandler,
cacheHandlers: cacheHandlersStringified,
preferredRegion, preferredRegion,
middlewareConfig: middlewareConfigBase64, middlewareConfig: middlewareConfigBase64,
serverActions, serverActions,
} = this.getOptions() } = this.getOptions()
const cacheHandlers = JSON.parse(cacheHandlersStringified || '{}')
const middlewareConfig: MiddlewareConfig = JSON.parse( const middlewareConfig: MiddlewareConfig = JSON.parse(
Buffer.from(middlewareConfigBase64, 'base64').toString() Buffer.from(middlewareConfigBase64, 'base64').toString()
) )
...@@ -156,6 +160,9 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> = ...@@ -156,6 +160,9 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
}, },
{ {
incrementalCacheHandler: cacheHandler ?? null, incrementalCacheHandler: cacheHandler ?? null,
},
{
cacheHandlers: cacheHandlers ?? {},
} }
) )
} else { } else {
...@@ -184,6 +191,9 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> = ...@@ -184,6 +191,9 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
{ {
userland500Page: userland500Path, userland500Page: userland500Path,
incrementalCacheHandler: cacheHandler ?? null, incrementalCacheHandler: cacheHandler ?? null,
},
{
cacheHandlers: cacheHandlers || {},
} }
) )
} }
......
...@@ -13,6 +13,7 @@ export async function createIncrementalCache({ ...@@ -13,6 +13,7 @@ export async function createIncrementalCache({
distDir, distDir,
dir, dir,
flushToDisk, flushToDisk,
cacheHandlers,
}: { }: {
dynamicIO: boolean dynamicIO: boolean
cacheHandler?: string cacheHandler?: string
...@@ -21,6 +22,7 @@ export async function createIncrementalCache({ ...@@ -21,6 +22,7 @@ export async function createIncrementalCache({
distDir: string distDir: string
dir: string dir: string
flushToDisk?: boolean flushToDisk?: boolean
cacheHandlers?: Record<string, string | undefined>
}) { }) {
// Custom cache handler overrides. // Custom cache handler overrides.
let CacheHandler: any let CacheHandler: any
...@@ -32,6 +34,20 @@ export async function createIncrementalCache({ ...@@ -32,6 +34,20 @@ export async function createIncrementalCache({
) )
} }
if (!(globalThis as any).__nextCacheHandlers && cacheHandlers) {
;(globalThis as any).__nextCacheHandlers = {}
for (const key of Object.keys(cacheHandlers)) {
if (cacheHandlers[key]) {
;(globalThis as any).__nextCacheHandlers[key] = interopDefault(
await import(formatDynamicImportPath(dir, cacheHandlers[key])).then(
(mod) => mod.default || mod
)
)
}
}
}
const incrementalCache = new IncrementalCache({ const incrementalCache = new IncrementalCache({
dev: false, dev: false,
requestHeaders: {}, requestHeaders: {},
......
...@@ -361,6 +361,7 @@ export async function exportPages( ...@@ -361,6 +361,7 @@ export async function exportPages(
// skip writing to disk in minimal mode for now, pending some // skip writing to disk in minimal mode for now, pending some
// changes to better support it // changes to better support it
flushToDisk: !hasNextSupport, flushToDisk: !hasNextSupport,
cacheHandlers: nextConfig.experimental.cacheHandlers,
}) })
renderOpts.incrementalCache = incrementalCache renderOpts.incrementalCache = incrementalCache
......
/*
This is the remote "use cache" handler it defaults
to a no-op unless customized
*/
/*
This is the static "use cache" handler it defaults
to a filesystem cache unless customized
*/
...@@ -363,6 +363,22 @@ export default class NextNodeServer extends BaseServer< ...@@ -363,6 +363,22 @@ export default class NextNodeServer extends BaseServer<
) )
} }
const { cacheHandlers } = this.nextConfig.experimental
if (!(globalThis as any).__nextCacheHandlers && cacheHandlers) {
;(globalThis as any).__nextCacheHandlers = {}
for (const key of Object.keys(cacheHandlers)) {
if (cacheHandlers[key]) {
;(globalThis as any).__nextCacheHandlers[key] = interopDefault(
await dynamicImportEsmDefault(
formatDynamicImportPath(this.distDir, cacheHandlers[key])
)
)
}
}
}
// incremental-cache is request specific // incremental-cache is request specific
// although can have shared caches in module scope // although can have shared caches in module scope
// per-cache handler // per-cache handler
......
...@@ -40,8 +40,26 @@ const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge' ...@@ -40,8 +40,26 @@ const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'
// If the expire time is less than . // If the expire time is less than .
const DYNAMIC_EXPIRE = 300 const DYNAMIC_EXPIRE = 300
const cacheHandlersSymbol = Symbol.for('@next/cache-handlers')
const _globalThis: typeof globalThis & {
[cacheHandlersSymbol]?: {
RemoteCache?: CacheHandler
DefaultCache?: CacheHandler
}
__nextCacheHandlers?: Record<string, CacheHandler>
} = globalThis
const cacheHandlerMap: Map<string, CacheHandler> = new Map([ const cacheHandlerMap: Map<string, CacheHandler> = new Map([
['default', DefaultCacheHandler], [
'default',
_globalThis[cacheHandlersSymbol]?.DefaultCache || DefaultCacheHandler,
],
[
'remote',
// in dev remote maps to default handler
// and is meant to be overridden in prod
_globalThis[cacheHandlersSymbol]?.RemoteCache || DefaultCacheHandler,
],
]) ])
function generateCacheEntry( function generateCacheEntry(
...@@ -413,7 +431,13 @@ export function cache(kind: string, id: string, fn: any) { ...@@ -413,7 +431,13 @@ export function cache(kind: string, id: string, fn: any) {
'"use cache" is only available with the experimental.dynamicIO config.' '"use cache" is only available with the experimental.dynamicIO config.'
) )
} }
for (const [key, value] of Object.entries(
_globalThis.__nextCacheHandlers || {}
)) {
cacheHandlerMap.set(key, value as CacheHandler)
}
const cacheHandler = cacheHandlerMap.get(kind) const cacheHandler = cacheHandlerMap.get(kind)
if (cacheHandler === undefined) { if (cacheHandler === undefined) {
throw new Error('Unknown cache handler: ' + kind) throw new Error('Unknown cache handler: ' + kind)
} }
......
async function getCachedRandom(x: number, children: React.ReactNode) {
'use cache: custom'
return {
x,
y: Math.random(),
r: children,
}
}
export default async function Page({
searchParams,
}: {
searchParams: Promise<{ n: string }>
}) {
const n = +(await searchParams).n
const values = await getCachedRandom(
n,
<p id="r">rnd{Math.random()}</p> // This should not invalidate the cache
)
return (
<>
<p id="x">{values.x}</p>
<p id="y">{values.y}</p>
{values.r}
</>
)
}
...@@ -10,6 +10,9 @@ const nextConfig = { ...@@ -10,6 +10,9 @@ const nextConfig = {
revalidate: 100, revalidate: 100,
}, },
}, },
cacheHandlers: {
custom: require.resolve('next/dist/server/lib/cache-handlers/default'),
},
}, },
} }
......
...@@ -10,15 +10,15 @@ describe('use-cache', () => { ...@@ -10,15 +10,15 @@ describe('use-cache', () => {
}) })
it('should cache results', async () => { it('should cache results', async () => {
const browser = await next.browser('/?n=1') const browser = await next.browser(`/?n=1`)
expect(await browser.waitForElementByCss('#x').text()).toBe('1') expect(await browser.waitForElementByCss('#x').text()).toBe('1')
const random1a = await browser.waitForElementByCss('#y').text() const random1a = await browser.waitForElementByCss('#y').text()
await browser.loadPage(new URL('/?n=2', next.url).toString()) await browser.loadPage(new URL(`/?n=2`, next.url).toString())
expect(await browser.waitForElementByCss('#x').text()).toBe('2') expect(await browser.waitForElementByCss('#x').text()).toBe('2')
const random2 = await browser.waitForElementByCss('#y').text() const random2 = await browser.waitForElementByCss('#y').text()
await browser.loadPage(new URL('/?n=1&unrelated', next.url).toString()) await browser.loadPage(new URL(`/?n=1&unrelated`, next.url).toString())
expect(await browser.waitForElementByCss('#x').text()).toBe('1') expect(await browser.waitForElementByCss('#x').text()).toBe('1')
const random1b = await browser.waitForElementByCss('#y').text() const random1b = await browser.waitForElementByCss('#y').text()
...@@ -35,6 +35,35 @@ describe('use-cache', () => { ...@@ -35,6 +35,35 @@ describe('use-cache', () => {
expect(await browser.waitForElementByCss('#r').text()).toContain('rnd') expect(await browser.waitForElementByCss('#r').text()).toContain('rnd')
}) })
if (!process.env.TURBOPACK_BUILD) {
it('should cache results custom handler', async () => {
const browser = await next.browser(`/custom-handler?n=1`)
expect(await browser.waitForElementByCss('#x').text()).toBe('1')
const random1a = await browser.waitForElementByCss('#y').text()
await browser.loadPage(
new URL(`/custom-handler?n=2`, next.url).toString()
)
expect(await browser.waitForElementByCss('#x').text()).toBe('2')
const random2 = await browser.waitForElementByCss('#y').text()
await browser.loadPage(
new URL(`/custom-handler?n=1&unrelated`, next.url).toString()
)
expect(await browser.waitForElementByCss('#x').text()).toBe('1')
const random1b = await browser.waitForElementByCss('#y').text()
// The two navigations to n=1 should use a cached value.
expect(random1a).toBe(random1b)
// The navigation to n=2 should be some other random value.
expect(random1a).not.toBe(random2)
// Client component child should have rendered but not invalidated the cache.
expect(await browser.waitForElementByCss('#r').text()).toContain('rnd')
})
}
it('should cache complex args', async () => { it('should cache complex args', async () => {
// Use two bytes that can't be encoded as UTF-8 to ensure serialization works. // Use two bytes that can't be encoded as UTF-8 to ensure serialization works.
const browser = await next.browser('/complex-args?n=a1') const browser = await next.browser('/complex-args?n=a1')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment