diff --git a/server.ts b/server.ts index 3e10677a8b1..6f61b8f20f9 100644 --- a/server.ts +++ b/server.ts @@ -443,6 +443,10 @@ 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 non-successful responses (status code different from 2XX). Without this, the + // rendered 404 not-found page gets stored in the cache and is later replayed via res.send() + // as HTTP 200 - turning a correct 404 into a soft-404. (matches dtq-dev cache behaviour) + if (hasNotSucceeded(req.res.statusCode)) { return; } // If bot cache is enabled, save it to that cache if it doesn't exist or is expired // (NOTE: has() will return false if page is expired in cache) @@ -459,6 +463,15 @@ function saveToCache(req, page: any) { } } +/** + * Check if status code is different from 2XX + * @param statusCode HTTP status code of the current response + */ +function hasNotSucceeded(statusCode) { + const rgx = new RegExp(/^20+/); + return !rgx.test(statusCode); +} + /** * Whether a user is authenticated or not */