Skip to content

Commit d9d0ebf

Browse files
adewaleclaude
andcommitted
fix: Use request origin for social preview URLs
The BASE_URL was hardcoded to keyboardia.dev, causing social previews to fail on staging.keyboardia.dev. Now derives the base URL from the request's origin, making it work correctly in all environments. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 83935cf commit d9d0ebf

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

app/src/worker/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,11 @@ export default {
195195
tempo: sessionData.state?.tempo ?? 120
196196
};
197197

198+
// Use the request's origin as the base URL (works for staging, production, etc.)
199+
const baseUrl = url.origin;
200+
198201
// Transform with social meta tags
199-
return injectSocialMeta(baseResponse, meta);
202+
return injectSocialMeta(baseResponse, meta, baseUrl);
200203
}
201204
}
202205
// Fall through to normal SPA serving if session not found

app/src/worker/social-preview.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* Uses HTMLRewriter for streaming HTML transformation.
66
*/
77

8-
const BASE_URL = 'https://keyboardia.dev';
9-
108
/**
119
* Regex to detect social media crawlers via User-Agent
1210
*/
@@ -45,7 +43,7 @@ export function escapeHtml(str: string): string {
4543
/**
4644
* Generate JSON-LD structured data for Schema.org
4745
*/
48-
function generateJsonLd(session: SessionMeta, url: string): string {
46+
function generateJsonLd(session: SessionMeta, url: string, baseUrl: string): string {
4947
// XSS prevention: escape session name for JSON context
5048
const safeName = session.name ? escapeHtml(session.name) : 'Untitled Session';
5149

@@ -58,7 +56,7 @@ function generateJsonLd(session: SessionMeta, url: string): string {
5856
'creator': {
5957
'@type': 'WebApplication',
6058
'name': 'Keyboardia',
61-
'url': 'https://keyboardia.dev',
59+
'url': baseUrl,
6260
'description': 'Collaborative step sequencer for creating beats together in real-time'
6361
},
6462
'audio': {
@@ -73,10 +71,14 @@ function generateJsonLd(session: SessionMeta, url: string): string {
7371

7472
/**
7573
* Inject social media meta tags into HTML response using HTMLRewriter
74+
* @param response - The base HTML response to transform
75+
* @param session - Session metadata for the preview
76+
* @param baseUrl - The base URL of the current environment (e.g., https://staging.keyboardia.dev)
7677
*/
7778
export function injectSocialMeta(
7879
response: Response,
79-
session: SessionMeta
80+
session: SessionMeta,
81+
baseUrl: string
8082
): Response {
8183
// XSS prevention: escape user-provided session name
8284
const safeName = session.name ? escapeHtml(session.name) : null;
@@ -89,8 +91,8 @@ export function injectSocialMeta(
8991
? `Listen to "${safeName}" on Keyboardia. A ${session.trackCount}-track beat at ${session.tempo} BPM. Create beats together in real-time.`
9092
: `Listen to this beat on Keyboardia. A ${session.trackCount}-track composition at ${session.tempo} BPM. Create beats together in real-time.`;
9193

92-
const url = `${BASE_URL}/s/${session.id}`;
93-
const ogImage = `${BASE_URL}/og/${session.id}.png`;
94+
const url = `${baseUrl}/s/${session.id}`;
95+
const ogImage = `${baseUrl}/og/${session.id}.png`;
9496

9597
// Track if we've appended to head (to avoid duplicates)
9698
let headAppended = false;
@@ -159,7 +161,7 @@ export function injectSocialMeta(
159161
el.append(`<meta property="og:site_name" content="Keyboardia" />`, { html: true });
160162
el.append(`<meta property="og:image:width" content="600" />`, { html: true });
161163
el.append(`<meta property="og:image:height" content="315" />`, { html: true });
162-
el.append(generateJsonLd(session, url), { html: true });
164+
el.append(generateJsonLd(session, url, baseUrl), { html: true });
163165
}
164166
})
165167
.transform(response);

0 commit comments

Comments
 (0)