Skip to content

Commit 00e6704

Browse files
committed
update related-website-set logic
1 parent 778be72 commit 00e6704

File tree

1 file changed

+39
-37
lines changed

1 file changed

+39
-37
lines changed

dist/well-known.js

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -128,43 +128,45 @@ return Promise.all([
128128
}),
129129
// Privacy Sandbox
130130
parseResponse('/.well-known/related-website-set.json', r => {
131-
return fetchWithTimeout(r.url)
132-
.then(response => {
133-
if (!response.ok) {
134-
// Retry logic
135-
const domain = getRootDomain(window.location.hostname);
136-
const retryUrl = `https://${domain}/.well-known/related-website-set.json`;
137-
return fetchWithTimeout(retryUrl)
138-
.then(retryResponse => {
139-
if (!retryResponse.ok) {
140-
throw new Error(`Retry failed with status: ${retryResponse.status}`);
141-
}
142-
return retryResponse.text();
143-
});
144-
}
145-
return response.text();
146-
})
147-
.then(text => {
148-
let result = {
149-
primary: null,
150-
associatedSites: null
151-
};
152-
try {
153-
const data = JSON.parse(text);
154-
result.primary = data.primary || null;
155-
result.associatedSites = data.associatedSites || null;
156-
} catch (e) {
157-
// Failed to parse JSON, result will contain default values (already set).
158-
}
159-
return result;
160-
})
161-
.catch(error => {
162-
// Return null object on any error
163-
return {
164-
primary: null,
165-
associatedSites: null
166-
};
167-
});
131+
return r.text() // Get the response as text first.
132+
.then(text => {
133+
try {
134+
const data = JSON.parse(text);
135+
return {
136+
primary: data.primary || null,
137+
associatedSites: data.associatedSites || null
138+
};
139+
} catch (error) {
140+
// If JSON parsing fails, try the root domain.
141+
const domain = getRootDomain(window.location.hostname);
142+
const retryUrl = `https://${domain}/.well-known/related-website-set.json`;
143+
return fetchWithTimeout(retryUrl) // Use fetchWithTimeout for the retry.
144+
.then(retryResponse => {
145+
if (!retryResponse.ok) {
146+
return null; // Return null if the retry fails.
147+
}
148+
return retryResponse.text();
149+
})
150+
.then(retryText => {
151+
if (!retryText) {
152+
return null;
153+
}
154+
try {
155+
const retryData = JSON.parse(retryText);
156+
return {
157+
primary: retryData.primary || null,
158+
associatedSites: retryData.associatedSites || null
159+
};
160+
} catch (retryError) {
161+
return null; // Return null if parsing the retry response fails
162+
}
163+
}).catch(e => {
164+
return null;
165+
});
166+
}
167+
}).catch(err => {
168+
return null;
169+
});
168170
}),
169171
parseResponse('/.well-known/privacy-sandbox-attestations.json'), //Attestation File
170172
// privacy

0 commit comments

Comments
 (0)