@@ -326,42 +326,45 @@ export function getAllProxyConfigs(collectPrefix: string): Record<string, ProxyC
326326}
327327
328328export interface InterceptRule {
329- /** Domain pattern to match (supports wildcards like *.google-analytics.com) */
329+ /** Domain to match (exact match or implicit subdomain suffix match, e.g. google-analytics.com matches *.google-analytics.com) */
330330 pattern : string
331331 /** Path prefix to match and strip from the original URL (e.g., /tr for www.facebook.com/tr) */
332332 pathPrefix : string
333333 /** Local path prefix to rewrite to */
334334 target : string
335335}
336336
337+ /**
338+ * Convert route definitions into intercept rules for client-side URL rewriting.
339+ */
340+ export function routesToInterceptRules ( routes : Record < string , { proxy : string } > ) : InterceptRule [ ] {
341+ const rules : InterceptRule [ ] = [ ]
342+ for ( const [ localPath , { proxy } ] of Object . entries ( routes ) ) {
343+ // Extract domain and path prefix from proxy URL
344+ // e.g., "https://www.facebook.com/tr/**" -> domain="www.facebook.com", pathPrefix="/tr"
345+ // e.g., "https://connect.facebook.net/**" -> domain="connect.facebook.net", pathPrefix=""
346+ const match = proxy . match ( / ^ h t t p s ? : \/ \/ ( [ ^ / ] + ) ( \/ .* ) ? \/ \* \* $ / )
347+ if ( match ?. [ 1 ] ) {
348+ const domain = match [ 1 ]
349+ const pathPrefix = match [ 2 ] || ''
350+ const target = localPath . replace ( / \/ \* \* $ / , '' )
351+ rules . push ( { pattern : domain , pathPrefix, target } )
352+ }
353+ }
354+ return rules
355+ }
356+
337357/**
338358 * Get intercept rules from all proxy configs.
339359 * These rules are embedded in the __nuxtScripts client plugin to rewrite
340360 * outbound fetch/sendBeacon URLs through the first-party proxy.
341361 */
342362export function getInterceptRules ( collectPrefix : string ) : InterceptRule [ ] {
343363 const configs = buildProxyConfig ( collectPrefix )
344- const rules : InterceptRule [ ] = [ ]
345-
346- // Extract unique domain -> target mappings from route rules
364+ const allRoutes : Record < string , { proxy : string } > = { }
347365 for ( const config of Object . values ( configs ) ) {
348- if ( ! config . routes )
349- continue
350- for ( const [ localPath , { proxy } ] of Object . entries ( config . routes ) ) {
351- // Extract domain and path prefix from proxy URL
352- // e.g., "https://www.facebook.com/tr/**" -> domain="www.facebook.com", pathPrefix="/tr"
353- // e.g., "https://connect.facebook.net/**" -> domain="connect.facebook.net", pathPrefix=""
354- const match = proxy . match ( / ^ h t t p s ? : \/ \/ ( [ ^ / ] + ) ( \/ .* ) ? \/ \* \* $ / )
355- if ( match ?. [ 1 ] ) {
356- const domain = match [ 1 ]
357- // Path prefix is everything between domain and /** (e.g., /tr), empty if none
358- const pathPrefix = match [ 2 ] || ''
359- // Extract target prefix: "/_proxy/meta-tr/**" -> "/_proxy/meta-tr"
360- const target = localPath . replace ( / \/ \* \* $ / , '' )
361- rules . push ( { pattern : domain , pathPrefix, target } )
362- }
363- }
366+ if ( config . routes )
367+ Object . assign ( allRoutes , config . routes )
364368 }
365-
366- return rules
369+ return routesToInterceptRules ( allRoutes )
367370}
0 commit comments