mirror of
https://github.com/trambarhq/relaks-wordpress-example.git
synced 2025-09-02 12:42:38 +02:00
Improved cache purging.
This commit is contained in:
@@ -138,6 +138,7 @@ async function handlePurgeRequest(req, res) {
|
|||||||
if (method === 'regex' && url === '/.*') {
|
if (method === 'regex' && url === '/.*') {
|
||||||
pageDependencies = {};
|
pageDependencies = {};
|
||||||
await NginxCache.purge(/.*/);
|
await NginxCache.purge(/.*/);
|
||||||
|
await PageRenderer.prefetch('/');
|
||||||
} else if (method === 'default') {
|
} else if (method === 'default') {
|
||||||
// look for URLs that looks like /wp-json/wp/v2/pages/4/
|
// look for URLs that looks like /wp-json/wp/v2/pages/4/
|
||||||
let m = /^\/wp\-json\/(\w+\/\w+\/\w+)\/(\d+)\/$/.exec(url);
|
let m = /^\/wp\-json\/(\w+\/\w+\/\w+)\/(\d+)\/$/.exec(url);
|
||||||
@@ -148,11 +149,7 @@ async function handlePurgeRequest(req, res) {
|
|||||||
// purge matching JSON files
|
// purge matching JSON files
|
||||||
let folderPath = m[1];
|
let folderPath = m[1];
|
||||||
let pattern = new RegExp(`^/json/${folderPath}.*`);
|
let pattern = new RegExp(`^/json/${folderPath}.*`);
|
||||||
let purgedURLs = await NginxCache.purge(pattern);
|
await NginxCache.purge(pattern);
|
||||||
if (purgedURLs.length === 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
purgedURLs = purgedURLs.map(addTrailingSlash);
|
|
||||||
|
|
||||||
// purge the timestamp so CSR code knows something has changed
|
// purge the timestamp so CSR code knows something has changed
|
||||||
await NginxCache.purge('/.mtime');
|
await NginxCache.purge('/.mtime');
|
||||||
@@ -160,16 +157,19 @@ async function handlePurgeRequest(req, res) {
|
|||||||
// look for pages that made use of the purged JSONs
|
// look for pages that made use of the purged JSONs
|
||||||
for (let [ path, sourceURLs ] of Object.entries(pageDependencies)) {
|
for (let [ path, sourceURLs ] of Object.entries(pageDependencies)) {
|
||||||
let affected = sourceURLs.some((sourceURL) => {
|
let affected = sourceURLs.some((sourceURL) => {
|
||||||
return purgedURLs.indexOf(sourceURL) !== -1;
|
return pattern.test(sourceURL);
|
||||||
});
|
});
|
||||||
if (affected) {
|
if (affected) {
|
||||||
// purge the cached page
|
// purge the cached page
|
||||||
await NginxCache.purge(path);
|
await NginxCache.purge(path);
|
||||||
delete pageDependencies[path];
|
delete pageDependencies[path];
|
||||||
|
|
||||||
|
if (path === '/') {
|
||||||
|
await PageRenderer.prefetch('/');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await PageRenderer.prefetch('/');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleError(err, req, res, next) {
|
function handleError(err, req, res, next) {
|
||||||
|
Reference in New Issue
Block a user