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");
|
let siteConfig = require(CWD + "/siteConfig.js");
|
||||||
|
|
||||||
/**
|
// remove a module and child modules from require cache, so server does not have
|
||||||
* Removes a module from the cache
|
// to be restarted
|
||||||
*/
|
function removeFromCache(moduleName) {
|
||||||
function purgeCache(moduleName) {
|
let mod = require.resolve(moduleName);
|
||||||
// Traverse the cache looking for the files
|
if (mod && (mod = require.cache[mod])) {
|
||||||
// loaded by the specified module name
|
(function traverse(mod) {
|
||||||
searchCache(moduleName, function(mod) {
|
mod.children.forEach(child => {
|
||||||
delete require.cache[mod.id];
|
traverse(child);
|
||||||
});
|
});
|
||||||
|
delete require.cache[mod.id];
|
||||||
|
})(mod);
|
||||||
|
}
|
||||||
|
|
||||||
// Remove cached paths to the module.
|
|
||||||
Object.keys(module.constructor._pathCache).forEach(function(cacheKey) {
|
Object.keys(module.constructor._pathCache).forEach(function(cacheKey) {
|
||||||
if (cacheKey.indexOf(moduleName) > 0) {
|
if (cacheKey.indexOf(moduleName) > 0) {
|
||||||
delete module.constructor._pathCache[cacheKey];
|
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 readMetadata;
|
||||||
let Metadata;
|
let Metadata;
|
||||||
|
|
||||||
function reloadMetadata() {
|
function reloadMetadata() {
|
||||||
purgeCache("./readMetadata.js");
|
removeFromCache("./readMetadata.js");
|
||||||
readMetadata = require("./readMetadata.js");
|
readMetadata = require("./readMetadata.js");
|
||||||
readMetadata.generateDocsMetadata();
|
readMetadata.generateDocsMetadata();
|
||||||
purgeCache("../core/metadata.js");
|
removeFromCache("../core/metadata.js");
|
||||||
Metadata = require("../core/metadata.js");
|
Metadata = require("../core/metadata.js");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,7 +103,7 @@ function execute(port) {
|
||||||
|
|
||||||
/* handle all requests for document pages */
|
/* handle all requests for document pages */
|
||||||
const app = express().get(/docs\/.*html$/, (req, res, next) => {
|
const app = express().get(/docs\/.*html$/, (req, res, next) => {
|
||||||
purgeCache(CWD + "/siteConfig.js");
|
removeFromCache(CWD + "/siteConfig.js");
|
||||||
siteConfig = require(CWD + "/siteConfig.js");
|
siteConfig = require(CWD + "/siteConfig.js");
|
||||||
|
|
||||||
let url = req.path.toString().replace(siteConfig.baseUrl, "");
|
let url = req.path.toString().replace(siteConfig.baseUrl, "");
|
||||||
|
@ -222,7 +198,7 @@ function execute(port) {
|
||||||
"](" + siteConfig.baseUrl + "docs/assets/"
|
"](" + siteConfig.baseUrl + "docs/assets/"
|
||||||
);
|
);
|
||||||
|
|
||||||
purgeCache("../core/DocsLayout.js");
|
removeFromCache("../core/DocsLayout.js");
|
||||||
const DocsLayout = require("../core/DocsLayout.js");
|
const DocsLayout = require("../core/DocsLayout.js");
|
||||||
const docComp = (
|
const docComp = (
|
||||||
<DocsLayout metadata={metadata} language={language} config={siteConfig}>
|
<DocsLayout metadata={metadata} language={language} config={siteConfig}>
|
||||||
|
@ -235,17 +211,17 @@ function execute(port) {
|
||||||
|
|
||||||
/* handle all requests for blog pages and posts */
|
/* handle all requests for blog pages and posts */
|
||||||
app.get(/blog\/.*html$/, (req, res) => {
|
app.get(/blog\/.*html$/, (req, res) => {
|
||||||
purgeCache(CWD + "/siteConfig.js");
|
removeFromCache(CWD + "/siteConfig.js");
|
||||||
siteConfig = require(CWD + "/siteConfig.js");
|
siteConfig = require(CWD + "/siteConfig.js");
|
||||||
if (fs.existsSync(__dirname + "/../core/MetadataBlog.js")) {
|
if (fs.existsSync(__dirname + "/../core/MetadataBlog.js")) {
|
||||||
purgeCache("../core/MetadataBlog.js");
|
removeFromCache("../core/MetadataBlog.js");
|
||||||
fs.removeSync(__dirname + "/../core/MetadataBlog.js");
|
fs.removeSync(__dirname + "/../core/MetadataBlog.js");
|
||||||
}
|
}
|
||||||
readMetadata.generateBlogMetadata();
|
readMetadata.generateBlogMetadata();
|
||||||
const MetadataBlog = require("../core/MetadataBlog.js");
|
const MetadataBlog = require("../core/MetadataBlog.js");
|
||||||
|
|
||||||
/* generate all of the blog pages */
|
/* generate all of the blog pages */
|
||||||
purgeCache("../core/BlogPageLayout.js");
|
removeFromCache("../core/BlogPageLayout.js");
|
||||||
const BlogPageLayout = require("../core/BlogPageLayout.js");
|
const BlogPageLayout = require("../core/BlogPageLayout.js");
|
||||||
const blogPages = {};
|
const blogPages = {};
|
||||||
/* make blog pages with 10 posts per page */
|
/* make blog pages with 10 posts per page */
|
||||||
|
@ -304,7 +280,7 @@ function execute(port) {
|
||||||
metadata.id = metadata.title;
|
metadata.id = metadata.title;
|
||||||
|
|
||||||
let language = "en";
|
let language = "en";
|
||||||
purgeCache("../core/BlogPostLayout.js");
|
removeFromCache("../core/BlogPostLayout.js");
|
||||||
const BlogPostLayout = require("../core/BlogPostLayout.js");
|
const BlogPostLayout = require("../core/BlogPostLayout.js");
|
||||||
|
|
||||||
const blogPostComp = (
|
const blogPostComp = (
|
||||||
|
@ -322,7 +298,7 @@ function execute(port) {
|
||||||
|
|
||||||
/* handle all other main pages */
|
/* handle all other main pages */
|
||||||
app.get("*.html", (req, res, next) => {
|
app.get("*.html", (req, res, next) => {
|
||||||
purgeCache(CWD + "/siteConfig.js");
|
removeFromCache(CWD + "/siteConfig.js");
|
||||||
siteConfig = require(CWD + "/siteConfig.js");
|
siteConfig = require(CWD + "/siteConfig.js");
|
||||||
|
|
||||||
/* look for user provided html file first */
|
/* look for user provided html file first */
|
||||||
|
@ -387,9 +363,9 @@ function execute(port) {
|
||||||
fs.copySync(userFile, tempFile);
|
fs.copySync(userFile, tempFile);
|
||||||
|
|
||||||
/* render into a string */
|
/* render into a string */
|
||||||
purgeCache(tempFile);
|
removeFromCache(tempFile);
|
||||||
const ReactComp = require(tempFile);
|
const ReactComp = require(tempFile);
|
||||||
purgeCache("../core/Site.js");
|
removeFromCache("../core/Site.js");
|
||||||
const Site = require("../core/Site.js");
|
const Site = require("../core/Site.js");
|
||||||
translate.setLanguage(language);
|
translate.setLanguage(language);
|
||||||
const str = renderToStaticMarkup(
|
const str = renderToStaticMarkup(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue