Skip to content

Commit d2f58d7

Browse files
fix: implement fallback URL fetching for improved content retrieval
1 parent cb70051 commit d2f58d7

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

dashboard/src/components/users/action-buttons.tsx

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,46 @@ const ActionButtons: FC<ActionButtonsProps> = ({ user }) => {
317317
alert(`${message}\n\n${content}`)
318318
}
319319

320-
const fetchContent = async (url: string): Promise<string> => {
321-
const response = await fetch(url)
322-
if (!response.ok) {
323-
throw new Error(`HTTP error! status: ${response.status}`)
320+
const buildDashboardFallbackUrl = (url: string): string | null => {
321+
try {
322+
const parsedUrl = new URL(url, window.location.origin)
323+
if (parsedUrl.origin === window.location.origin) return null
324+
325+
return `${window.location.origin}${parsedUrl.pathname}${parsedUrl.search}${parsedUrl.hash}`
326+
} catch (error) {
327+
console.error('Failed to build fallback url:', error)
328+
return null
329+
}
330+
}
331+
332+
async function fetchWithDashboardFallback<T>(url: string, parser: (response: Response) => Promise<T>): Promise<T> {
333+
const attemptFetch = async (targetUrl: string) => {
334+
const response = await fetch(targetUrl)
335+
if (!response.ok) {
336+
throw new Error(`HTTP error! status: ${response.status}`)
337+
}
338+
return parser(response)
339+
}
340+
341+
try {
342+
return await attemptFetch(url)
343+
} catch (primaryError) {
344+
const fallbackUrl = buildDashboardFallbackUrl(url)
345+
if (fallbackUrl) {
346+
try {
347+
return await attemptFetch(fallbackUrl)
348+
} catch (fallbackError) {
349+
console.error('Fallback fetch failed:', fallbackError)
350+
}
351+
}
352+
throw primaryError
324353
}
325-
return response.text()
326354
}
327355

356+
const fetchContent = (url: string): Promise<string> => fetchWithDashboardFallback(url, response => response.text())
357+
358+
const fetchBlob = (url: string): Promise<Blob> => fetchWithDashboardFallback(url, response => response.blob())
359+
328360
const handleLinksCopy = async (subLink: SubscribeLink) => {
329361
try {
330362
if (isIOS()) {
@@ -373,12 +405,7 @@ const ActionButtons: FC<ActionButtonsProps> = ({ user }) => {
373405
}
374406
} else {
375407
// Non-iOS: regular download
376-
const response = await fetch(subLink.link)
377-
if (!response.ok) {
378-
throw new Error(`HTTP error! status: ${response.status}`)
379-
}
380-
381-
const blob = await response.blob()
408+
const blob = await fetchBlob(subLink.link)
382409
const url = window.URL.createObjectURL(blob)
383410
const a = document.createElement('a')
384411
a.href = url

0 commit comments

Comments
 (0)