Skip to content

Commit

Permalink
Update app cache handler loading (#46290
Browse files Browse the repository at this point in the history
Follow-up to #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.
  • Loading branch information
ijjk authored Feb 23, 2023
1 parent 2882eb4 commit 5792533
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 84 deletions.
2 changes: 2 additions & 0 deletions packages/next/src/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ export function getEdgeServerEntry(opts: {
pagesType: opts.pagesType,
appDirLoader: Buffer.from(opts.appDirLoader || '').toString('base64'),
sriEnabled: !opts.isDev && !!opts.config.experimental.sri?.algorithm,
incrementalCacheHandlerPath:
opts.config.experimental.incrementalCacheHandlerPath,
}

return {
Expand Down
7 changes: 7 additions & 0 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,13 @@ export default async function build(
experimental: {
...config.experimental,
trustHostHeader: ciEnvironment.hasNextSupport,
incrementalCacheHandlerPath: config.experimental
.incrementalCacheHandlerPath
? path.relative(
distDir,
config.experimental.incrementalCacheHandlerPath
)
: undefined,
},
},
appDir: dir,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type EdgeSSRLoaderQuery = {
appDirLoader?: string
pagesType: 'app' | 'pages' | 'root'
sriEnabled: boolean
incrementalCacheHandlerPath?: string
}

/*
Expand Down Expand Up @@ -44,6 +45,7 @@ export default async function edgeSSRLoader(this: any) {
appDirLoader: appDirLoaderBase64,
pagesType,
sriEnabled,
incrementalCacheHandlerPath,
} = this.getOptions()

const appDirLoader = Buffer.from(
Expand Down Expand Up @@ -117,6 +119,12 @@ export default async function edgeSSRLoader(this: any) {
const appRenderToHTML = null
`
}
const incrementalCacheHandler = ${
incrementalCacheHandlerPath
? `require("${incrementalCacheHandlerPath}")`
: 'null'
}
const buildManifest = self.__BUILD_MANIFEST
const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST
Expand Down Expand Up @@ -146,6 +154,7 @@ export default async function edgeSSRLoader(this: any) {
config: ${stringifiedConfig},
buildId: ${JSON.stringify(buildId)},
fontLoaderManifest,
incrementalCacheHandler,
})
export const ComponentMod = pageMod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function getRender({
config,
buildId,
fontLoaderManifest,
incrementalCacheHandler,
}: {
pagesType: 'app' | 'pages' | 'root'
dev: boolean
Expand All @@ -51,6 +52,7 @@ export function getRender({
config: NextConfigComplete
buildId: string
fontLoaderManifest: FontLoaderManifest
incrementalCacheHandler?: any
}) {
const isAppPath = pagesType === 'app'
const baseLoadComponentResult = {
Expand Down Expand Up @@ -80,6 +82,7 @@ export function getRender({
},
appRenderToHTML,
pagesRenderToHTML,
incrementalCacheHandler,
loadComponent: async (pathname) => {
if (isAppPath) return null

Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/export/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,8 @@ export default async function exportApp(
debugOutput: options.debugOutput,
isrMemoryCacheSize: nextConfig.experimental.isrMemoryCacheSize,
fetchCache: nextConfig.experimental.appDir,
incrementalCacheHandlerPath:
nextConfig.experimental.incrementalCacheHandlerPath,
})

for (const validation of result.ampValidations || []) {
Expand Down
10 changes: 10 additions & 0 deletions packages/next/src/export/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ interface ExportPageInput {
debugOutput?: boolean
isrMemoryCacheSize?: NextConfigComplete['experimental']['isrMemoryCacheSize']
fetchCache?: boolean
incrementalCacheHandlerPath?: string
}

interface ExportPageResults {
Expand Down Expand Up @@ -141,6 +142,7 @@ export default async function exportPage({
debugOutput,
isrMemoryCacheSize,
fetchCache,
incrementalCacheHandlerPath,
}: ExportPageInput): Promise<ExportPageResults> {
setHttpClientAndAgentOptions({
httpAgentOptions,
Expand Down Expand Up @@ -329,6 +331,13 @@ export default async function exportPage({
// only fully static paths are fully generated here
if (isAppDir) {
if (fetchCache) {
let CacheHandler: any

if (incrementalCacheHandlerPath) {
CacheHandler = require(incrementalCacheHandlerPath)
CacheHandler = CacheHandler.default || CacheHandler
}

curRenderOpts.incrementalCache = new IncrementalCache({
dev: false,
requestHeaders: {},
Expand All @@ -353,6 +362,7 @@ export default async function exportPage({
stat: (f) => fs.promises.stat(f),
},
serverDistDir: join(distDir, 'server'),
CurCacheHandler: CacheHandler,
})
}

Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/lib/create-router-client-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export function createClientRouterFilter(
let subPath = ''
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++) {
const curPart = pathParts[i]

Expand Down
27 changes: 11 additions & 16 deletions packages/next/src/server/lib/incremental-cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ export class IncrementalCache {
requestHeaders,
maxMemoryCacheSize,
getPrerenderManifest,
incrementalCacheHandlerPath,
fetchCacheKeyPrefix,
CurCacheHandler,
}: {
fs?: CacheFs
dev: boolean
Expand All @@ -79,23 +79,18 @@ export class IncrementalCache {
flushToDisk?: boolean
requestHeaders: IncrementalCache['requestHeaders']
maxMemoryCacheSize?: number
incrementalCacheHandlerPath?: string
getPrerenderManifest: () => PrerenderManifest
fetchCacheKeyPrefix?: string
CurCacheHandler?: typeof CacheHandler
}) {
let cacheHandlerMod: any

if (fs && serverDistDir) {
cacheHandlerMod = FileSystemCache
}

if (process.env.NEXT_RUNTIME !== 'edge' && incrementalCacheHandlerPath) {
cacheHandlerMod = require(incrementalCacheHandlerPath)
cacheHandlerMod = cacheHandlerMod.default || cacheHandlerMod
}
if (!CurCacheHandler) {
if (fs && serverDistDir) {
CurCacheHandler = FileSystemCache
}

if (!incrementalCacheHandlerPath && minimalMode && fetchCache) {
cacheHandlerMod = FetchCache
if (minimalMode && fetchCache) {
CurCacheHandler = FetchCache
}
}

if (process.env.__NEXT_TEST_MAX_ISR_CACHE) {
Expand All @@ -107,8 +102,8 @@ export class IncrementalCache {
this.requestHeaders = requestHeaders
this.prerenderManifest = getPrerenderManifest()

if (cacheHandlerMod) {
this.cacheHandler = new (cacheHandlerMod as typeof CacheHandler)({
if (CurCacheHandler) {
this.cacheHandler = new CurCacheHandler({
dev,
fs,
flushToDisk,
Expand Down
12 changes: 10 additions & 2 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,15 @@ export default class NextNodeServer extends BaseServer {
requestHeaders: IncrementalCache['requestHeaders']
}) {
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
// although can have shared caches in module scope
// per-cache handler
Expand All @@ -292,8 +301,6 @@ export default class NextNodeServer extends BaseServer {
maxMemoryCacheSize: this.nextConfig.experimental.isrMemoryCacheSize,
flushToDisk:
!this.minimalMode && this.nextConfig.experimental.isrFlushToDisk,
incrementalCacheHandlerPath:
this.nextConfig.experimental?.incrementalCacheHandlerPath,
getPrerenderManifest: () => {
if (dev) {
return {
Expand All @@ -307,6 +314,7 @@ export default class NextNodeServer extends BaseServer {
return this.getPrerenderManifest()
}
},
CurCacheHandler: CacheHandler,
})
}

Expand Down
5 changes: 3 additions & 2 deletions packages/next/src/server/web-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ interface WebServerOptions extends Options {
Pick<BaseServer['renderOpts'], 'buildId'>
pagesRenderToHTML?: typeof import('./render').renderToHTML
appRenderToHTML?: typeof import('./app-render').renderToHTMLOrFlight
incrementalCacheHandler?: any
}
}

Expand Down Expand Up @@ -71,8 +72,8 @@ export default class NextWebServer extends BaseServer<WebServerOptions> {
fetchCacheKeyPrefix: this.nextConfig.experimental.fetchCacheKeyPrefix,
maxMemoryCacheSize: this.nextConfig.experimental.isrMemoryCacheSize,
flushToDisk: false,
incrementalCacheHandlerPath:
this.nextConfig.experimental?.incrementalCacheHandlerPath,
CurCacheHandler:
this.serverOptions.webServerConfig.incrementalCacheHandler,
getPrerenderManifest: () => {
if (dev) {
return {
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/app-dir/app-static/app-static-custom-handler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
process.env.CUSTOM_CACHE_HANDLER = '1'
require('./app-static.test')
Loading

0 comments on commit 5792533

Please sign in to comment.