@@ -29,8 +29,8 @@ export default {
2929 const targetUrl = await lookupUrl ( id , env ) ;
3030
3131 if ( targetUrl ) {
32- // Fire-and-forget click tracking (don't await)
33- trackClick ( id , env ) . catch ( ( ) => { } ) ; // Ignore errors
32+ // Fire-and-forget click tracking using waitUntil to ensure it completes
33+ ctx . waitUntil ( trackClick ( id , env ) ) ;
3434
3535 // Redirect to target URL
3636 return Response . redirect ( targetUrl , 302 ) ;
@@ -102,20 +102,24 @@ async function lookupUrl(id, env) {
102102async function trackClick ( id , env ) {
103103 const apiKey = env . INTERNAL_API_KEY ;
104104 const apiUrl = env . AZURE_API_URL ;
105+
106+ console . log ( `trackClick: id=${ id } , apiUrl=${ apiUrl } , hasKey=${ ! ! apiKey } ` ) ;
107+
105108 if ( ! apiKey || ! apiUrl ) {
106- return ; // No API credentials configured, skip tracking
109+ console . log ( 'trackClick: Missing credentials, skipping' ) ;
110+ return ;
107111 }
108112
109113 try {
110- await fetch ( `${ apiUrl } /api/click/${ id } ` , {
114+ const response = await fetch ( `${ apiUrl } /api/click/${ id } ` , {
111115 method : 'POST' ,
112116 headers : {
113117 'X-Internal-Key' : apiKey ,
114118 'Content-Type' : 'application/json' ,
115119 } ,
116120 } ) ;
121+ console . log ( `trackClick: Response status=${ response . status } ` ) ;
117122 } catch ( error ) {
118- // Silently fail - don't impact redirects
119123 console . error ( 'Error tracking click:' , error ) ;
120124 }
121125}
0 commit comments