diff --git a/assets/images/invite-open-graph.png b/assets/images/invite-open-graph.png new file mode 100644 index 000000000..b08192a26 Binary files /dev/null and b/assets/images/invite-open-graph.png differ diff --git a/package.json b/package.json index b93588d3a..848ad4cb4 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/scripts/__tests__/invite-link-preview.test.ts b/scripts/__tests__/invite-link-preview.test.ts new file mode 100644 index 000000000..3410a56fb --- /dev/null +++ b/scripts/__tests__/invite-link-preview.test.ts @@ -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 = + `` + + `` + + `` + + `` + + `` + + `` + + `` + + `` + + `` + + `` + + `` + + `` + + `` + + ``; + +describe('applyInvitePreview', () => { + it('swaps title, description and image across og/twitter tags', () => { + const out = applyInvitePreview(EXPORTED_HEAD); + + expect(out).toContain(`You're invited to Solid`); + expect(out).toContain(``); + expect(out).toContain( + ``, + ); + expect(out).toContain(``); + expect(out).toContain(``); + expect(out).toContain(``); + expect(out).toContain(``); + }); + + it('leaves the site-wide canonical link and og:url in place', () => { + const out = applyInvitePreview(EXPORTED_HEAD); + + expect(out).toContain(``); + expect(out).toContain(``); + }); + + 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" ', + description: 'd', + image: 'i', + }); + + expect(out).toContain( + '', + ); + }); + + it('throws if an expected tag is missing', () => { + expect(() => applyInvitePreview('')).toThrow('og:title'); + }); +}); diff --git a/scripts/invite-link-preview.js b/scripts/invite-link-preview.js new file mode 100644 index 000000000..091734b0e --- /dev/null +++ b/scripts/invite-link-preview.js @@ -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, '&').replace(/"/g, '"').replace(/`); + if (!tag.test(out)) throw new Error(`[invite-preview] ${key} tag not found`); + out = out.replace(tag, ``); + } + out = out.replace( + /]*)>[^<]*<\/title>/, + `${escapeAttr(preview.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}`); +}