-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up
require.cache
handling (#68743)
Follow-up from #68535. Since the `require.cache` handling is not specific for Webpack, we move the helper functions out of `webpack/plugins/nextjs-require-cache-hot-reloader.ts`. In addition, when deleting an entry from the `require.cache`, while cleaning up its references in the `children` of parent modules, we now consider all entries in the `require.cache` instead of relying on a list of "origin modules". This list had entries that were not needed, most notably the server runtime bundles (the experimental runtimes were even missing), and at least one (`node-manifest-loader`) was also missing. This has a negligible effect on performance. Handling 1200 `require.cache` entries takes 0.3ms on an M2. The approach was also previously aligned with @sokra.
- Loading branch information
1 parent
b1e19ec
commit 4595c5d
Showing
6 changed files
with
49 additions
and
79 deletions.
There are no files selected for viewing
59 changes: 1 addition & 58 deletions
59
packages/next/src/build/webpack/plugins/nextjs-require-cache-hot-reloader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import isError from '../../lib/is-error' | ||
import { realpathSync } from '../../lib/realpath' | ||
import { clearManifestCache } from '../load-manifest' | ||
|
||
function deleteFromRequireCache(filePath: string) { | ||
try { | ||
filePath = realpathSync(filePath) | ||
} catch (e) { | ||
if (isError(e) && e.code !== 'ENOENT') throw e | ||
} | ||
const mod = require.cache[filePath] | ||
if (mod) { | ||
// remove the child reference from all parent modules | ||
for (const parent of Object.values(require.cache)) { | ||
if (parent?.children) { | ||
const idx = parent.children.indexOf(mod) | ||
if (idx >= 0) parent.children.splice(idx, 1) | ||
} | ||
} | ||
// remove parent references from external modules | ||
for (const child of mod.children) { | ||
child.parent = null | ||
} | ||
delete require.cache[filePath] | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
export function deleteAppClientCache() { | ||
deleteFromRequireCache( | ||
require.resolve('next/dist/compiled/next-server/app-page.runtime.dev.js') | ||
) | ||
deleteFromRequireCache( | ||
require.resolve( | ||
'next/dist/compiled/next-server/app-page-experimental.runtime.dev.js' | ||
) | ||
) | ||
} | ||
|
||
export function deleteCache(filePath: string) { | ||
// try to clear it from the fs cache | ||
clearManifestCache(filePath) | ||
|
||
deleteFromRequireCache(filePath) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters