1
0
mirror of https://github.com/trambarhq/relaks-wordpress-example.git synced 2025-09-02 04:32:37 +02:00

Improved cache purging.

This commit is contained in:
Chung Leong
2019-02-06 12:48:51 +01:00
parent 3b3e23c4d8
commit a8785a2132

View File

@@ -138,6 +138,7 @@ async function handlePurgeRequest(req, res) {
if (method === 'regex' && url === '/.*') {
pageDependencies = {};
await NginxCache.purge(/.*/);
await PageRenderer.prefetch('/');
} else if (method === 'default') {
// look for URLs that looks like /wp-json/wp/v2/pages/4/
let m = /^\/wp\-json\/(\w+\/\w+\/\w+)\/(\d+)\/$/.exec(url);
@@ -148,11 +149,7 @@ async function handlePurgeRequest(req, res) {
// purge matching JSON files
let folderPath = m[1];
let pattern = new RegExp(`^/json/${folderPath}.*`);
let purgedURLs = await NginxCache.purge(pattern);
if (purgedURLs.length === 0) {
return;
}
purgedURLs = purgedURLs.map(addTrailingSlash);
await NginxCache.purge(pattern);
// purge the timestamp so CSR code knows something has changed
await NginxCache.purge('/.mtime');
@@ -160,16 +157,19 @@ async function handlePurgeRequest(req, res) {
// look for pages that made use of the purged JSONs
for (let [ path, sourceURLs ] of Object.entries(pageDependencies)) {
let affected = sourceURLs.some((sourceURL) => {
return purgedURLs.indexOf(sourceURL) !== -1;
return pattern.test(sourceURL);
});
if (affected) {
// purge the cached page
await NginxCache.purge(path);
delete pageDependencies[path];
if (path === '/') {
await PageRenderer.prefetch('/');
}
}
}
}
await PageRenderer.prefetch('/');
}
function handleError(err, req, res, next) {