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: {
JSON.stringify(opts.middlewareConfig || {})
).toString('base64'),
serverActions: opts.config.experimental.serverActions,
cacheHandlers: JSON.stringify(opts.config.experimental.cacheHandlers || {}),
}
return {
......
......@@ -2175,6 +2175,7 @@ export default async function build(
pageType,
dynamicIO: isAppDynamicIOEnabled,
cacheHandler: config.cacheHandler,
cacheHandlers: config.experimental.cacheHandlers,
isrFlushToDisk: ciEnvironment.hasNextSupport
? false
: config.experimental.isrFlushToDisk,
......
......@@ -36,7 +36,8 @@ export async function loadEntrypoint(
| 'pages-api',
replacements: Record<`VAR_${string}`, string>,
injections?: Record<string, string>,
imports?: Record<string, string | null>
imports?: Record<string, string | null>,
importMaps?: Record<string, Record<string, string>>
): Promise<string> {
const filepath = path.resolve(
path.join(TEMPLATES_ESM_FOLDER, `${entrypoint}.js`)
......@@ -44,6 +45,28 @@ export async function loadEntrypoint(
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
// imports to be relative to the root of the `next` package.
let count = 0
......
......@@ -17,6 +17,12 @@ import { createServerModuleMap } from '../../server/app-render/action-utils'
declare const incrementalCacheHandler: any
// OPTIONAL_IMPORT:incrementalCacheHandler
const cacheHandlers = {}
if (!(globalThis as any).__nextCacheHandlers) {
;(globalThis as any).__nextCacheHandlers = cacheHandlers
}
const Document: DocumentType = null!
const appMod = null
const errorMod = null
......
......@@ -40,6 +40,12 @@ declare const user500RouteModuleOptions: any
// INJECT:errorRouteModuleOptions
// INJECT:user500RouteModuleOptions
const cacheHandlers = {}
if (!(globalThis as any).__nextCacheHandlers) {
;(globalThis as any).__nextCacheHandlers = cacheHandlers
}
const pageMod = {
...userlandPage,
routeModule: new RouteModule({
......
......@@ -99,6 +99,7 @@ import type { OutgoingHttpHeaders } from 'http'
import type { AppSegmentConfig } from './segment-config/app/app-segment-config'
import type { AppSegment } 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'
......@@ -1490,6 +1491,7 @@ export async function isPageStatic({
maxMemoryCacheSize,
nextConfigOutput,
cacheHandler,
cacheHandlers,
cacheLifeProfiles,
pprConfig,
buildId,
......@@ -1511,6 +1513,7 @@ export async function isPageStatic({
isrFlushToDisk?: boolean
maxMemoryCacheSize?: number
cacheHandler?: string
cacheHandlers?: Record<string, string | undefined>
cacheLifeProfiles?: {
[profile: string]: import('../server/use-cache/cache-life').CacheLife
}
......@@ -1518,6 +1521,16 @@ export async function isPageStatic({
pprConfig: ExperimentalPPRConfig | undefined
buildId: string
}): Promise<PageIsStaticResult> {
await createIncrementalCache({
cacheHandler,
cacheHandlers,
distDir,
dir,
dynamicIO,
flushToDisk: isrFlushToDisk,
cacheMaxMemorySize: maxMemoryCacheSize,
})
const isPageStaticSpan = trace('is-page-static-utils', parentId)
return isPageStaticSpan
.traceAsyncFn(async (): Promise<PageIsStaticResult> => {
......
......@@ -24,6 +24,7 @@ export type EdgeSSRLoaderQuery = {
pagesType: PAGE_TYPES
sriEnabled: boolean
cacheHandler?: string
cacheHandlers?: string
preferredRegion: string | string[] | undefined
middlewareConfig: string
serverActions?: {
......@@ -75,11 +76,14 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
pagesType,
sriEnabled,
cacheHandler,
cacheHandlers: cacheHandlersStringified,
preferredRegion,
middlewareConfig: middlewareConfigBase64,
serverActions,
} = this.getOptions()
const cacheHandlers = JSON.parse(cacheHandlersStringified || '{}')
const middlewareConfig: MiddlewareConfig = JSON.parse(
Buffer.from(middlewareConfigBase64, 'base64').toString()
)
......@@ -156,6 +160,9 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
},
{
incrementalCacheHandler: cacheHandler ?? null,
},
{
cacheHandlers: cacheHandlers ?? {},
}
)
} else {
......@@ -184,6 +191,9 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
{
userland500Page: userland500Path,
incrementalCacheHandler: cacheHandler ?? null,
},
{
cacheHandlers: cacheHandlers || {},
}
)
}
......
......@@ -13,6 +13,7 @@ export async function createIncrementalCache({
distDir,
dir,
flushToDisk,
cacheHandlers,
}: {
dynamicIO: boolean
cacheHandler?: string
......@@ -21,6 +22,7 @@ export async function createIncrementalCache({
distDir: string
dir: string
flushToDisk?: boolean
cacheHandlers?: Record<string, string | undefined>
}) {
// Custom cache handler overrides.
let CacheHandler: any
......@@ -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({
dev: false,
requestHeaders: {},
......
......@@ -361,6 +361,7 @@ export async function exportPages(
// skip writing to disk in minimal mode for now, pending some
// changes to better support it
flushToDisk: !hasNextSupport,
cacheHandlers: nextConfig.experimental.cacheHandlers,
})
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<
)
}
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
// although can have shared caches in module scope
// per-cache handler
......
......@@ -40,8 +40,26 @@ const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'
// If the expire time is less than .
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([
['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(
......@@ -413,7 +431,13 @@ export function cache(kind: string, id: string, fn: any) {
'"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)
if (cacheHandler === undefined) {
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 = {
revalidate: 100,
},
},
cacheHandlers: {
custom: require.resolve('next/dist/server/lib/cache-handlers/default'),
},
},
}
......
......@@ -10,15 +10,15 @@ describe('use-cache', () => {
})
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')
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')
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')
const random1b = await browser.waitForElementByCss('#y').text()
......@@ -35,6 +35,35 @@ describe('use-cache', () => {
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 () => {
// Use two bytes that can't be encoded as UTF-8 to ensure serialization works.
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