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 */
7778export 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