Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/images/invite-open-graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"analyze:ios": "npx expo export --source-maps --platform ios --no-bytecode && npx source-map-explorer 'dist/_expo/static/js/ios/*.js'",
"analyze:android": "npx expo export --source-maps --platform android --no-bytecode && npx source-map-explorer 'dist/_expo/static/js/android/*.js'",
"analyze:web": "npx source-map-explorer 'dist/_expo/static/js/web/*.js'",
"export:web": "npm run assets:update && npm run assets:sync-public && node scripts/export-web.mjs",
"export:web": "npm run assets:update && npm run assets:sync-public && node scripts/export-web.mjs && node scripts/invite-link-preview.js",
"sourcemaps:upload": "node scripts/upload-sourcemaps.mjs",
"sourcemaps:strip": "find dist -name '*.map' -type f -delete",
"build:web": "npm run export:web && npm run sourcemaps:upload && npm run sourcemaps:strip",
Expand Down
65 changes: 65 additions & 0 deletions scripts/__tests__/invite-link-preview.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { applyInvitePreview, INVITE_PREVIEW } from '../invite-link-preview';

// A trimmed copy of the tags the static export writes into every page from
// app/+html.tsx, so the swap is tested against the real shape of the markup.
const EXPORTED_HEAD =
`<!DOCTYPE html><html lang="en"><head>` +
`<title></title>` +
`<meta name="description" content="Solid is the savings super-app that combines DeFi yields with traditional banking. Earn competitive yields on your savings with institutional-grade security."/>` +
`<link rel="canonical" href="https://app.solid.xyz"/>` +
`<meta property="og:type" content="website"/>` +
`<meta property="og:title" content="Solid - The Savings Super-App"/>` +
`<meta property="og:description" content="Solid is a yield-bearing account that works in the background."/>` +
`<meta property="og:image" content="https://app.solid.xyz/assets/images/solid-open-graph.png"/>` +
`<meta property="og:url" content="https://app.solid.xyz"/>` +
`<meta name="twitter:card" content="summary_large_image"/>` +
`<meta name="twitter:title" content="Solid - The Savings Super-App"/>` +
`<meta name="twitter:description" content="Solid is a yield-bearing account that works in the background."/>` +
`<meta name="twitter:image" content="https://app.solid.xyz/assets/images/solid-open-graph.png"/>` +
`</head><body></body></html>`;

describe('applyInvitePreview', () => {
it('swaps title, description and image across og/twitter tags', () => {
const out = applyInvitePreview(EXPORTED_HEAD);

expect(out).toContain(`<title>You're invited to Solid</title>`);
expect(out).toContain(`<meta property="og:title" content="You're invited to Solid"/>`);
expect(out).toContain(
`<meta property="og:description" content="${INVITE_PREVIEW.description}"/>`,
);
expect(out).toContain(`<meta property="og:image" content="${INVITE_PREVIEW.image}"/>`);
expect(out).toContain(`<meta name="twitter:title" content="You're invited to Solid"/>`);
expect(out).toContain(`<meta name="twitter:image" content="${INVITE_PREVIEW.image}"/>`);
expect(out).toContain(`<meta name="description" content="${INVITE_PREVIEW.description}"/>`);
});

it('leaves the site-wide canonical link and og:url in place', () => {
const out = applyInvitePreview(EXPORTED_HEAD);

expect(out).toContain(`<link rel="canonical" href="https://app.solid.xyz"/>`);
expect(out).toContain(`<meta property="og:url" content="https://app.solid.xyz"/>`);
});

it('removes the old site-wide branding from the invite page', () => {
const out = applyInvitePreview(EXPORTED_HEAD);

expect(out).not.toContain('Solid - The Savings Super-App');
expect(out).not.toContain('solid-open-graph.png');
});

it('escapes characters that would break an attribute', () => {
const out = applyInvitePreview(EXPORTED_HEAD, {
title: 'Ampersand & "quote" <tag>',
description: 'd',
image: 'i',
});

expect(out).toContain(
'<meta property="og:title" content="Ampersand &amp; &quot;quote&quot; &lt;tag>"/>',
);
});

it('throws if an expected tag is missing', () => {
expect(() => applyInvitePreview('<html><head></head></html>')).toThrow('og:title');
});
});
63 changes: 63 additions & 0 deletions scripts/invite-link-preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Gives the invite page (/invite/CODE) its own link preview.
*
* The static web export writes the same site-wide tags from app/+html.tsx into
* every page, and link-preview crawlers (Telegram, WhatsApp) do not run
* JavaScript, so the invite-specific tags have to be swapped into the exported
* HTML itself. Run after `expo export -p web`.
*
* CommonJS so the pure helper can be unit-tested under jest-expo, which does
* not transform ESM `.mjs` sources.
*/
const { existsSync, readFileSync, writeFileSync } = require('node:fs');

const INVITE_PREVIEW = {
title: "You're invited to Solid",
description: 'Sign up with this invite and get card credit to spend with your Solid card.',
image: 'https://app.solid.xyz/assets/images/invite-open-graph.png',
};

const escapeAttr = value =>
value.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;');

const TAGS = [
['property', 'og:title', 'title'],
['property', 'og:description', 'description'],
['property', 'og:image', 'image'],
['name', 'twitter:title', 'title'],
['name', 'twitter:description', 'description'],
['name', 'twitter:image', 'image'],
['name', 'description', 'description'],
];

/**
* Returns the HTML with the invite preview tags applied; throws if a tag is missing.
* The canonical link and og:url keep the site-wide value: the page is one static
* file for every code and there is no bare /invite page to point at.
*/
function applyInvitePreview(html, preview = INVITE_PREVIEW) {
let out = html;
for (const [attr, key, field] of TAGS) {
const tag = new RegExp(`<meta ${attr}="${key}" content="[^"]*"\\s*/?>`);
if (!tag.test(out)) throw new Error(`[invite-preview] ${key} tag not found`);
out = out.replace(tag, `<meta ${attr}="${key}" content="${escapeAttr(preview[field])}"/>`);
}
out = out.replace(
/<title([^>]*)>[^<]*<\/title>/,
`<title$1>${escapeAttr(preview.title)}</title>`,
);
return out;
}

module.exports = { applyInvitePreview, INVITE_PREVIEW };

if (require.main === module) {
const file = process.argv[2] ?? 'dist/invite/[code].html';
if (!existsSync(file)) {
// Non-production builds export a single page and have no invite HTML.
console.warn(`[invite-preview] ${file} not found, skipped`);
process.exit(0);
}
writeFileSync(file, applyInvitePreview(readFileSync(file, 'utf8')));
console.log(`[invite-preview] updated ${file}`);
}
Loading