Skip to content

Commit

Permalink
Add description and icon to preview URLs for non-document resources g…
Browse files Browse the repository at this point in the history
…ristlabs#1242

When pasting a URL in some app or website that allows previewing the
link, for other resources than documents, you were offered an irrelevant
description. This patches aims to give a generic description of what is
Grist.
  • Loading branch information
fflorent committed Oct 4, 2024
1 parent e7c1002 commit 7fb5208
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
32 changes: 14 additions & 18 deletions app/server/lib/sendAppPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ export function makeSendAppPage({ server, staticDir, tag, testLogin, baseDomain
const staticTag = options.tag || tag;
// If boot tag is used, serve assets locally, otherwise respect
// APP_STATIC_URL.
const staticOrigin = staticTag === 'boot' ? '' : (process.env.APP_STATIC_URL || '');
const staticBaseUrl = `${staticOrigin}/v/${staticTag}/`;
const staticOrigin = staticTag === 'boot' ? '' : (process.env.APP_STATIC_URL || config.homeUrl || '');
const staticBaseUrl = new URL(`/v/${staticTag}/`, staticOrigin).href;
const customHeadHtmlSnippet = server.create.getExtraHeadHtml?.() ?? "";
const warning = testLogin ? "<div class=\"dev_warning\">Authentication is not enforced</div>" : "";
// Preload all languages that will be used or are requested by client.
Expand All @@ -173,7 +173,7 @@ export function makeSendAppPage({ server, staticDir, tag, testLogin, baseDomain
const content = fileContent
.replace("<!-- INSERT WARNING -->", warning)
.replace("<!-- INSERT TITLE -->", getPageTitle(req, config))
.replace("<!-- INSERT META -->", getPageMetadataHtmlSnippet(config))
.replace("<!-- INSERT META -->", getPageMetadataHtmlSnippet(req, config, staticBaseUrl))
.replace("<!-- INSERT TITLE SUFFIX -->", getPageTitleSuffix(server.getGristConfig()))
.replace("<!-- INSERT BASE -->", `<base href="${staticBaseUrl}">` + tagManagerSnippet)
.replace("<!-- INSERT LOCALE -->", preloads)
Expand Down Expand Up @@ -284,25 +284,21 @@ function getPageTitle(req: express.Request, config: GristLoadConfig): string {
*
* Note: The string returned is escaped and safe to insert into HTML.
*/
function getPageMetadataHtmlSnippet(config: GristLoadConfig): string {
function getPageMetadataHtmlSnippet(req: express.Request, config: GristLoadConfig, staticBaseUrl: string): string {
const metadataElements: string[] = [];
const maybeDoc = getDocFromConfig(config);

const description = maybeDoc?.options?.description;
if (description) {
const content = handlebars.Utils.escapeExpression(description);
metadataElements.push(`<meta name="description" content="${content}">`);
metadataElements.push(`<meta property="og:description" content="${content}">`);
metadataElements.push(`<meta name="twitter:description" content="${content}">`);
}
const description = maybeDoc?.options?.description ?? String(translate(req, 'gristMetaDescription'));
const escapedDescription = handlebars.Utils.escapeExpression(description);
metadataElements.push(`<meta name="description" content="${escapedDescription}">`);
metadataElements.push(`<meta property="og:description" content="${escapedDescription}">`);
metadataElements.push(`<meta name="twitter:description" content="${escapedDescription}">`);

const icon = maybeDoc?.options?.icon;
if (icon) {
const content = handlebars.Utils.escapeExpression(icon);
metadataElements.push(`<meta name="thumbnail" content="${content}">`);
metadataElements.push(`<meta property="og:image" content="${content}">`);
metadataElements.push(`<meta name="twitter:image" content="${content}">`);
}
const icon = maybeDoc?.options?.icon ?? new URL('/img/logo-grist.png', staticBaseUrl).href;
const escapedIcon = handlebars.Utils.escapeExpression(icon);
metadataElements.push(`<meta name="thumbnail" content="${escapedIcon}">`);
metadataElements.push(`<meta property="og:image" content="${escapedIcon}">`);
metadataElements.push(`<meta name="twitter:image" content="${escapedIcon}">`);

return metadataElements.join('\n');
}
Expand Down
3 changes: 2 additions & 1 deletion static/locales/en.server.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"sendAppPage": {
"Loading": "Loading"
"Loading": "Loading",
"gristMetaDescription": "Grist is the evolution of spreadsheets."
},
"oidc": {
"emailNotVerifiedError": "Please verify your email with the identity provider, and log in again."
Expand Down

0 comments on commit 7fb5208

Please sign in to comment.