Skip to content

Commit f9aabe9

Browse files
authored
UBERF-7425: Fix some CF caching issues (#5936)
Signed-off-by: Andrey Sobolev <[email protected]>
1 parent 9217aff commit f9aabe9

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

server/front/src/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,12 @@ async function getFile (
168168
preConditions.IfModifiedSince(req.headers, { lastModified: new Date(stat.modifiedOn) }) === 'notModified'
169169
) {
170170
// Matched, return not modified
171-
res.statusCode = 304
171+
res.writeHead(304, {
172+
'content-type': stat.contentType,
173+
etag: stat.etag,
174+
'last-modified': new Date(stat.modifiedOn).toISOString(),
175+
'cache-control': cacheControlValue
176+
})
172177
res.end()
173178
return
174179
}
@@ -375,7 +380,10 @@ export function start (
375380
res.status(404).send()
376381
return
377382
}
378-
const isImage = blobInfo.contentType.includes('image/')
383+
384+
// try image and octet streams
385+
const isImage =
386+
blobInfo.contentType.includes('image/') || blobInfo.contentType.includes('application/octet-stream')
379387

380388
if (token === undefined) {
381389
if (blobInfo !== undefined && !isImage) {

server/front/src/utils.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Request } from 'express'
1+
import type { IncomingMessage } from 'http'
22

33
export const ETagSupport = {
44
isWeak (etag: string): boolean {
@@ -30,7 +30,7 @@ function toList (value: string): string[] {
3030
}
3131

3232
export const preConditions = {
33-
IfMatch: (headers: Request['headers'], state: { etag: string }): 'fetch' | 'notModified' => {
33+
IfMatch: (headers: IncomingMessage['headers'], state: { etag: string }): 'fetch' | 'notModified' => {
3434
const header = (headers as any)['if-match']
3535
if (header == null) {
3636
return 'fetch'
@@ -40,7 +40,7 @@ export const preConditions = {
4040
}
4141
return toList(header).some((etag) => ETagSupport.strongMatch(etag, state.etag)) ? 'notModified' : 'fetch'
4242
},
43-
IfNoneMatch: (headers: Request['headers'], state: { etag: string }): 'fetch' | 'notModified' => {
43+
IfNoneMatch: (headers: IncomingMessage['headers'], state: { etag: string }): 'fetch' | 'notModified' => {
4444
const header = (headers as any)['if-none-match']
4545
if (header == null) {
4646
return 'fetch'
@@ -52,7 +52,7 @@ export const preConditions = {
5252
return toList(header).some((etag) => ETagSupport.weakMatch(etag, state.etag)) ? 'notModified' : 'fetch'
5353
}
5454
},
55-
IfModifiedSince: (headers: Request['headers'], state: { lastModified: Date }): 'fetch' | 'notModified' => {
55+
IfModifiedSince: (headers: IncomingMessage['headers'], state: { lastModified: Date }): 'fetch' | 'notModified' => {
5656
if ((headers as any)['if-none-match'] != null) {
5757
return 'fetch'
5858
}
@@ -67,7 +67,7 @@ export const preConditions = {
6767
}
6868
return state.lastModified.getTime() <= date ? 'notModified' : 'fetch'
6969
},
70-
IfUnmodifiedSince: (headers: Request['headers'], state: { lastModified: Date }): 'fetch' | 'failed' => {
70+
IfUnmodifiedSince: (headers: IncomingMessage['headers'], state: { lastModified: Date }): 'fetch' | 'failed' => {
7171
if ((headers as any)['if-match'] != null) {
7272
return 'fetch'
7373
}

0 commit comments

Comments
 (0)