Unverified Commit 34c991ce authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

ensure custom cache handlers are preloaded (#74622)

### What
When specifying a custom "use cache" handler via
`experimental.cacheHandlers`, the handler is not used during `next
start`

### Why
Custom cache handlers are initialized after the first request in
`getIncrementalCache`. But because `unstable_preloadEntries` is the
default during `next start`, we won't have loaded the custom cache
handlers and the app will be stuck using the default cache handler once
its set on the global namespace.

### How
This updates `unstable_preloadEntries` to load any custom cache handlers
so they're used instead of the default.

Fixes #72552
parent 58cd123f
...@@ -283,6 +283,8 @@ export default class NextNodeServer extends BaseServer< ...@@ -283,6 +283,8 @@ export default class NextNodeServer extends BaseServer<
const appPathsManifest = this.getAppPathsManifest() const appPathsManifest = this.getAppPathsManifest()
const pagesManifest = this.getPagesManifest() const pagesManifest = this.getPagesManifest()
await this.loadCustomCacheHandlers()
for (const page of Object.keys(pagesManifest || {})) { for (const page of Object.keys(pagesManifest || {})) {
await loadComponents({ await loadComponents({
distDir: this.distDir, distDir: this.distDir,
...@@ -374,25 +376,7 @@ export default class NextNodeServer extends BaseServer< ...@@ -374,25 +376,7 @@ export default class NextNodeServer extends BaseServer<
) )
} }
protected async getIncrementalCache({ protected async loadCustomCacheHandlers() {
requestHeaders,
requestProtocol,
}: {
requestHeaders: IncrementalCache['requestHeaders']
requestProtocol: 'http' | 'https'
}) {
const dev = !!this.renderOpts.dev
let CacheHandler: any
const { cacheHandler } = this.nextConfig
if (cacheHandler) {
CacheHandler = interopDefault(
await dynamicImportEsmDefault(
formatDynamicImportPath(this.distDir, cacheHandler)
)
)
}
const { cacheHandlers } = this.nextConfig.experimental const { cacheHandlers } = this.nextConfig.experimental
if (!(globalThis as any).__nextCacheHandlers && cacheHandlers) { if (!(globalThis as any).__nextCacheHandlers && cacheHandlers) {
...@@ -412,6 +396,28 @@ export default class NextNodeServer extends BaseServer< ...@@ -412,6 +396,28 @@ export default class NextNodeServer extends BaseServer<
;(globalThis as any).__nextCacheHandlers.default = DefaultCacheHandler ;(globalThis as any).__nextCacheHandlers.default = DefaultCacheHandler
} }
} }
}
protected async getIncrementalCache({
requestHeaders,
requestProtocol,
}: {
requestHeaders: IncrementalCache['requestHeaders']
requestProtocol: 'http' | 'https'
}) {
const dev = !!this.renderOpts.dev
let CacheHandler: any
const { cacheHandler } = this.nextConfig
if (cacheHandler) {
CacheHandler = interopDefault(
await dynamicImportEsmDefault(
formatDynamicImportPath(this.distDir, cacheHandler)
)
)
}
await this.loadCustomCacheHandlers()
// 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
......
export default function RootLayout({ children }) {
return (
<html>
<head />
<body>{children}</body>
</html>
)
}
import { Suspense } from 'react'
import { unstable_cacheLife as cacheLife } from 'next/cache'
async function getData() {
'use cache'
cacheLife({
revalidate: 3,
})
return Math.random()
}
async function AsyncComp() {
let data = await getData()
return <p id="data">{data}</p>
}
export default function Home() {
return (
<main>
<Suspense fallback={<p>Loading...</p>}>
<AsyncComp />
</Suspense>
</main>
)
}
const cacheHandler =
require('next/dist/server/lib/cache-handlers/default').default
module.exports = {
async get(cacheKey, softTags) {
console.log('CustomCacheHandler::Get', cacheKey, softTags)
return cacheHandler.get(cacheKey, softTags)
},
async set(cacheKey, pendingEntry) {
console.log('CustomCacheHandler::Set', cacheKey)
return cacheHandler.set(cacheKey, pendingEntry)
},
async expireTags(...tags) {},
async receiveExpiredTags(...tags) {},
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
dynamicIO: true,
cacheHandlers: {
default: require.resolve('./handler.js'),
},
},
}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
describe('use-cache-custom-handler', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
// Skip deployment so we can test the custom cache handlers log output
skipDeployment: true,
})
if (skipped) return
it('should use custom cache handler if provided', async () => {
const outputIndex = next.cliOutput.length
const browser = await next.browser(`/`)
const initialData = await browser.elementById('data').text()
expect(initialData).toMatch(/^\d+\.\d+$/)
expect(next.cliOutput.slice(outputIndex)).toContain(
'CustomCacheHandler::Get'
)
expect(next.cliOutput.slice(outputIndex)).toContain(
'CustomCacheHandler::Set'
)
await retry(async () => {
await browser.refresh()
const data = await browser.elementById('data').text()
expect(data).toMatch(/^\d+\.\d+$/)
expect(data).not.toEqual(initialData)
})
})
})
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