Skip to content

Commit

Permalink
fix memory leak in require.cache cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Sep 14, 2023
1 parent b61dd99 commit f6f68ea
Showing 1 changed file with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const originModules = [
require.resolve('../../../server/require'),
require.resolve('../../../server/load-components'),
require.resolve('../../../server/next-server'),
require.resolve('../../../server/app-renderer/use-flight-response'),
require.resolve('../../../compiled/react-server-dom-webpack/client.edge'),
require.resolve(
'../../../compiled/react-server-dom-webpack-experimental/client.edge'
Expand All @@ -20,27 +21,7 @@ const originModules = [

const RUNTIME_NAMES = ['webpack-runtime', 'webpack-api-runtime']

export function deleteAppClientCache() {
// ensure we reset the cache for rsc components
// loaded via react-server-dom-webpack
const reactServerDomModId = require.resolve(
'react-server-dom-webpack/client.edge'
)
const reactServerDomMod = require.cache[reactServerDomModId]

if (reactServerDomMod) {
for (const child of reactServerDomMod.children) {
child.parent = null
delete require.cache[child.id]
}
}
delete require.cache[reactServerDomModId]
}

export function deleteCache(filePath: string) {
// try to clear it from the fs cache
clearManifestCache(filePath)

function deleteFromRequireCache(filePath: string) {
try {
filePath = realpathSync(filePath)
} catch (e) {
Expand All @@ -66,6 +47,29 @@ export function deleteCache(filePath: string) {
return false
}

export function deleteAppClientCache() {
// ensure we reset the cache for rsc components
// loaded via react-server-dom-webpack
const reactServerDomModId = require.resolve(
'react-server-dom-webpack/client.edge'
)
const reactServerDomMod = require.cache[reactServerDomModId]

if (reactServerDomMod) {
for (const child of [...reactServerDomMod.children]) {
deleteFromRequireCache(child.id)
}
deleteFromRequireCache(reactServerDomModId)
}
}

export function deleteCache(filePath: string) {
// try to clear it from the fs cache
clearManifestCache(filePath)

deleteFromRequireCache(filePath)
}

const PLUGIN_NAME = 'NextJsRequireCacheHotReloader'

// This plugin flushes require.cache after emitting the files. Providing 'hot reloading' of server files.
Expand Down

0 comments on commit f6f68ea

Please sign in to comment.