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

Update app cache handler loading (#46290

Follow-up to https://github.com/vercel/next.js/pull/46287 this updates
how we load the cache handler for the incremental cache so it's
compatible with edge and also adds regression testing with a custom
handler.
parent 2882eb4e
...@@ -225,6 +225,8 @@ export function getEdgeServerEntry(opts: { ...@@ -225,6 +225,8 @@ export function getEdgeServerEntry(opts: {
pagesType: opts.pagesType, pagesType: opts.pagesType,
appDirLoader: Buffer.from(opts.appDirLoader || '').toString('base64'), appDirLoader: Buffer.from(opts.appDirLoader || '').toString('base64'),
sriEnabled: !opts.isDev && !!opts.config.experimental.sri?.algorithm, sriEnabled: !opts.isDev && !!opts.config.experimental.sri?.algorithm,
incrementalCacheHandlerPath:
opts.config.experimental.incrementalCacheHandlerPath,
} }
return { return {
......
...@@ -913,6 +913,13 @@ export default async function build( ...@@ -913,6 +913,13 @@ export default async function build(
experimental: { experimental: {
...config.experimental, ...config.experimental,
trustHostHeader: ciEnvironment.hasNextSupport, trustHostHeader: ciEnvironment.hasNextSupport,
incrementalCacheHandlerPath: config.experimental
.incrementalCacheHandlerPath
? path.relative(
distDir,
config.experimental.incrementalCacheHandlerPath
)
: undefined,
}, },
}, },
appDir: dir, appDir: dir,
......
...@@ -15,6 +15,7 @@ export type EdgeSSRLoaderQuery = { ...@@ -15,6 +15,7 @@ export type EdgeSSRLoaderQuery = {
appDirLoader?: string appDirLoader?: string
pagesType: 'app' | 'pages' | 'root' pagesType: 'app' | 'pages' | 'root'
sriEnabled: boolean sriEnabled: boolean
incrementalCacheHandlerPath?: string
} }
/* /*
...@@ -44,6 +45,7 @@ export default async function edgeSSRLoader(this: any) { ...@@ -44,6 +45,7 @@ export default async function edgeSSRLoader(this: any) {
appDirLoader: appDirLoaderBase64, appDirLoader: appDirLoaderBase64,
pagesType, pagesType,
sriEnabled, sriEnabled,
incrementalCacheHandlerPath,
} = this.getOptions() } = this.getOptions()
const appDirLoader = Buffer.from( const appDirLoader = Buffer.from(
...@@ -117,6 +119,12 @@ export default async function edgeSSRLoader(this: any) { ...@@ -117,6 +119,12 @@ export default async function edgeSSRLoader(this: any) {
const appRenderToHTML = null const appRenderToHTML = null
` `
} }
const incrementalCacheHandler = ${
incrementalCacheHandlerPath
? `require("${incrementalCacheHandlerPath}")`
: 'null'
}
const buildManifest = self.__BUILD_MANIFEST const buildManifest = self.__BUILD_MANIFEST
const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST
...@@ -146,6 +154,7 @@ export default async function edgeSSRLoader(this: any) { ...@@ -146,6 +154,7 @@ export default async function edgeSSRLoader(this: any) {
config: ${stringifiedConfig}, config: ${stringifiedConfig},
buildId: ${JSON.stringify(buildId)}, buildId: ${JSON.stringify(buildId)},
fontLoaderManifest, fontLoaderManifest,
incrementalCacheHandler,
}) })
export const ComponentMod = pageMod export const ComponentMod = pageMod
......
...@@ -31,6 +31,7 @@ export function getRender({ ...@@ -31,6 +31,7 @@ export function getRender({
config, config,
buildId, buildId,
fontLoaderManifest, fontLoaderManifest,
incrementalCacheHandler,
}: { }: {
pagesType: 'app' | 'pages' | 'root' pagesType: 'app' | 'pages' | 'root'
dev: boolean dev: boolean
...@@ -51,6 +52,7 @@ export function getRender({ ...@@ -51,6 +52,7 @@ export function getRender({
config: NextConfigComplete config: NextConfigComplete
buildId: string buildId: string
fontLoaderManifest: FontLoaderManifest fontLoaderManifest: FontLoaderManifest
incrementalCacheHandler?: any
}) { }) {
const isAppPath = pagesType === 'app' const isAppPath = pagesType === 'app'
const baseLoadComponentResult = { const baseLoadComponentResult = {
...@@ -80,6 +82,7 @@ export function getRender({ ...@@ -80,6 +82,7 @@ export function getRender({
}, },
appRenderToHTML, appRenderToHTML,
pagesRenderToHTML, pagesRenderToHTML,
incrementalCacheHandler,
loadComponent: async (pathname) => { loadComponent: async (pathname) => {
if (isAppPath) return null if (isAppPath) return null
......
...@@ -653,6 +653,8 @@ export default async function exportApp( ...@@ -653,6 +653,8 @@ export default async function exportApp(
debugOutput: options.debugOutput, debugOutput: options.debugOutput,
isrMemoryCacheSize: nextConfig.experimental.isrMemoryCacheSize, isrMemoryCacheSize: nextConfig.experimental.isrMemoryCacheSize,
fetchCache: nextConfig.experimental.appDir, fetchCache: nextConfig.experimental.appDir,
incrementalCacheHandlerPath:
nextConfig.experimental.incrementalCacheHandlerPath,
}) })
for (const validation of result.ampValidations || []) { for (const validation of result.ampValidations || []) {
......
...@@ -84,6 +84,7 @@ interface ExportPageInput { ...@@ -84,6 +84,7 @@ interface ExportPageInput {
debugOutput?: boolean debugOutput?: boolean
isrMemoryCacheSize?: NextConfigComplete['experimental']['isrMemoryCacheSize'] isrMemoryCacheSize?: NextConfigComplete['experimental']['isrMemoryCacheSize']
fetchCache?: boolean fetchCache?: boolean
incrementalCacheHandlerPath?: string
} }
interface ExportPageResults { interface ExportPageResults {
...@@ -141,6 +142,7 @@ export default async function exportPage({ ...@@ -141,6 +142,7 @@ export default async function exportPage({
debugOutput, debugOutput,
isrMemoryCacheSize, isrMemoryCacheSize,
fetchCache, fetchCache,
incrementalCacheHandlerPath,
}: ExportPageInput): Promise<ExportPageResults> { }: ExportPageInput): Promise<ExportPageResults> {
setHttpClientAndAgentOptions({ setHttpClientAndAgentOptions({
httpAgentOptions, httpAgentOptions,
...@@ -329,6 +331,13 @@ export default async function exportPage({ ...@@ -329,6 +331,13 @@ export default async function exportPage({
// only fully static paths are fully generated here // only fully static paths are fully generated here
if (isAppDir) { if (isAppDir) {
if (fetchCache) { if (fetchCache) {
let CacheHandler: any
if (incrementalCacheHandlerPath) {
CacheHandler = require(incrementalCacheHandlerPath)
CacheHandler = CacheHandler.default || CacheHandler
}
curRenderOpts.incrementalCache = new IncrementalCache({ curRenderOpts.incrementalCache = new IncrementalCache({
dev: false, dev: false,
requestHeaders: {}, requestHeaders: {},
...@@ -353,6 +362,7 @@ export default async function exportPage({ ...@@ -353,6 +362,7 @@ export default async function exportPage({
stat: (f) => fs.promises.stat(f), stat: (f) => fs.promises.stat(f),
}, },
serverDistDir: join(distDir, 'server'), serverDistDir: join(distDir, 'server'),
CurCacheHandler: CacheHandler,
}) })
} }
......
...@@ -20,6 +20,8 @@ export function createClientRouterFilter( ...@@ -20,6 +20,8 @@ export function createClientRouterFilter(
let subPath = '' let subPath = ''
const pathParts = path.split('/') const pathParts = path.split('/')
// start at 1 since we split on '/' and the path starts
// with this so the first entry is an empty string
for (let i = 1; i < pathParts.length + 1; i++) { for (let i = 1; i < pathParts.length + 1; i++) {
const curPart = pathParts[i] const curPart = pathParts[i]
......
...@@ -67,8 +67,8 @@ export class IncrementalCache { ...@@ -67,8 +67,8 @@ export class IncrementalCache {
requestHeaders, requestHeaders,
maxMemoryCacheSize, maxMemoryCacheSize,
getPrerenderManifest, getPrerenderManifest,
incrementalCacheHandlerPath,
fetchCacheKeyPrefix, fetchCacheKeyPrefix,
CurCacheHandler,
}: { }: {
fs?: CacheFs fs?: CacheFs
dev: boolean dev: boolean
...@@ -79,23 +79,18 @@ export class IncrementalCache { ...@@ -79,23 +79,18 @@ export class IncrementalCache {
flushToDisk?: boolean flushToDisk?: boolean
requestHeaders: IncrementalCache['requestHeaders'] requestHeaders: IncrementalCache['requestHeaders']
maxMemoryCacheSize?: number maxMemoryCacheSize?: number
incrementalCacheHandlerPath?: string
getPrerenderManifest: () => PrerenderManifest getPrerenderManifest: () => PrerenderManifest
fetchCacheKeyPrefix?: string fetchCacheKeyPrefix?: string
CurCacheHandler?: typeof CacheHandler
}) { }) {
let cacheHandlerMod: any if (!CurCacheHandler) {
if (fs && serverDistDir) {
if (fs && serverDistDir) { CurCacheHandler = FileSystemCache
cacheHandlerMod = FileSystemCache }
}
if (process.env.NEXT_RUNTIME !== 'edge' && incrementalCacheHandlerPath) {
cacheHandlerMod = require(incrementalCacheHandlerPath)
cacheHandlerMod = cacheHandlerMod.default || cacheHandlerMod
}
if (!incrementalCacheHandlerPath && minimalMode && fetchCache) { if (minimalMode && fetchCache) {
cacheHandlerMod = FetchCache CurCacheHandler = FetchCache
}
} }
if (process.env.__NEXT_TEST_MAX_ISR_CACHE) { if (process.env.__NEXT_TEST_MAX_ISR_CACHE) {
...@@ -107,8 +102,8 @@ export class IncrementalCache { ...@@ -107,8 +102,8 @@ export class IncrementalCache {
this.requestHeaders = requestHeaders this.requestHeaders = requestHeaders
this.prerenderManifest = getPrerenderManifest() this.prerenderManifest = getPrerenderManifest()
if (cacheHandlerMod) { if (CurCacheHandler) {
this.cacheHandler = new (cacheHandlerMod as typeof CacheHandler)({ this.cacheHandler = new CurCacheHandler({
dev, dev,
fs, fs,
flushToDisk, flushToDisk,
......
...@@ -277,6 +277,15 @@ export default class NextNodeServer extends BaseServer { ...@@ -277,6 +277,15 @@ export default class NextNodeServer extends BaseServer {
requestHeaders: IncrementalCache['requestHeaders'] requestHeaders: IncrementalCache['requestHeaders']
}) { }) {
const dev = !!this.renderOpts.dev const dev = !!this.renderOpts.dev
let CacheHandler: any
const { incrementalCacheHandlerPath } = this.nextConfig.experimental
if (incrementalCacheHandlerPath) {
CacheHandler = require(this.minimalMode
? join(this.distDir, incrementalCacheHandlerPath)
: incrementalCacheHandlerPath)
CacheHandler = CacheHandler.default || CacheHandler
}
// incremental-cache is request specific with a shared // incremental-cache is request specific with a shared
// although can have shared caches in module scope // although can have shared caches in module scope
// per-cache handler // per-cache handler
...@@ -292,8 +301,6 @@ export default class NextNodeServer extends BaseServer { ...@@ -292,8 +301,6 @@ export default class NextNodeServer extends BaseServer {
maxMemoryCacheSize: this.nextConfig.experimental.isrMemoryCacheSize, maxMemoryCacheSize: this.nextConfig.experimental.isrMemoryCacheSize,
flushToDisk: flushToDisk:
!this.minimalMode && this.nextConfig.experimental.isrFlushToDisk, !this.minimalMode && this.nextConfig.experimental.isrFlushToDisk,
incrementalCacheHandlerPath:
this.nextConfig.experimental?.incrementalCacheHandlerPath,
getPrerenderManifest: () => { getPrerenderManifest: () => {
if (dev) { if (dev) {
return { return {
...@@ -307,6 +314,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -307,6 +314,7 @@ export default class NextNodeServer extends BaseServer {
return this.getPrerenderManifest() return this.getPrerenderManifest()
} }
}, },
CurCacheHandler: CacheHandler,
}) })
} }
......
...@@ -38,6 +38,7 @@ interface WebServerOptions extends Options { ...@@ -38,6 +38,7 @@ interface WebServerOptions extends Options {
Pick<BaseServer['renderOpts'], 'buildId'> Pick<BaseServer['renderOpts'], 'buildId'>
pagesRenderToHTML?: typeof import('./render').renderToHTML pagesRenderToHTML?: typeof import('./render').renderToHTML
appRenderToHTML?: typeof import('./app-render').renderToHTMLOrFlight appRenderToHTML?: typeof import('./app-render').renderToHTMLOrFlight
incrementalCacheHandler?: any
} }
} }
...@@ -71,8 +72,8 @@ export default class NextWebServer extends BaseServer<WebServerOptions> { ...@@ -71,8 +72,8 @@ export default class NextWebServer extends BaseServer<WebServerOptions> {
fetchCacheKeyPrefix: this.nextConfig.experimental.fetchCacheKeyPrefix, fetchCacheKeyPrefix: this.nextConfig.experimental.fetchCacheKeyPrefix,
maxMemoryCacheSize: this.nextConfig.experimental.isrMemoryCacheSize, maxMemoryCacheSize: this.nextConfig.experimental.isrMemoryCacheSize,
flushToDisk: false, flushToDisk: false,
incrementalCacheHandlerPath: CurCacheHandler:
this.nextConfig.experimental?.incrementalCacheHandlerPath, this.serverOptions.webServerConfig.incrementalCacheHandler,
getPrerenderManifest: () => { getPrerenderManifest: () => {
if (dev) { if (dev) {
return { return {
......
process.env.CUSTOM_CACHE_HANDLER = '1'
require('./app-static.test')
...@@ -14,9 +14,10 @@ createNextDescribe( ...@@ -14,9 +14,10 @@ createNextDescribe(
files: __dirname, files: __dirname,
env: { env: {
NEXT_DEBUG_BUILD: '1', NEXT_DEBUG_BUILD: '1',
CUSTOM_CACHE_HANDLER: process.env.CUSTOM_CACHE_HANDLER,
}, },
}, },
({ next, isNextDev: isDev, isNextStart }) => { ({ next, isNextDev: isDev, isNextStart, isNextDeploy }) => {
if (isNextStart) { if (isNextStart) {
it('should output HTML/RSC files for static paths', async () => { it('should output HTML/RSC files for static paths', async () => {
const files = ( const files = (
...@@ -383,19 +384,23 @@ createNextDescribe( ...@@ -383,19 +384,23 @@ createNextDescribe(
const html = await res.text() const html = await res.text()
const $ = cheerio.load(html) const $ = cheerio.load(html)
const layoutData = $('#layout-data').text() // the test cache handler is simple and doesn't share
const pageData = $('#page-data').text() // state across workers so not guaranteed to have cache hit
if (!(isNextDeploy && process.env.CUSTOM_CACHE_HANDLER)) {
const layoutData = $('#layout-data').text()
const pageData = $('#page-data').text()
const res2 = await fetchViaHTTP( const res2 = await fetchViaHTTP(
next.url, next.url,
'/variable-revalidate-edge/revalidate-3' '/variable-revalidate-edge/revalidate-3'
) )
expect(res2.status).toBe(200) expect(res2.status).toBe(200)
const html2 = await res2.text() const html2 = await res2.text()
const $2 = cheerio.load(html2) const $2 = cheerio.load(html2)
expect($2('#layout-data').text()).toBe(layoutData) expect($2('#layout-data').text()).toBe(layoutData)
expect($2('#page-data').text()).toBe(pageData) expect($2('#page-data').text()).toBe(pageData)
}
return 'success' return 'success'
}, 'success') }, 'success')
}) })
...@@ -584,32 +589,36 @@ createNextDescribe( ...@@ -584,32 +589,36 @@ createNextDescribe(
} }
}) })
it('should handle dynamicParams: false correctly', async () => { // since we aren't leveraging fs cache with custom handler
const validParams = ['tim', 'seb', 'styfle'] // then these will 404 as they are cache misses
if (!(isNextStart && process.env.CUSTOM_CACHE_HANDLER)) {
it('should handle dynamicParams: false correctly', async () => {
const validParams = ['tim', 'seb', 'styfle']
for (const param of validParams) { for (const param of validParams) {
const res = await next.fetch(`/blog/${param}`, { const res = await next.fetch(`/blog/${param}`, {
redirect: 'manual', redirect: 'manual',
}) })
expect(res.status).toBe(200) expect(res.status).toBe(200)
const html = await res.text() const html = await res.text()
const $ = cheerio.load(html) const $ = cheerio.load(html)
expect(JSON.parse($('#params').text())).toEqual({ expect(JSON.parse($('#params').text())).toEqual({
author: param, author: param,
}) })
expect($('#page').text()).toBe('/blog/[author]') expect($('#page').text()).toBe('/blog/[author]')
} }
const invalidParams = ['timm', 'non-existent'] const invalidParams = ['timm', 'non-existent']
for (const param of invalidParams) { for (const param of invalidParams) {
const invalidRes = await next.fetch(`/blog/${param}`, { const invalidRes = await next.fetch(`/blog/${param}`, {
redirect: 'manual', redirect: 'manual',
}) })
expect(invalidRes.status).toBe(404) expect(invalidRes.status).toBe(404)
expect(await invalidRes.text()).toContain('page could not be found') expect(await invalidRes.text()).toContain('page could not be found')
} }
}) })
}
it('should work with forced dynamic path', async () => { it('should work with forced dynamic path', async () => {
for (const slug of ['first', 'second']) { for (const slug of ['first', 'second']) {
...@@ -664,40 +673,50 @@ createNextDescribe( ...@@ -664,40 +673,50 @@ createNextDescribe(
} }
}) })
it('should navigate to static path correctly', async () => { // since we aren't leveraging fs cache with custom handler
const browser = await next.browser('/blog/tim') // then these will 404 as they are cache misses
await browser.eval('window.beforeNav = 1') if (!(isNextStart && process.env.CUSTOM_CACHE_HANDLER)) {
it('should navigate to static path correctly', async () => {
expect( const browser = await next.browser('/blog/tim')
await browser.eval('document.documentElement.innerHTML') await browser.eval('window.beforeNav = 1')
).toContain('/blog/[author]')
await browser.elementByCss('#author-2').click() expect(
await browser.eval('document.documentElement.innerHTML')
await check(async () => { ).toContain('/blog/[author]')
const params = JSON.parse(await browser.elementByCss('#params').text()) await browser.elementByCss('#author-2').click()
return params.author === 'seb' ? 'found' : params
}, 'found') await check(async () => {
const params = JSON.parse(
await browser.elementByCss('#params').text()
)
return params.author === 'seb' ? 'found' : params
}, 'found')
expect(await browser.eval('window.beforeNav')).toBe(1) expect(await browser.eval('window.beforeNav')).toBe(1)
await browser.elementByCss('#author-1-post-1').click() await browser.elementByCss('#author-1-post-1').click()
await check(async () => { await check(async () => {
const params = JSON.parse(await browser.elementByCss('#params').text()) const params = JSON.parse(
return params.author === 'tim' && params.slug === 'first-post' await browser.elementByCss('#params').text()
? 'found' )
: params return params.author === 'tim' && params.slug === 'first-post'
}, 'found') ? 'found'
: params
}, 'found')
expect(await browser.eval('window.beforeNav')).toBe(1) expect(await browser.eval('window.beforeNav')).toBe(1)
await browser.back() await browser.back()
await check(async () => { await check(async () => {
const params = JSON.parse(await browser.elementByCss('#params').text()) const params = JSON.parse(
return params.author === 'seb' ? 'found' : params await browser.elementByCss('#params').text()
}, 'found') )
return params.author === 'seb' ? 'found' : params
}, 'found')
expect(await browser.eval('window.beforeNav')).toBe(1) expect(await browser.eval('window.beforeNav')).toBe(1)
}) })
}
it('should ssr dynamically when detected automatically with fetch cache option', async () => { it('should ssr dynamically when detected automatically with fetch cache option', async () => {
const pathname = '/ssr-auto/cache-no-store' const pathname = '/ssr-auto/cache-no-store'
...@@ -959,5 +978,13 @@ createNextDescribe( ...@@ -959,5 +978,13 @@ createNextDescribe(
await waitFor(1000) await waitFor(1000)
checkUrl() checkUrl()
}) })
if (process.env.CUSTOM_CACHE_HANDLER && !isNextDeploy) {
it('should have logs from cache-handler', () => {
expect(next.cliOutput).toContain('initialized custom cache-handler')
expect(next.cliOutput).toContain('cache-handler get')
expect(next.cliOutput).toContain('cache-handler set')
})
}
} }
) )
const cache = new Map()
module.exports = class CacheHandler {
constructor(options) {
this.options = options
this.cache = {}
console.log('initialized custom cache-handler')
}
async get(key) {
console.log('cache-handler get', key)
return cache.get(key)
}
async set(key, data) {
console.log('cache-handler set', key)
cache.set(key, {
value: data,
lastModified: Date.now(),
})
}
}
module.exports = { module.exports = {
experimental: { experimental: {
appDir: true, appDir: true,
incrementalCacheHandlerPath: process.env.CUSTOM_CACHE_HANDLER
? require.resolve('./cache-handler.js')
: undefined,
}, },
// assetPrefix: '/assets', // assetPrefix: '/assets',
rewrites: async () => { rewrites: async () => {
......
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