Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UBERF-6121: Fix front service caching #5029

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions server/front/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"@types/jest": "^29.5.5",
"@types/morgan": "~1.9.9",
"@types/apicache": "^1.6.6"
"@types/morgan": "~1.9.9"
},
"dependencies": {
"@hcengineering/core": "^0.6.28",
Expand All @@ -57,7 +56,6 @@
"body-parser": "^1.20.2",
"sharp": "~0.32.0",
"@hcengineering/minio": "^0.6.0",
"morgan": "^1.10.0",
"apicache": "^1.6.3"
"morgan": "^1.10.0"
}
}
63 changes: 29 additions & 34 deletions server/front/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,21 @@
import { MeasureContext, WorkspaceId, metricsAggregate } from '@hcengineering/core'
import { MinioService } from '@hcengineering/minio'
import { Token, decodeToken } from '@hcengineering/server-token'
import apicache from 'apicache'
import bp from 'body-parser'
import cors from 'cors'
import express, { Request, Response } from 'express'
import fileUpload, { UploadedFile } from 'express-fileupload'
import expressStaticGzip from 'express-static-gzip'
import https from 'https'
import morgan from 'morgan'
import { extname, join, resolve } from 'path'
import { join, resolve } from 'path'
import { cwd } from 'process'
import sharp from 'sharp'
import { v4 as uuid } from 'uuid'
import { preConditions } from './utils'

const cacheControlValue = 'public, max-age=365d'
const cacheControlMaxAge = '365d'
const cacheControlNoCache = 'max-age=1d, no-cache, must-revalidate'

async function minioUpload (
ctx: MeasureContext,
Expand All @@ -47,8 +46,9 @@ async function minioUpload (

const resp = await ctx.with(
'storage upload',
{ file: file.name, contentType: file.mimetype },
async () => await minio.put(workspace, id, file.data, file.size, meta)
{ workspace: workspace.name },
async () => await minio.put(workspace, id, file.data, file.size, meta),
{ file: file.name, contentType: file.mimetype }
)

await ctx.info('minio upload', resp)
Expand Down Expand Up @@ -243,10 +243,6 @@ export function start (
): () => void {
const app = express()

const cache = apicache.options({
respectCacheControl: true
}).middleware

app.use(cors())
app.use(fileUpload())
app.use(bp.json())
Expand Down Expand Up @@ -280,7 +276,7 @@ export function start (
LAST_NAME_FIRST: config.lastNameFirst,
...(extraConfig ?? {})
}
res.set('Cache-Control', `${cacheControlValue}, must-revalidate`)
res.set('Cache-Control', cacheControlNoCache)
res.status(200)
res.json(data)
})
Expand Down Expand Up @@ -312,12 +308,12 @@ export function start (
console.log('serving static files from', dist)

app.use(
cache('1 day'),
expressStaticGzip(dist, {
serveStatic: {
maxAge: '365d',
etag: true,
lastModified: true
lastModified: true,
index: false
}
})
)
Expand Down Expand Up @@ -706,35 +702,34 @@ export function start (
}
})

const filesPatterns = [
'.js',
'.js.gz',
'js.map',
'js.map.gz',
'.woff',
'.woff2',
'.svg.gz',
'.css',
'.css.gz',
'.ico',
'.svg',
'.webp',
'.png',
'.avif'
]

app.get('*', function (request, response) {
const url = request.path.split('/').filter((it) => it !== '')
if (url.length === 1) {
const ext = extname(url[0])
const notFoundResource = [
'.js',
'.js.gz',
'.svg',
'.webp',
'.woff',
'.woff2',
'.svg.gz',
'.css',
'.css.gz',
'.png',
'.avif'
]
if (notFoundResource.includes(ext)) {
response.sendStatus(404)
return
}
if (filesPatterns.some((it) => request.path.endsWith(it))) {
response.sendStatus(404)
return
}
response.sendFile(join(dist, 'index.html'), {
maxAge: cacheControlMaxAge,
etag: true,
lastModified: true,
cacheControl: false,
headers: {
'Cache-Control': `${cacheControlValue}, must-revalidate`
'Cache-Control': cacheControlNoCache
}
})
})
Expand Down