diff options
author | Chocobozzz <me@florianbigard.com> | 2019-02-11 11:52:34 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-02-11 11:52:34 +0100 |
commit | 88108880bbdba473cfe36ecbebc1c3c4f972e102 (patch) | |
tree | b242efb3b4f0d7e49d88f2d1f2063b5b3b0489c0 /server/lib/client-html.ts | |
parent | 53a94c7cfa8368da4cd248d65df8346905938f0c (diff) | |
parent | 9b712a2017e4ab3cf12cd6bd58278905520159d0 (diff) | |
download | PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.tar.gz PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.tar.zst PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.zip |
Merge branch 'develop' into pr/1217
Diffstat (limited to 'server/lib/client-html.ts')
-rw-r--r-- | server/lib/client-html.ts | 61 |
1 files changed, 38 insertions, 23 deletions
diff --git a/server/lib/client-html.ts b/server/lib/client-html.ts index fc013e0c3..b2c376e20 100644 --- a/server/lib/client-html.ts +++ b/server/lib/client-html.ts | |||
@@ -1,7 +1,7 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import * as Bluebird from 'bluebird' | 2 | import * as Bluebird from 'bluebird' |
3 | import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/models/i18n/i18n' | 3 | import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/models/i18n/i18n' |
4 | import { CONFIG, CUSTOM_HTML_TAG_COMMENTS, EMBED_SIZE, STATIC_PATHS } from '../initializers' | 4 | import { CONFIG, CUSTOM_HTML_TAG_COMMENTS, EMBED_SIZE } from '../initializers' |
5 | import { join } from 'path' | 5 | import { join } from 'path' |
6 | import { escapeHTML } from '../helpers/core-utils' | 6 | import { escapeHTML } from '../helpers/core-utils' |
7 | import { VideoModel } from '../models/video/video' | 7 | import { VideoModel } from '../models/video/video' |
@@ -18,21 +18,13 @@ export class ClientHtml { | |||
18 | ClientHtml.htmlCache = {} | 18 | ClientHtml.htmlCache = {} |
19 | } | 19 | } |
20 | 20 | ||
21 | static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) { | 21 | static async getDefaultHTMLPage (req: express.Request, res: express.Response, paramLang?: string) { |
22 | const path = ClientHtml.getIndexPath(req, res, paramLang) | 22 | const html = await ClientHtml.getIndexHTML(req, res, paramLang) |
23 | if (ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path] | ||
24 | |||
25 | const buffer = await readFile(path) | ||
26 | 23 | ||
27 | let html = buffer.toString() | 24 | let customHtml = ClientHtml.addTitleTag(html) |
28 | 25 | customHtml = ClientHtml.addDescriptionTag(customHtml) | |
29 | html = ClientHtml.addTitleTag(html) | ||
30 | html = ClientHtml.addDescriptionTag(html) | ||
31 | html = ClientHtml.addCustomCSS(html) | ||
32 | 26 | ||
33 | ClientHtml.htmlCache[path] = html | 27 | return customHtml |
34 | |||
35 | return html | ||
36 | } | 28 | } |
37 | 29 | ||
38 | static async getWatchHTMLPage (videoId: string, req: express.Request, res: express.Response) { | 30 | static async getWatchHTMLPage (videoId: string, req: express.Request, res: express.Response) { |
@@ -55,7 +47,26 @@ export class ClientHtml { | |||
55 | return ClientHtml.getIndexHTML(req, res) | 47 | return ClientHtml.getIndexHTML(req, res) |
56 | } | 48 | } |
57 | 49 | ||
58 | return ClientHtml.addOpenGraphAndOEmbedTags(html, video) | 50 | let customHtml = ClientHtml.addTitleTag(html, escapeHTML(video.name)) |
51 | customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(video.description)) | ||
52 | customHtml = ClientHtml.addOpenGraphAndOEmbedTags(customHtml, video) | ||
53 | |||
54 | return customHtml | ||
55 | } | ||
56 | |||
57 | private static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) { | ||
58 | const path = ClientHtml.getIndexPath(req, res, paramLang) | ||
59 | if (ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path] | ||
60 | |||
61 | const buffer = await readFile(path) | ||
62 | |||
63 | let html = buffer.toString() | ||
64 | |||
65 | html = ClientHtml.addCustomCSS(html) | ||
66 | |||
67 | ClientHtml.htmlCache[path] = html | ||
68 | |||
69 | return html | ||
59 | } | 70 | } |
60 | 71 | ||
61 | private static getIndexPath (req: express.Request, res: express.Response, paramLang?: string) { | 72 | private static getIndexPath (req: express.Request, res: express.Response, paramLang?: string) { |
@@ -81,14 +92,18 @@ export class ClientHtml { | |||
81 | return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html') | 92 | return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html') |
82 | } | 93 | } |
83 | 94 | ||
84 | private static addTitleTag (htmlStringPage: string) { | 95 | private static addTitleTag (htmlStringPage: string, title?: string) { |
85 | const titleTag = '<title>' + CONFIG.INSTANCE.NAME + '</title>' | 96 | let text = title || CONFIG.INSTANCE.NAME |
97 | if (title) text += ` - ${CONFIG.INSTANCE.NAME}` | ||
98 | |||
99 | const titleTag = `<title>${text}</title>` | ||
86 | 100 | ||
87 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag) | 101 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag) |
88 | } | 102 | } |
89 | 103 | ||
90 | private static addDescriptionTag (htmlStringPage: string) { | 104 | private static addDescriptionTag (htmlStringPage: string, description?: string) { |
91 | const descriptionTag = `<meta name="description" content="${CONFIG.INSTANCE.SHORT_DESCRIPTION}" />` | 105 | const content = description || CONFIG.INSTANCE.SHORT_DESCRIPTION |
106 | const descriptionTag = `<meta name="description" content="${content}" />` | ||
92 | 107 | ||
93 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag) | 108 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag) |
94 | } | 109 | } |
@@ -100,8 +115,8 @@ export class ClientHtml { | |||
100 | } | 115 | } |
101 | 116 | ||
102 | private static addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoModel) { | 117 | private static addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoModel) { |
103 | const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName() | 118 | const previewUrl = CONFIG.WEBSERVER.URL + video.getPreviewStaticPath() |
104 | const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid | 119 | const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath() |
105 | 120 | ||
106 | const videoNameEscaped = escapeHTML(video.name) | 121 | const videoNameEscaped = escapeHTML(video.name) |
107 | const videoDescriptionEscaped = escapeHTML(video.description) | 122 | const videoDescriptionEscaped = escapeHTML(video.description) |
@@ -172,8 +187,8 @@ export class ClientHtml { | |||
172 | // Schema.org | 187 | // Schema.org |
173 | tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>` | 188 | tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>` |
174 | 189 | ||
175 | // SEO | 190 | // SEO, use origin video url so Google does not index remote videos |
176 | tagsString += `<link rel="canonical" href="${videoUrl}" />` | 191 | tagsString += `<link rel="canonical" href="${video.url}" />` |
177 | 192 | ||
178 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.OPENGRAPH_AND_OEMBED, tagsString) | 193 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.OPENGRAPH_AND_OEMBED, tagsString) |
179 | } | 194 | } |