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

Enable context caching for middleware/edge functions in dev (#47840)

As discussed this enables the context caching for the middleware/edge
functions sandbox in development which reduces execution time noticeably
as modules don't need to be re-processed for each request unless an HMR
event related to that context has occurred.

In a follow-up PR we can investigate spreading the middleware/edge
function invocations across workers/processes now that we have detached
rendering/invoke processes from the routing process.

<details>
	<summary>before</summary>

![CleanShot 2023-04-02 at 22 23
45@2x](https://user-images.githubusercontent.com/22380829/229419430-5eed36e9-2615-44be-9314-490cb7283e5b.png)

</details>

<details>
	<summary>after</summary>

![CleanShot 2023-04-02 at 22 24
41@2x](https://user-images.githubusercontent.com/22380829/229419472-822a2d16-7e0d-4ee8-99c2-b1211010ebcf.png)


</details>

x-ref: [slack
thread](https://vercel.slack.com/archives/C03KAR5DCKC/p1677431214936269)
Fixes: https://github.com/vercel/next.js/issues/43999
parent 68f328f9
......@@ -81,7 +81,12 @@ export class NextJsRequireCacheHotReloader implements WebpackPluginInstance {
PLUGIN_NAME,
(_file, { targetPath, content }) => {
deleteCache(targetPath)
clearModuleContext(targetPath, content.toString('utf-8'))
const contentStr = content.toString('utf-8')
if ((global as any)._nextClearModuleContext) {
;(global as any)._nextClearModuleContext(targetPath, contentStr)
}
clearModuleContext(targetPath, contentStr)
}
)
......
......@@ -8,7 +8,7 @@ import {
deleteCache as _deleteCache,
deleteAppClientCache as _deleteAppClientCache,
} from '../../build/webpack/plugins/nextjs-require-cache-hot-reloader'
import { clearModuleContext as _clearModuleContext } from '../web/sandbox/context'
export const WORKER_SELF_EXIT_CODE = 77
const MAXIMUM_HEAP_SIZE_ALLOWED =
......@@ -21,6 +21,10 @@ let result:
hostname: string
}
export function clearModuleContext(target: string, content: string) {
_clearModuleContext(target, content)
}
export function deleteAppClientCache() {
_deleteAppClientCache()
}
......
......@@ -90,7 +90,12 @@ export const createWorker = (
(serverPort || 0) + 1
),
},
exposedMethods: ['initialize', 'deleteCache', 'deleteAppClientCache'],
exposedMethods: [
'initialize',
'deleteCache',
'deleteAppClientCache',
'clearModuleContext',
],
}) as any as InstanceType<typeof Worker> & {
initialize: typeof import('./render-server').initialize
deleteCache: typeof import('./render-server').deleteCache
......
......@@ -183,6 +183,7 @@ type RenderWorker = Worker & {
initialize: typeof import('./lib/render-server').initialize
deleteCache: typeof import('./lib/render-server').deleteCache
deleteAppClientCache: typeof import('./lib/render-server').deleteAppClientCache
clearModuleContext: typeof import('./lib/render-server').clearModuleContext
}
export default class NextNodeServer extends BaseServer {
......@@ -289,12 +290,31 @@ export default class NextNodeServer extends BaseServer {
}
})
;(global as any)._nextDeleteCache = (filePath: string) => {
this.renderWorkers?.pages?.deleteCache(filePath)
this.renderWorkers?.app?.deleteCache(filePath)
try {
this.renderWorkers?.pages?.deleteCache(filePath)
this.renderWorkers?.app?.deleteCache(filePath)
} catch (err) {
console.error(err)
}
}
;(global as any)._nextDeleteAppClientCache = () => {
this.renderWorkers?.pages?.deleteAppClientCache()
this.renderWorkers?.app?.deleteAppClientCache()
try {
this.renderWorkers?.pages?.deleteAppClientCache()
this.renderWorkers?.app?.deleteAppClientCache()
} catch (err) {
console.error(err)
}
}
;(global as any)._nextClearModuleContext = (
targetPath: any,
content: any
) => {
try {
this.renderWorkers?.pages?.clearModuleContext(targetPath, content)
this.renderWorkers?.app?.clearModuleContext(targetPath, content)
} catch (err) {
console.error(err)
}
}
}
......@@ -2118,7 +2138,7 @@ export default class NextNodeServer extends BaseServer {
page: page,
body: getRequestMeta(params.request, '__NEXT_CLONABLE_BODY'),
},
useCache: !this.renderOpts.dev,
useCache: true,
onWarning: params.onWarning,
})
......@@ -2596,7 +2616,7 @@ export default class NextNodeServer extends BaseServer {
},
body: getRequestMeta(params.req, '__NEXT_CLONABLE_BODY'),
},
useCache: !this.renderOpts.dev,
useCache: true,
onWarning: params.onWarning,
incrementalCache: getRequestMeta(params.req, '_nextIncrementalCache'),
})
......
......@@ -41,21 +41,37 @@ interface ModuleContext {
*/
const moduleContexts = new Map<string, ModuleContext>()
const pendingModuleCaches = new Map<string, Promise<ModuleContext>>()
/**
* For a given path a context, this function checks if there is any module
* context that contains the path with an older content and, if that's the
* case, removes the context from the cache.
*/
export function clearModuleContext(path: string, content: Buffer | string) {
for (const [key, cache] of moduleContexts) {
export async function clearModuleContext(
path: string,
content: Buffer | string
) {
const handleContext = (
key: string,
cache: ReturnType<typeof moduleContexts['get']>,
context: typeof moduleContexts | typeof pendingModuleCaches
) => {
const prev = cache?.paths.get(path)?.replace(WEBPACK_HASH_REGEX, '')
if (
typeof prev !== 'undefined' &&
prev !== content.toString().replace(WEBPACK_HASH_REGEX, '')
) {
moduleContexts.delete(key)
context.delete(key)
}
}
for (const [key, cache] of moduleContexts) {
handleContext(key, cache, moduleContexts)
}
for (const [key, cache] of pendingModuleCaches) {
handleContext(key, await cache, pendingModuleCaches)
}
}
async function loadWasm(
......@@ -404,8 +420,6 @@ interface ModuleContextOptions {
edgeFunctionEntry: Pick<EdgeFunctionDefinition, 'assets' | 'wasm'>
}
const pendingModuleCaches = new Map<string, Promise<ModuleContext>>()
function getModuleContextShared(options: ModuleContextOptions) {
let deferredModuleContext = pendingModuleCaches.get(options.moduleName)
if (!deferredModuleContext) {
......
......@@ -67,7 +67,7 @@ describe('Middleware development errors', () => {
await assertMiddlewareRender(false)
// assert no extra message on stderr
expect(context.logs.stderr).toBe('')
expect(context.logs.stderr).not.toContain('error')
})
})
......
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