mirror of
https://github.com/trambarhq/relaks-wordpress-example.git
synced 2025-09-02 20:52:33 +02:00
Improved cache purging.
This commit is contained in:
@@ -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) {
|
||||
|
Reference in New Issue
Block a user