aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-07-29 15:20:36 +0200
committerChocobozzz <me@florianbigard.com>2019-07-29 15:20:36 +0200
commitcd4cb177e6e35f69832304366c6f9df96600fb9e (patch)
tree4936951477b39cd471b8a70c96bf79a951d2c6ba /server/controllers
parentc928e1364fbdff87f27fd982710b95426a250491 (diff)
downloadPeerTube-cd4cb177e6e35f69832304366c6f9df96600fb9e.tar.gz
PeerTube-cd4cb177e6e35f69832304366c6f9df96600fb9e.tar.zst
PeerTube-cd4cb177e6e35f69832304366c6f9df96600fb9e.zip
Improve static files cache
Diffstat (limited to 'server/controllers')
-rw-r--r--server/controllers/client.ts16
-rw-r--r--server/controllers/static.ts4
2 files changed, 11 insertions, 9 deletions
diff --git a/server/controllers/client.ts b/server/controllers/client.ts
index 18b8b58e9..51f610afa 100644
--- a/server/controllers/client.ts
+++ b/server/controllers/client.ts
@@ -1,7 +1,7 @@
1import * as express from 'express' 1import * as express from 'express'
2import { join } from 'path' 2import { join } from 'path'
3import { root } from '../helpers/core-utils' 3import { root } from '../helpers/core-utils'
4import { ACCEPT_HEADERS, STATIC_MAX_AGE } from '../initializers/constants' 4import { ACCEPT_HEADERS, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers/constants'
5import { asyncMiddleware, embedCSP } from '../middlewares' 5import { asyncMiddleware, embedCSP } from '../middlewares'
6import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '../../shared/models/i18n/i18n' 6import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '../../shared/models/i18n/i18n'
7import { ClientHtml } from '../lib/client-html' 7import { ClientHtml } from '../lib/client-html'
@@ -10,7 +10,6 @@ import { logger } from '../helpers/logger'
10const clientsRouter = express.Router() 10const clientsRouter = express.Router()
11 11
12const distPath = join(root(), 'client', 'dist') 12const distPath = join(root(), 'client', 'dist')
13const assetsImagesPath = join(root(), 'client', 'dist', 'assets', 'images')
14const embedPath = join(distPath, 'standalone', 'videos', 'embed.html') 13const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
15const testEmbedPath = join(distPath, 'standalone', 'videos', 'test-embed.html') 14const testEmbedPath = join(distPath, 'standalone', 'videos', 'test-embed.html')
16 15
@@ -42,13 +41,14 @@ const staticClientFiles = [
42] 41]
43for (const staticClientFile of staticClientFiles) { 42for (const staticClientFile of staticClientFiles) {
44 const path = join(root(), 'client', 'dist', staticClientFile) 43 const path = join(root(), 'client', 'dist', staticClientFile)
45 clientsRouter.use('/' + staticClientFile, express.static(path, { maxAge: STATIC_MAX_AGE }))
46}
47 44
48clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE })) 45 clientsRouter.get('/' + staticClientFile, (req: express.Request, res: express.Response) => {
49clientsRouter.use('/client/assets/images', express.static(assetsImagesPath, { maxAge: STATIC_MAX_AGE })) 46 res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
47 })
48}
50 49
51clientsRouter.use('/client/locales/:locale/:file.json', serveServerTranslations) 50clientsRouter.use('/client/locales/:locale/:file.json', serveServerTranslations)
51clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE.CLIENT }))
52 52
53// 404 for static files not found 53// 404 for static files not found
54clientsRouter.use('/client/*', (req: express.Request, res: express.Response) => { 54clientsRouter.use('/client/*', (req: express.Request, res: express.Response) => {
@@ -74,7 +74,9 @@ async function serveServerTranslations (req: express.Request, res: express.Respo
74 if (is18nLocale(locale) && LOCALE_FILES.indexOf(file) !== -1) { 74 if (is18nLocale(locale) && LOCALE_FILES.indexOf(file) !== -1) {
75 const completeLocale = getCompleteLocale(locale) 75 const completeLocale = getCompleteLocale(locale)
76 const completeFileLocale = buildFileLocale(completeLocale) 76 const completeFileLocale = buildFileLocale(completeLocale)
77 return res.sendFile(join(__dirname, `../../../client/dist/locale/${file}_${completeFileLocale}.json`)) 77
78 const path = join(__dirname, `../../../client/dist/locale/${file}_${completeFileLocale}.json`)
79 return res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
78 } 80 }
79 81
80 return res.sendStatus(404) 82 return res.sendStatus(404)
diff --git a/server/controllers/static.ts b/server/controllers/static.ts
index af7de562a..4296183a0 100644
--- a/server/controllers/static.ts
+++ b/server/controllers/static.ts
@@ -68,13 +68,13 @@ staticRouter.use(
68const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR 68const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
69staticRouter.use( 69staticRouter.use(
70 STATIC_PATHS.THUMBNAILS, 70 STATIC_PATHS.THUMBNAILS,
71 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist 71 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist
72) 72)
73 73
74const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR 74const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
75staticRouter.use( 75staticRouter.use(
76 STATIC_PATHS.AVATARS, 76 STATIC_PATHS.AVATARS,
77 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist 77 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist
78) 78)
79 79
80// We don't have video previews, fetch them from the origin instance 80// We don't have video previews, fetch them from the origin instance