[DURACOM-172] add check to save only successful response into the SSR cache

This commit is contained in:
Giuseppe Digilio
2023-06-22 18:16:53 +02:00
parent 499bfe3154
commit 6a58e49fb4

View File

@@ -461,6 +461,8 @@ function saveToCache(req, page: any) {
const key = getCacheKey(req);
// Avoid caching "/reload/[random]" paths (these are hard refreshes after logout)
if (key.startsWith('/reload')) { return; }
// Avoid caching not successful responses (status code different from 2XX status)
if (hasNotSucceeded(req.res.statusCode)) { return; }
// Retrieve response headers to save, if any
const headers = retrieveHeaders(req.res);
@@ -479,6 +481,15 @@ function saveToCache(req, page: any) {
}
}
/**
* Check if status code is different from 2XX
* @param statusCode
*/
function hasNotSucceeded(statusCode) {
const rgx = new RegExp(/20+/);
return !rgx.test(statusCode)
}
function retrieveHeaders(response) {
const headers = Object.create({});
if (Array.isArray(environment.cache.serverSide.headers) && environment.cache.serverSide.headers.length > 0) {