Skip to content

Commit

Permalink
UBERF-7425: Fix some CF caching issues (#5936)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Sobolev <[email protected]>
  • Loading branch information
haiodo authored Jun 27, 2024
1 parent 9217aff commit f9aabe9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
12 changes: 10 additions & 2 deletions server/front/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,12 @@ async function getFile (
preConditions.IfModifiedSince(req.headers, { lastModified: new Date(stat.modifiedOn) }) === 'notModified'
) {
// Matched, return not modified
res.statusCode = 304
res.writeHead(304, {
'content-type': stat.contentType,
etag: stat.etag,
'last-modified': new Date(stat.modifiedOn).toISOString(),
'cache-control': cacheControlValue
})
res.end()
return
}
Expand Down Expand Up @@ -375,7 +380,10 @@ export function start (
res.status(404).send()
return
}
const isImage = blobInfo.contentType.includes('image/')

// try image and octet streams
const isImage =
blobInfo.contentType.includes('image/') || blobInfo.contentType.includes('application/octet-stream')

if (token === undefined) {
if (blobInfo !== undefined && !isImage) {
Expand Down
10 changes: 5 additions & 5 deletions server/front/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Request } from 'express'
import type { IncomingMessage } from 'http'

export const ETagSupport = {
isWeak (etag: string): boolean {
Expand Down Expand Up @@ -30,7 +30,7 @@ function toList (value: string): string[] {
}

export const preConditions = {
IfMatch: (headers: Request['headers'], state: { etag: string }): 'fetch' | 'notModified' => {
IfMatch: (headers: IncomingMessage['headers'], state: { etag: string }): 'fetch' | 'notModified' => {
const header = (headers as any)['if-match']
if (header == null) {
return 'fetch'
Expand All @@ -40,7 +40,7 @@ export const preConditions = {
}
return toList(header).some((etag) => ETagSupport.strongMatch(etag, state.etag)) ? 'notModified' : 'fetch'
},
IfNoneMatch: (headers: Request['headers'], state: { etag: string }): 'fetch' | 'notModified' => {
IfNoneMatch: (headers: IncomingMessage['headers'], state: { etag: string }): 'fetch' | 'notModified' => {
const header = (headers as any)['if-none-match']
if (header == null) {
return 'fetch'
Expand All @@ -52,7 +52,7 @@ export const preConditions = {
return toList(header).some((etag) => ETagSupport.weakMatch(etag, state.etag)) ? 'notModified' : 'fetch'
}
},
IfModifiedSince: (headers: Request['headers'], state: { lastModified: Date }): 'fetch' | 'notModified' => {
IfModifiedSince: (headers: IncomingMessage['headers'], state: { lastModified: Date }): 'fetch' | 'notModified' => {
if ((headers as any)['if-none-match'] != null) {
return 'fetch'
}
Expand All @@ -67,7 +67,7 @@ export const preConditions = {
}
return state.lastModified.getTime() <= date ? 'notModified' : 'fetch'
},
IfUnmodifiedSince: (headers: Request['headers'], state: { lastModified: Date }): 'fetch' | 'failed' => {
IfUnmodifiedSince: (headers: IncomingMessage['headers'], state: { lastModified: Date }): 'fetch' | 'failed' => {
if ((headers as any)['if-match'] != null) {
return 'fetch'
}
Expand Down

0 comments on commit f9aabe9

Please sign in to comment.