From e032aec9b92be25a996923361f83a96a89505254 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 18 Jul 2018 09:52:46 +0200 Subject: [PATCH] Render CSS/title/description tags on server side --- client/src/app/app.component.ts | 44 +++-- client/src/index.html | 14 +- scripts/build/client.sh | 19 +- server/controllers/api/config.ts | 3 + server/controllers/client.ts | 150 ++-------------- server/initializers/constants.ts | 9 +- server/lib/client-html.ts | 180 +++++++++++++++++++ server/tests/api/server/config.ts | 26 ++- server/tests/real-world/populate-database.ts | 11 +- server/tests/utils/requests/requests.ts | 8 + 10 files changed, 290 insertions(+), 174 deletions(-) create mode 100644 server/lib/client-html.ts diff --git a/client/src/app/app.component.ts b/client/src/app/app.component.ts index fc4d6c6a2..2149768a2 100644 --- a/client/src/app/app.component.ts +++ b/client/src/app/app.component.ts @@ -4,6 +4,7 @@ import { GuardsCheckStart, NavigationEnd, Router } from '@angular/router' import { AuthService, RedirectService, ServerService } from '@app/core' import { is18nPath } from '../../../shared/models/i18n' import { ScreenService } from '@app/shared/misc/screen.service' +import { skip } from 'rxjs/operators' @Component({ selector: 'my-app', @@ -89,25 +90,36 @@ export class AppComponent implements OnInit { } ) + // Inject JS this.serverService.configLoaded - .subscribe(() => { - const config = this.serverService.getConfig() + .subscribe(() => { + const config = this.serverService.getConfig() - // We test customCSS if the admin removed the css - if (this.customCSS || config.instance.customizations.css) { - const styleTag = '' - this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag) - } - - if (config.instance.customizations.javascript) { - try { - // tslint:disable:no-eval - eval(config.instance.customizations.javascript) - } catch (err) { - console.error('Cannot eval custom JavaScript.', err) + if (config.instance.customizations.javascript) { + try { + // tslint:disable:no-eval + eval(config.instance.customizations.javascript) + } catch (err) { + console.error('Cannot eval custom JavaScript.', err) + } } - } - }) + }) + + // Inject CSS if modified (admin config settings) + this.serverService.configLoaded + .pipe(skip(1)) // We only want to subscribe to reloads, because the CSS is already injected by the server + .subscribe(() => { + const headStyle = document.querySelector('style.custom-css-style') + if (headStyle) headStyle.parentNode.removeChild(headStyle) + + const config = this.serverService.getConfig() + + // We test customCSS if the admin removed the css + if (this.customCSS || config.instance.customizations.css) { + const styleTag = '' + this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag) + } + }) } isUserLoggedIn () { diff --git a/client/src/index.html b/client/src/index.html index a57df3a93..f00af8bff 100644 --- a/client/src/index.html +++ b/client/src/index.html @@ -1,20 +1,22 @@ - PeerTube - - - - - + + + + + + + + diff --git a/scripts/build/client.sh b/scripts/build/client.sh index 106532c4f..e4d053e82 100755 --- a/scripts/build/client.sh +++ b/scripts/build/client.sh @@ -10,16 +10,19 @@ defaultLanguage="en_US" npm run ng build -- --output-path "dist/$defaultLanguage/" --deploy-url "/client/$defaultLanguage/" --prod --stats-json mv "./dist/$defaultLanguage/assets" "./dist" -# Supported languages -languages=("fr_FR" "eu_ES" "ca_ES" "cs_CZ" "eo") +# Don't build other languages if --light arg is provided +if [ -z ${1+x} ] || [ "$1" != "--light" ]; then + # Supported languages + languages=("fr_FR" "eu_ES" "ca_ES" "cs_CZ" "eo") -for lang in "${languages[@]}"; do - npm run ng build -- --prod --i18n-file "./src/locale/target/angular_$lang.xml" --i18n-format xlf --i18n-locale "$lang" \ - --output-path "dist/$lang/" --deploy-url "/client/$lang/" + for lang in "${languages[@]}"; do + npm run ng build -- --prod --i18n-file "./src/locale/target/angular_$lang.xml" --i18n-format xlf --i18n-locale "$lang" \ + --output-path "dist/$lang/" --deploy-url "/client/$lang/" - # Do no duplicate assets - rm -r "./dist/$lang/assets" -done + # Do no duplicate assets + rm -r "./dist/$lang/assets" + done +fi NODE_ENV=production npm run webpack -- --config webpack/webpack.video-embed.js --mode production --json > "./dist/embed-stats.json" diff --git a/server/controllers/api/config.ts b/server/controllers/api/config.ts index 3788975a9..9c1b2818c 100644 --- a/server/controllers/api/config.ts +++ b/server/controllers/api/config.ts @@ -8,6 +8,7 @@ import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/util import { CONFIG, CONSTRAINTS_FIELDS, reloadConfig } from '../../initializers' import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares' import { customConfigUpdateValidator } from '../../middlewares/validators/config' +import { ClientHtml } from '../../lib/client-html' const packageJSON = require('../../../../package.json') const configRouter = express.Router() @@ -119,6 +120,7 @@ async function deleteCustomConfig (req: express.Request, res: express.Response, await unlinkPromise(CONFIG.CUSTOM_FILE) reloadConfig() + ClientHtml.invalidCache() const data = customConfig() @@ -145,6 +147,7 @@ async function updateCustomConfig (req: express.Request, res: express.Response, await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON, undefined, 2)) reloadConfig() + ClientHtml.invalidCache() const data = customConfig() return res.json(data).end() diff --git a/server/controllers/client.ts b/server/controllers/client.ts index 13ca15e9d..352d45fbf 100644 --- a/server/controllers/client.ts +++ b/server/controllers/client.ts @@ -1,21 +1,10 @@ -import * as Bluebird from 'bluebird' import * as express from 'express' -import * as helmet from 'helmet' import { join } from 'path' -import * as validator from 'validator' -import { escapeHTML, readFileBufferPromise, root } from '../helpers/core-utils' -import { ACCEPT_HEADERS, CONFIG, EMBED_SIZE, OPENGRAPH_AND_OEMBED_COMMENT, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers' +import { root } from '../helpers/core-utils' +import { ACCEPT_HEADERS, STATIC_MAX_AGE } from '../initializers' import { asyncMiddleware } from '../middlewares' -import { VideoModel } from '../models/video/video' -import { VideoPrivacy } from '../../shared/models/videos' -import { - buildFileLocale, - getCompleteLocale, - getDefaultLocale, - is18nLocale, - LOCALE_FILES, - POSSIBLE_LOCALES -} from '../../shared/models/i18n/i18n' +import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '../../shared/models/i18n/i18n' +import { ClientHtml } from '../lib/client-html' const clientsRouter = express.Router() @@ -79,7 +68,7 @@ clientsRouter.use('/client/*', (req: express.Request, res: express.Response, nex // Try to provide the right language index.html clientsRouter.use('/(:language)?', function (req, res) { if (req.accepts(ACCEPT_HEADERS) === 'html') { - return res.sendFile(getIndexPath(req, res, req.params.language)) + return generateHTMLPage(req, res, req.params.language) } return res.status(404).end() @@ -93,131 +82,20 @@ export { // --------------------------------------------------------------------------- -function getIndexPath (req: express.Request, res: express.Response, paramLang?: string) { - let lang: string +async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) { + const html = await ClientHtml.getIndexHTML(req, res) - // Check param lang validity - if (paramLang && is18nLocale(paramLang)) { - lang = paramLang - - // Save locale in cookies - res.cookie('clientLanguage', lang, { - secure: CONFIG.WEBSERVER.SCHEME === 'https', - sameSite: true, - maxAge: 1000 * 3600 * 24 * 90 // 3 months - }) - - } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) { - lang = req.cookies.clientLanguage - } else { - lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale() - } - - return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html') + return sendHTML(html, res) } -function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoModel) { - const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName() - const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid +async function generateWatchHtmlPage (req: express.Request, res: express.Response) { + const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res) - const videoNameEscaped = escapeHTML(video.name) - const videoDescriptionEscaped = escapeHTML(video.description) - const embedUrl = CONFIG.WEBSERVER.URL + video.getEmbedStaticPath() - - const openGraphMetaTags = { - 'og:type': 'video', - 'og:title': videoNameEscaped, - 'og:image': previewUrl, - 'og:url': videoUrl, - 'og:description': videoDescriptionEscaped, - - 'og:video:url': embedUrl, - 'og:video:secure_url': embedUrl, - 'og:video:type': 'text/html', - 'og:video:width': EMBED_SIZE.width, - 'og:video:height': EMBED_SIZE.height, - - 'name': videoNameEscaped, - 'description': videoDescriptionEscaped, - 'image': previewUrl, - - 'twitter:card': CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary_large_image', - 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME, - 'twitter:title': videoNameEscaped, - 'twitter:description': videoDescriptionEscaped, - 'twitter:image': previewUrl, - 'twitter:player': embedUrl, - 'twitter:player:width': EMBED_SIZE.width, - 'twitter:player:height': EMBED_SIZE.height - } - - const oembedLinkTags = [ - { - type: 'application/json+oembed', - href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl), - title: videoNameEscaped - } - ] - - const schemaTags = { - '@context': 'http://schema.org', - '@type': 'VideoObject', - name: videoNameEscaped, - description: videoDescriptionEscaped, - thumbnailUrl: previewUrl, - uploadDate: video.createdAt.toISOString(), - duration: video.getActivityStreamDuration(), - contentUrl: videoUrl, - embedUrl: embedUrl, - interactionCount: video.views - } - - let tagsString = '' - - // Opengraph - Object.keys(openGraphMetaTags).forEach(tagName => { - const tagValue = openGraphMetaTags[tagName] - - tagsString += `` - }) - - // OEmbed - for (const oembedLinkTag of oembedLinkTags) { - tagsString += `` - } - - // Schema.org - tagsString += `` - - // SEO - tagsString += `` - - return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString) + return sendHTML(html, res) } -async function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) { - const videoId = '' + req.params.id - let videoPromise: Bluebird +function sendHTML (html: string, res: express.Response) { + res.set('Content-Type', 'text/html; charset=UTF-8') - // Let Angular application handle errors - if (validator.isUUID(videoId, 4)) { - videoPromise = VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(videoId) - } else if (validator.isInt(videoId)) { - videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(+videoId) - } else { - return res.sendFile(getIndexPath(req, res)) - } - - let [ file, video ] = await Promise.all([ - readFileBufferPromise(getIndexPath(req, res)), - videoPromise - ]) - - const html = file.toString() - - // Let Angular application handle errors - if (!video || video.privacy === VideoPrivacy.PRIVATE) return res.sendFile(getIndexPath(req, res)) - - const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video) - res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags) + return res.send(html) } diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index e844c8203..ba48399de 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -466,7 +466,12 @@ const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENT // --------------------------------------------------------------------------- -const OPENGRAPH_AND_OEMBED_COMMENT = '' +const CUSTOM_HTML_TAG_COMMENTS = { + TITLE: '', + DESCRIPTION: '', + CUSTOM_CSS: '', + OPENGRAPH_AND_OEMBED: '' +} // --------------------------------------------------------------------------- @@ -528,7 +533,7 @@ export { JOB_ATTEMPTS, LAST_MIGRATION_VERSION, OAUTH_LIFETIME, - OPENGRAPH_AND_OEMBED_COMMENT, + CUSTOM_HTML_TAG_COMMENTS, BROADCAST_CONCURRENCY, PAGINATION_COUNT_DEFAULT, ACTOR_FOLLOW_SCORE, diff --git a/server/lib/client-html.ts b/server/lib/client-html.ts new file mode 100644 index 000000000..72984e778 --- /dev/null +++ b/server/lib/client-html.ts @@ -0,0 +1,180 @@ +import * as express from 'express' +import * as Bluebird from 'bluebird' +import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/models/i18n/i18n' +import { CONFIG, EMBED_SIZE, CUSTOM_HTML_TAG_COMMENTS, STATIC_PATHS } from '../initializers' +import { join } from 'path' +import { escapeHTML, readFileBufferPromise } from '../helpers/core-utils' +import { VideoModel } from '../models/video/video' +import * as validator from 'validator' +import { VideoPrivacy } from '../../shared/models/videos' + +export class ClientHtml { + + private static htmlCache: { [path: string]: string } = {} + + static invalidCache () { + ClientHtml.htmlCache = {} + } + + static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) { + const path = ClientHtml.getIndexPath(req, res, paramLang) + if (ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path] + + const buffer = await readFileBufferPromise(path) + + let html = buffer.toString() + + html = ClientHtml.addTitleTag(html) + html = ClientHtml.addDescriptionTag(html) + html = ClientHtml.addCustomCSS(html) + + ClientHtml.htmlCache[path] = html + + return html + } + + static async getWatchHTMLPage (videoId: string, req: express.Request, res: express.Response) { + let videoPromise: Bluebird + + // Let Angular application handle errors + if (validator.isUUID(videoId, 4)) { + videoPromise = VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(videoId) + } else if (validator.isInt(videoId)) { + videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(+videoId) + } else { + return ClientHtml.getIndexHTML(req, res) + } + + const [ html, video ] = await Promise.all([ + ClientHtml.getIndexHTML(req, res), + videoPromise + ]) + + // Let Angular application handle errors + if (!video || video.privacy === VideoPrivacy.PRIVATE) { + return ClientHtml.getIndexHTML(req, res) + } + + return ClientHtml.addOpenGraphAndOEmbedTags(html, video) + } + + private static getIndexPath (req: express.Request, res: express.Response, paramLang?: string) { + let lang: string + + // Check param lang validity + if (paramLang && is18nLocale(paramLang)) { + lang = paramLang + + // Save locale in cookies + res.cookie('clientLanguage', lang, { + secure: CONFIG.WEBSERVER.SCHEME === 'https', + sameSite: true, + maxAge: 1000 * 3600 * 24 * 90 // 3 months + }) + + } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) { + lang = req.cookies.clientLanguage + } else { + lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale() + } + + return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html') + } + + private static addTitleTag (htmlStringPage: string) { + const titleTag = '' + CONFIG.INSTANCE.NAME + '' + + return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag) + } + + private static addDescriptionTag (htmlStringPage: string) { + const descriptionTag = `` + + return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag) + } + + private static addCustomCSS (htmlStringPage: string) { + const styleTag = '' + + return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag) + } + + private static addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoModel) { + const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName() + const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid + + const videoNameEscaped = escapeHTML(video.name) + const videoDescriptionEscaped = escapeHTML(video.description) + const embedUrl = CONFIG.WEBSERVER.URL + video.getEmbedStaticPath() + + const openGraphMetaTags = { + 'og:type': 'video', + 'og:title': videoNameEscaped, + 'og:image': previewUrl, + 'og:url': videoUrl, + 'og:description': videoDescriptionEscaped, + + 'og:video:url': embedUrl, + 'og:video:secure_url': embedUrl, + 'og:video:type': 'text/html', + 'og:video:width': EMBED_SIZE.width, + 'og:video:height': EMBED_SIZE.height, + + 'name': videoNameEscaped, + 'description': videoDescriptionEscaped, + 'image': previewUrl, + + 'twitter:card': CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary_large_image', + 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME, + 'twitter:title': videoNameEscaped, + 'twitter:description': videoDescriptionEscaped, + 'twitter:image': previewUrl, + 'twitter:player': embedUrl, + 'twitter:player:width': EMBED_SIZE.width, + 'twitter:player:height': EMBED_SIZE.height + } + + const oembedLinkTags = [ + { + type: 'application/json+oembed', + href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl), + title: videoNameEscaped + } + ] + + const schemaTags = { + '@context': 'http://schema.org', + '@type': 'VideoObject', + name: videoNameEscaped, + description: videoDescriptionEscaped, + thumbnailUrl: previewUrl, + uploadDate: video.createdAt.toISOString(), + duration: video.getActivityStreamDuration(), + contentUrl: videoUrl, + embedUrl: embedUrl, + interactionCount: video.views + } + + let tagsString = '' + + // Opengraph + Object.keys(openGraphMetaTags).forEach(tagName => { + const tagValue = openGraphMetaTags[tagName] + + tagsString += `` + }) + + // OEmbed + for (const oembedLinkTag of oembedLinkTags) { + tagsString += `` + } + + // Schema.org + tagsString += `` + + // SEO + tagsString += `` + + return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.OPENGRAPH_AND_OEMBED, tagsString) + } +} diff --git a/server/tests/api/server/config.ts b/server/tests/api/server/config.ts index 79b5aaf2d..7d21b6ce9 100644 --- a/server/tests/api/server/config.ts +++ b/server/tests/api/server/config.ts @@ -4,7 +4,7 @@ import 'mocha' import * as chai from 'chai' import { About } from '../../../../shared/models/server/about.model' import { CustomConfig } from '../../../../shared/models/server/custom-config.model' -import { deleteCustomConfig, getAbout, killallServers, reRunServer } from '../../utils' +import { deleteCustomConfig, getAbout, killallServers, makeHTMLRequest, reRunServer } from '../../utils' const expect = chai.expect import { @@ -69,6 +69,12 @@ function checkUpdatedConfig (data: CustomConfig) { expect(data.transcoding.resolutions['1080p']).to.be.false } +function checkIndexTags (html: string, title: string, description: string, css: string) { + expect(html).to.contain('' + title + '') + expect(html).to.contain('') + expect(html).to.contain('') +} + describe('Test config', function () { let server = null @@ -109,6 +115,14 @@ describe('Test config', function () { checkInitialConfig(data) }) + it('Should have valid index html tags (title, description...)', async function () { + const res = await makeHTMLRequest(server.url, '/videos/trending') + + const description = 'PeerTube, a federated (ActivityPub) video streaming platform using P2P (BitTorrent) directly in the web browser ' + + 'with WebTorrent and Angular.' + checkIndexTags(res.text, 'PeerTube', description, '') + }) + it('Should update the customized configuration', async function () { const newCustomConfig: CustomConfig = { instance: { @@ -167,6 +181,12 @@ describe('Test config', function () { checkUpdatedConfig(data) }) + it('Should have valid index html updated tags (title, description...)', async function () { + const res = await makeHTMLRequest(server.url, '/videos/trending') + + checkIndexTags(res.text, 'PeerTube updated', 'my short description', 'body { background-color: red; }') + }) + it('Should have the configuration updated after a restart', async function () { this.timeout(10000) @@ -178,6 +198,10 @@ describe('Test config', function () { const data = res.body checkUpdatedConfig(data) + + // Check HTML too + const resHtml = await makeHTMLRequest(server.url, '/videos/trending') + checkIndexTags(resHtml.text, 'PeerTube updated', 'my short description', 'body { background-color: red; }') }) it('Should fetch the about information', async function () { diff --git a/server/tests/real-world/populate-database.ts b/server/tests/real-world/populate-database.ts index 5f93d09db..f0f82f7f8 100644 --- a/server/tests/real-world/populate-database.ts +++ b/server/tests/real-world/populate-database.ts @@ -19,6 +19,12 @@ start() // ---------------------------------------------------------------------------- async function start () { + await flushTests() + + console.log('Flushed tests.') + + const server = await runServer(6) + process.on('exit', async () => { killallServers([ server ]) return @@ -26,11 +32,6 @@ async function start () { process.on('SIGINT', goodbye) process.on('SIGTERM', goodbye) - await flushTests() - - console.log('Flushed tests.') - - const server = await runServer(6) await setAccessTokensToServers([ server ]) console.log('Servers ran.') diff --git a/server/tests/utils/requests/requests.ts b/server/tests/utils/requests/requests.ts index ebde692cd..b88b3ce5b 100644 --- a/server/tests/utils/requests/requests.ts +++ b/server/tests/utils/requests/requests.ts @@ -123,6 +123,13 @@ function makePutBodyRequest (options: { .expect(options.statusCodeExpected) } +function makeHTMLRequest (url: string, path: string) { + return request(url) + .get(path) + .set('Accept', 'text/html') + .expect(200) +} + function updateAvatarRequest (options: { url: string, path: string, @@ -149,6 +156,7 @@ function updateAvatarRequest (options: { // --------------------------------------------------------------------------- export { + makeHTMLRequest, makeGetRequest, makeUploadRequest, makePostBodyRequest,