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