]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/client.ts
Don't cache embed HTML file
[github/Chocobozzz/PeerTube.git] / server / controllers / client.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
65fcc311 2import { join } from 'path'
e032aec9 3import { root } from '../helpers/core-utils'
34dd7cb4 4import { ACCEPT_HEADERS, STATIC_MAX_AGE } from '../initializers/constants'
dfab4fa9 5import { asyncMiddleware, embedCSP } from '../middlewares'
e032aec9
C
6import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '../../shared/models/i18n/i18n'
7import { ClientHtml } from '../lib/client-html'
57c36b27 8import { logger } from '../helpers/logger'
dfab4fa9 9import { CONFIG } from '@server/initializers/config'
830bcd0f 10
65fcc311 11const clientsRouter = express.Router()
830bcd0f 12
e02643f3
C
13const distPath = join(root(), 'client', 'dist')
14const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
99941732 15const testEmbedPath = join(distPath, 'standalone', 'videos', 'test-embed.html')
830bcd0f 16
d8755eed 17// Special route that add OpenGraph and oEmbed tags
830bcd0f 18// Do not use a template engine for a so little thing
9aac4423 19clientsRouter.use('/videos/watch/:id', asyncMiddleware(generateWatchHtmlPage))
92bf2f62
C
20clientsRouter.use('/accounts/:nameWithHost', asyncMiddleware(generateAccountHtmlPage))
21clientsRouter.use('/video-channels/:nameWithHost', asyncMiddleware(generateVideoChannelHtmlPage))
830bcd0f 22
dfab4fa9
C
23const embedCSPMiddleware = CONFIG.CSP.ENABLED
24 ? embedCSP
25 : (req: express.Request, res: express.Response, next: express.NextFunction) => next()
26
9aac4423 27clientsRouter.use(
d00e2393 28 '/videos/embed',
dfab4fa9 29 embedCSPMiddleware,
9aac4423 30 (req: express.Request, res: express.Response) => {
d00e2393 31 res.removeHeader('X-Frame-Options')
78646451
C
32 // Don't cache HTML file since it's an index to the immutable JS/CSS files
33 res.sendFile(embedPath, { maxAge: 0 })
d00e2393
RK
34 }
35)
9aac4423
C
36clientsRouter.use(
37 '/videos/test-embed',
38 (req: express.Request, res: express.Response) => res.sendFile(testEmbedPath)
39)
830bcd0f 40
79530164 41// Static HTML/CSS/JS client files
78967fca
C
42
43const staticClientFiles = [
fcc7c060 44 'manifest.webmanifest',
78967fca
C
45 'ngsw-worker.js',
46 'ngsw.json'
47]
48for (const staticClientFile of staticClientFiles) {
49 const path = join(root(), 'client', 'dist', staticClientFile)
78967fca 50
cd4cb177
C
51 clientsRouter.get('/' + staticClientFile, (req: express.Request, res: express.Response) => {
52 res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
53 })
54}
79530164 55
552d95b1 56clientsRouter.use('/client/locales/:locale/:file.json', serveServerTranslations)
cd4cb177 57clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE.CLIENT }))
552d95b1
C
58
59// 404 for static files not found
60clientsRouter.use('/client/*', (req: express.Request, res: express.Response) => {
61 res.sendStatus(404)
62})
63
64// Always serve index client page (the client is a single page application, let it handle routing)
65// Try to provide the right language index.html
66clientsRouter.use('/(:language)?', asyncMiddleware(serveIndexHTML))
67
68// ---------------------------------------------------------------------------
69
70export {
71 clientsRouter
72}
73
74// ---------------------------------------------------------------------------
75
a1587156 76function serveServerTranslations (req: express.Request, res: express.Response) {
7ce44a74
C
77 const locale = req.params.locale
78 const file = req.params.file
79
bdd428a6 80 if (is18nLocale(locale) && LOCALE_FILES.includes(file)) {
74b7c6d4
C
81 const completeLocale = getCompleteLocale(locale)
82 const completeFileLocale = buildFileLocale(completeLocale)
cd4cb177 83
350131cb 84 const path = join(__dirname, `../../../client/dist/locale/${file}.${completeFileLocale}.json`)
cd4cb177 85 return res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
e945b184
C
86 }
87
88 return res.sendStatus(404)
552d95b1 89}
79530164 90
552d95b1 91async function serveIndexHTML (req: express.Request, res: express.Response) {
989e526a 92 if (req.accepts(ACCEPT_HEADERS) === 'html') {
57c36b27
C
93 try {
94 await generateHTMLPage(req, res, req.params.language)
95 return
96 } catch (err) {
97 logger.error('Cannot generate HTML page.', err)
98 }
989e526a
C
99 }
100
101 return res.status(404).end()
65fcc311 102}
830bcd0f 103
e032aec9 104async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
9aac4423 105 const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang)
989e526a 106
e032aec9 107 return sendHTML(html, res)
989e526a
C
108}
109
e032aec9
C
110async function generateWatchHtmlPage (req: express.Request, res: express.Response) {
111 const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res)
830bcd0f 112
e032aec9 113 return sendHTML(html, res)
830bcd0f
C
114}
115
92bf2f62
C
116async function generateAccountHtmlPage (req: express.Request, res: express.Response) {
117 const html = await ClientHtml.getAccountHTMLPage(req.params.nameWithHost, req, res)
118
119 return sendHTML(html, res)
120}
121
122async function generateVideoChannelHtmlPage (req: express.Request, res: express.Response) {
123 const html = await ClientHtml.getVideoChannelHTMLPage(req.params.nameWithHost, req, res)
124
125 return sendHTML(html, res)
126}
127
e032aec9
C
128function sendHTML (html: string, res: express.Response) {
129 res.set('Content-Type', 'text/html; charset=UTF-8')
eb080476 130
e032aec9 131 return res.send(html)
830bcd0f 132}