11import { augmentPageWithWordPressThemeStyles } from './wordpress'
22import { buildPopupCacheRecord , cleanPageDetectionRecord } from './popup-cache'
3- import { fetchMainHeadersFallback } from './headers'
3+ import { fetchMainHeadersFallback , mergeHeaderRecords } from './headers'
44import { clearBadge , clearTabSession , getTabData , getTabSnapshot , updateBadgeForTab , writeTabData } from './tab-store'
55import { buildEffectivePageRules , loadDetectorSettings , loadTechRules } from './detector-settings'
66import { scheduleBundleLicenseDetection } from './bundle-license'
@@ -24,7 +24,7 @@ const needsMainHeadersFallback = (record: any, currentUrl: string): boolean => {
2424 const recordUrl = normalizePageUrl ( record . url )
2525 const tabUrl = normalizePageUrl ( currentUrl )
2626 if ( recordUrl && tabUrl && recordUrl !== tabUrl ) return true
27- return ! Number ( record . headerCount || 0 ) && ! ( record . technologies || [ ] ) . length
27+ return Number ( record . headerCount || 0 ) <= 1 && ! Object . keys ( record . headers || { } ) . length && ! ( record . technologies || [ ] ) . length
2828}
2929
3030const headerRecordMatchesUrl = ( record : any , currentUrl : string ) : boolean => {
@@ -33,6 +33,22 @@ const headerRecordMatchesUrl = (record: any, currentUrl: string): boolean => {
3333 return Boolean ( recordUrl && tabUrl && recordUrl === tabUrl )
3434}
3535
36+ const headerRecordSharesPagePath = ( record : any , currentUrl : string ) : boolean => {
37+ try {
38+ const recordUrl = new URL ( String ( record ?. url || '' ) )
39+ const tabUrl = new URL ( String ( currentUrl || '' ) )
40+ return recordUrl . origin === tabUrl . origin && recordUrl . pathname === tabUrl . pathname
41+ } catch {
42+ return false
43+ }
44+ }
45+
46+ const hasUsefulHeaderRecord = ( record : any ) : boolean =>
47+ Boolean ( record && ( Number ( record . headerCount || 0 ) > 1 || Object . keys ( record . headers || { } ) . length || ( record . technologies || [ ] ) . length ) )
48+
49+ const shouldPreserveMainHeaderRecord = ( record : any , currentUrl : string ) : boolean =>
50+ headerRecordMatchesUrl ( record , currentUrl ) || ( headerRecordSharesPagePath ( record , currentUrl ) && hasUsefulHeaderRecord ( record ) )
51+
3652export const saveTabDataAndBadge = async ( tabId : number , data : any , settings : any ) => {
3753 const tab = await getTabSnapshot ( tabId )
3854 if ( ! isDetectablePageUrl ( tab . url ) ) {
@@ -102,8 +118,13 @@ export const runActivePageDetection = async (tabId: number) => {
102118
103119 if ( needsMainHeadersFallback ( data . main , ( page as any ) . url || tab . url ) ) {
104120 const fallback = await fetchMainHeadersFallback ( ( page as any ) . url || '' , rules . headers || { } , settings )
105- if ( fallback ) data . main = fallback
106- else if ( data . main && ! headerRecordMatchesUrl ( data . main , ( page as any ) . url || tab . url ) ) delete data . main
121+ if ( fallback ) {
122+ data . main = shouldPreserveMainHeaderRecord ( data . main , ( page as any ) . url || tab . url )
123+ ? mergeHeaderRecords ( data . main , fallback )
124+ : fallback
125+ } else if ( data . main && ! shouldPreserveMainHeaderRecord ( data . main , ( page as any ) . url || tab . url ) ) {
126+ delete data . main
127+ }
107128 }
108129
109130 data . updatedAt = Date . now ( )
0 commit comments