mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 23:57:22 +02:00
Clean up cache removal function
This commit is contained in:
parent
065b8938b2
commit
2e33758bef
1 changed files with 23 additions and 47 deletions
|
@ -29,17 +29,19 @@ function execute(port) {
|
|||
|
||||
let siteConfig = require(CWD + "/siteConfig.js");
|
||||
|
||||
/**
|
||||
* Removes a module from the cache
|
||||
*/
|
||||
function purgeCache(moduleName) {
|
||||
// Traverse the cache looking for the files
|
||||
// loaded by the specified module name
|
||||
searchCache(moduleName, function(mod) {
|
||||
delete require.cache[mod.id];
|
||||
});
|
||||
// remove a module and child modules from require cache, so server does not have
|
||||
// to be restarted
|
||||
function removeFromCache(moduleName) {
|
||||
let mod = require.resolve(moduleName);
|
||||
if (mod && (mod = require.cache[mod])) {
|
||||
(function traverse(mod) {
|
||||
mod.children.forEach(child => {
|
||||
traverse(child);
|
||||
});
|
||||
delete require.cache[mod.id];
|
||||
})(mod);
|
||||
}
|
||||
|
||||
// Remove cached paths to the module.
|
||||
Object.keys(module.constructor._pathCache).forEach(function(cacheKey) {
|
||||
if (cacheKey.indexOf(moduleName) > 0) {
|
||||
delete module.constructor._pathCache[cacheKey];
|
||||
|
@ -47,42 +49,16 @@ function execute(port) {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Traverses the cache to search for all the cached
|
||||
* files of the specified module name
|
||||
*/
|
||||
function searchCache(moduleName, callback) {
|
||||
// Resolve the module identified by the specified name
|
||||
let mod = require.resolve(moduleName);
|
||||
|
||||
// Check if the module has been resolved and found within
|
||||
// the cache
|
||||
if (mod && (mod = require.cache[mod]) !== undefined) {
|
||||
// Recursively go over the results
|
||||
(function traverse(mod) {
|
||||
// Go over each of the module's children and
|
||||
// traverse them
|
||||
mod.children.forEach(function(child) {
|
||||
traverse(child);
|
||||
});
|
||||
|
||||
// Call the specified callback providing the
|
||||
// found cached module
|
||||
callback(mod);
|
||||
})(mod);
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************/
|
||||
|
||||
let readMetadata;
|
||||
let Metadata;
|
||||
|
||||
function reloadMetadata() {
|
||||
purgeCache("./readMetadata.js");
|
||||
removeFromCache("./readMetadata.js");
|
||||
readMetadata = require("./readMetadata.js");
|
||||
readMetadata.generateDocsMetadata();
|
||||
purgeCache("../core/metadata.js");
|
||||
removeFromCache("../core/metadata.js");
|
||||
Metadata = require("../core/metadata.js");
|
||||
}
|
||||
|
||||
|
@ -127,7 +103,7 @@ function execute(port) {
|
|||
|
||||
/* handle all requests for document pages */
|
||||
const app = express().get(/docs\/.*html$/, (req, res, next) => {
|
||||
purgeCache(CWD + "/siteConfig.js");
|
||||
removeFromCache(CWD + "/siteConfig.js");
|
||||
siteConfig = require(CWD + "/siteConfig.js");
|
||||
|
||||
let url = req.path.toString().replace(siteConfig.baseUrl, "");
|
||||
|
@ -222,7 +198,7 @@ function execute(port) {
|
|||
"](" + siteConfig.baseUrl + "docs/assets/"
|
||||
);
|
||||
|
||||
purgeCache("../core/DocsLayout.js");
|
||||
removeFromCache("../core/DocsLayout.js");
|
||||
const DocsLayout = require("../core/DocsLayout.js");
|
||||
const docComp = (
|
||||
<DocsLayout metadata={metadata} language={language} config={siteConfig}>
|
||||
|
@ -235,17 +211,17 @@ function execute(port) {
|
|||
|
||||
/* handle all requests for blog pages and posts */
|
||||
app.get(/blog\/.*html$/, (req, res) => {
|
||||
purgeCache(CWD + "/siteConfig.js");
|
||||
removeFromCache(CWD + "/siteConfig.js");
|
||||
siteConfig = require(CWD + "/siteConfig.js");
|
||||
if (fs.existsSync(__dirname + "/../core/MetadataBlog.js")) {
|
||||
purgeCache("../core/MetadataBlog.js");
|
||||
removeFromCache("../core/MetadataBlog.js");
|
||||
fs.removeSync(__dirname + "/../core/MetadataBlog.js");
|
||||
}
|
||||
readMetadata.generateBlogMetadata();
|
||||
const MetadataBlog = require("../core/MetadataBlog.js");
|
||||
|
||||
/* generate all of the blog pages */
|
||||
purgeCache("../core/BlogPageLayout.js");
|
||||
removeFromCache("../core/BlogPageLayout.js");
|
||||
const BlogPageLayout = require("../core/BlogPageLayout.js");
|
||||
const blogPages = {};
|
||||
/* make blog pages with 10 posts per page */
|
||||
|
@ -304,7 +280,7 @@ function execute(port) {
|
|||
metadata.id = metadata.title;
|
||||
|
||||
let language = "en";
|
||||
purgeCache("../core/BlogPostLayout.js");
|
||||
removeFromCache("../core/BlogPostLayout.js");
|
||||
const BlogPostLayout = require("../core/BlogPostLayout.js");
|
||||
|
||||
const blogPostComp = (
|
||||
|
@ -322,7 +298,7 @@ function execute(port) {
|
|||
|
||||
/* handle all other main pages */
|
||||
app.get("*.html", (req, res, next) => {
|
||||
purgeCache(CWD + "/siteConfig.js");
|
||||
removeFromCache(CWD + "/siteConfig.js");
|
||||
siteConfig = require(CWD + "/siteConfig.js");
|
||||
|
||||
/* look for user provided html file first */
|
||||
|
@ -387,9 +363,9 @@ function execute(port) {
|
|||
fs.copySync(userFile, tempFile);
|
||||
|
||||
/* render into a string */
|
||||
purgeCache(tempFile);
|
||||
removeFromCache(tempFile);
|
||||
const ReactComp = require(tempFile);
|
||||
purgeCache("../core/Site.js");
|
||||
removeFromCache("../core/Site.js");
|
||||
const Site = require("../core/Site.js");
|
||||
translate.setLanguage(language);
|
||||
const str = renderToStaticMarkup(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue