Unverified Commit 83395c92 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

fix memory leak in require.cache (#24282)

Previously only the top-level page would be cleared from the require cache, with this change it also clears `module.parent` and `modules.children`.
parent 3a2cd0e0
...@@ -3,14 +3,33 @@ import { isWebpack5 } from 'next/dist/compiled/webpack/webpack' ...@@ -3,14 +3,33 @@ import { isWebpack5 } from 'next/dist/compiled/webpack/webpack'
import { realpathSync } from 'fs' import { realpathSync } from 'fs'
import path from 'path' import path from 'path'
const originModules = [
require.resolve('../../../next-server/server/require'),
require.resolve('../../../next-server/server/load-components'),
]
function deleteCache(filePath: string) { function deleteCache(filePath: string) {
try { try {
delete require.cache[realpathSync(filePath)] filePath = realpathSync(filePath)
} catch (e) { } catch (e) {
if (e.code !== 'ENOENT') throw e if (e.code !== 'ENOENT') throw e
} finally {
delete require.cache[filePath]
} }
const module = require.cache[filePath]
if (module) {
// remove the child reference from the originModules
for (const originModule of originModules) {
const parent = require.cache[originModule]
if (parent) {
const idx = parent.children.indexOf(module)
if (idx >= 0) parent.children.splice(idx, 1)
}
}
// remove parent references from external modules
for (const child of module.children) {
child.parent = null
}
}
delete require.cache[filePath]
} }
const PLUGIN_NAME = 'NextJsRequireCacheHotReloader' const PLUGIN_NAME = 'NextJsRequireCacheHotReloader'
......
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