Skip to content

Commit 20c4926

Browse files
committed
Lengthen cache ttl on 404s
1 parent 8479400 commit 20c4926

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

modules/middleware/findFile.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,26 +94,24 @@ function searchEntries(tarballStream, entryName, wantsIndex) {
9494

9595
const chunks = [];
9696

97-
stream
98-
.on('data', chunk => chunks.push(chunk))
99-
.on('end', () => {
100-
const content = Buffer.concat(chunks);
101-
102-
// Set some extra properties for files that we will
103-
// need to serve them and for ?meta listings.
104-
entry.contentType = getContentType(entry.name);
105-
entry.integrity = getIntegrity(content);
106-
entry.lastModified = header.mtime.toUTCString();
107-
entry.size = content.length;
108-
109-
// Set the content only for the foundEntry and
110-
// discard the buffer for all others.
111-
if (entry === foundEntry) {
112-
entry.content = content;
113-
}
97+
stream.on('data', chunk => chunks.push(chunk)).on('end', () => {
98+
const content = Buffer.concat(chunks);
99+
100+
// Set some extra properties for files that we will
101+
// need to serve them and for ?meta listings.
102+
entry.contentType = getContentType(entry.name);
103+
entry.integrity = getIntegrity(content);
104+
entry.lastModified = header.mtime.toUTCString();
105+
entry.size = content.length;
106+
107+
// Set the content only for the foundEntry and
108+
// discard the buffer for all others.
109+
if (entry === foundEntry) {
110+
entry.content = content;
111+
}
114112

115-
next();
116-
});
113+
next();
114+
});
117115
});
118116
});
119117
}
@@ -139,6 +137,10 @@ export default function findFile(req, res, next) {
139137
if (!foundEntry) {
140138
return res
141139
.status(404)
140+
.set({
141+
'Cache-Control': 'public, max-age=31536000', // 1 year
142+
'Cache-Tag': 'missing, missing-entry'
143+
})
142144
.type('text')
143145
.send(`Cannot find "${req.filename}" in ${req.packageSpec}`);
144146
}
@@ -158,6 +160,10 @@ export default function findFile(req, res, next) {
158160
} else {
159161
return res
160162
.status(404)
163+
.set({
164+
'Cache-Control': 'public, max-age=31536000', // 1 year
165+
'Cache-Tag': 'missing, missing-index'
166+
})
161167
.type('text')
162168
.send(
163169
`Cannot find an index in "${req.filename}" in ${

0 commit comments

Comments
 (0)